Solved need command line to return to line 16 in C++

December 14, 2012 at 14:27:47
Specs: windows Vista home premium SP2, Intel Pentium dual Core T4200 2.00Ghz

I guess you could call me a beginner in C++.
So i'm building this program to identify faults in a program by a pre-programmed fault identifier. What I need is so after each IF operation (IF the identifier equals a certain identifier) it returns to line 16 and starts from there. It scans the lines down and chooses an operation if matches the selected number. HOWEVER the current programming it scans down, chooses an identifier BUT i need it to go back to line 16 so you don't need to re-start the program every time. Any help?
P.S How do I make this program open other programs from inside itself?

~oldie
Not everyone can decipher Klingon script...
chay' ta' SoH tlhe' vam Doch Daq


See More: need command line to return to line 16 in C++

Report •


✔ Best Answer
December 24, 2012 at 02:16:51


Maybe I'm too late for an answer but I use:

LOOP:
<code here>
goto LOOP;



#1
December 14, 2012 at 23:27:44

You need to read up about "do...while" loops and "switch...case" statements.

Have a look at this thread for various ways to run an external program: http://stackoverflow.com/questions/...


Report •

#2
December 15, 2012 at 10:50:38

It would depend on how you're opening the file, but find the start of line 16, use tellg() and save the value. Then, when you want to go back to line 16, just feed that value back into seekg().

As for programs, system() is the C way of running programs, but your program will wait for the new program to return before proceeding. Beyond that, it's platform specific, and ijack's link answers that question nicely for Win32.

How To Ask Questions The Smart Way


Report •

#3
December 15, 2012 at 12:52:57

I took it that the OP was asking how to return to line 16 of the program rather than finding line 16 in the file. It seemed to make more sense of the question.

Report •

Related Solutions

#4
December 15, 2012 at 13:09:52

Possibly. OLDISGOOD's meaning is lost in his overuse of the pronoun "it," but I think we've covered all bases this way.

How To Ask Questions The Smart Way


Report •

#5
December 19, 2012 at 10:18:18

I seem to be confuzing everyone here, let me try to simplifiy:
When my program runs, it does one of the IF commands, then terminates to the last 3 end lines in the program. What I need it to do is that when each operation is completed, the program would return to line 16 and wait for further instructions. line 16 is the original data entrance point, and all data entered there counts for all the IF operations If you want I can post some sample code for you to peruse.

~oldie
Not everyone can decipher Klingon script...
chay' ta' SoH tlhe' vam Doch Daq


Report •

#6
December 19, 2012 at 14:40:35

In that case, you should take a look at ijack's advice, and learn about looping.

How To Ask Questions The Smart Way


Report •

#7
December 19, 2012 at 17:11:20

Yes, posting your code, with some comments about what you want to do, would be helpful.

Report •

#8
December 19, 2012 at 17:36:36

here is the code. So far I have only gotten past the basic inputs (how to use the program, clear the screen, etc) I started it from line 13 as that where the loop needs to begin and everything above that is just the usual 'int a, b, c;' stuff.

line 13: cout<< "" <<endl;
cout<<"CommandString:>";
cin>> text;

if
(text==1){
cout<<"HELP FILE" <<endl;
cout<<"Basic Commands:" <<endl;
cout<<"1: Displays Help File" <<endl;
cout<<"2: Clears Screen" <<endl;
cout<<"3: Displays the contents of current directory" <<endl;
cout<<"" <<endl;
cout<<"CommandString:>";
cin>> text;
}

if
(text==2){
system("cls");
cout<<"CommandString:>";
cin>> text;
}
if
(text==3){
system("dir");
cout<<"CommandString:>";
cin>> text;
}
else{
cout<<"Command not recognized." <<endl;
cout<<"CommandString:>";
cin>> text;
}

cin.get();
cin.get();
return 0;
}

P.S don't ask me why I put the DIR command in. Just wanted to make something ;-)

~oldie
Not everyone can decipher Klingon script...
chay' ta' SoH tlhe' vam Doch Daq


Report •

#9
December 20, 2012 at 01:45:10

http://www.cplusplus.com/doc/tutori...

Concentrate on the "do while" loop and the "switch" statement. If you want to write good programs you will ignore the "goto" statement until you fully understand the language.


Report •

#10
December 20, 2012 at 06:49:39

I'd probably rewrite it like this. It still needs some more error handling, but that's an exercise for the reader.
//Not shown: The start of your program, which you forgot to include.
  while (cout << "\nCommandString:>" << flush && cin >> text && text != 4) {
    switch (text) {
    case 1:
      cout <<"HELP FILE\n"
        << "Basic Commands:\n"
        << "1: Displays Help File\n"
        << "2: Clears Screen\n"
        << "3: Displays the contents of current directory\n" << endl;
      break;
    case 2:
      system("cls");
      break;
    case 3:
      system("dir");
    case 4:
      break;
    default:
      cout << "Command not recognized." << endl;
    }
  }
  cin.get();
  cin.get();
  return 0;
}

How To Ask Questions The Smart Way


Report •

#11
December 20, 2012 at 09:48:06

You know what, just forget it. I never was much of a whiz at C++ and i'm not really inclined to start right this second. I'll just type up a text file, print it out, and pass it around at work so people will quit calling me when i'm not the IT guy. Just this one program that we use that i'm good at, so everyone automagically thinks that i'm the guy to fix it. I'm not. Whatever.
Thanks for the help anyway! I still might work on some C++ learning in the near future.... ;-)

~oldie
Not everyone can decipher Klingon script...
chay' ta' SoH tlhe' vam Doch Daq


Report •

#12
December 20, 2012 at 10:13:36

Well that's the wrong attitude for learning.

In all seriousness, if you're just looking for a quick and easy way (nothing in C++ is ever quick nor easy) to throw some basic scripted task together, you might try one of the scripting languages.

How To Ask Questions The Smart Way


Report •

#13
December 20, 2012 at 13:29:40

Well, that was a waste of everyone's time, wasn't it. :-{

Report •

#14
December 24, 2012 at 02:16:51
✔ Best Answer


Maybe I'm too late for an answer but I use:

LOOP:
<code here>
goto LOOP;


Report •

#15
December 26, 2012 at 11:01:22

Hey, iforgot,
I tried your loop code, seems to work but I have some weird glitches that will need to be worked out. When you do a command, it does the command then requires you to push enter before you can enter another command. I'll find it eventually.

P.S I apologize for wasting people's time, I just wanted a loop statement like iforgot's now that I realize it ;-)

~oldie
Not everyone can decipher Klingon script...
chay' ta' SoH tlhe' vam Doch Daq


Report •

#16
December 26, 2012 at 12:21:15

Suggested reading: Go To Statement Considered Harmful.

Also, this might sound vain but I do suggest my version, as it fixes a few bugs in your version. If you don't understand it, ask questions.

How To Ask Questions The Smart Way


Report •


Ask Question