I'm stuck with goto. I need to stop cause sometimes, it screws up my code. I used to do something like this.

int command;
start:
command = NULL; // this was a random guess to see if it would work
cin >> command;
if(command == 1){
cout << "Your chose 1" << endl;
pause; //I defined this
goto start;
}

And so after some heated debates over goto, I decided to try out the while. So now my code looks like this.

int command;
cin >> command;
        while(command == 1) 
               { 
                      reset; //resets command to null (this is defined)
     cout << "You chose 1" << endl;
               pause; //defined
  }

But when I run that, the code works other than the fact that it exits out after pause. How do I have it go back to the main "cin"?

Dani AI

Generated

Your original goto worked because each time execution jumped back to the input site. The while version does not because cin >> command is done once outside the loop, so nothing reads a new value after pause. was right to flag those “reset”/“pause” lines — a bare identifier like reset; only does something if it’s a real function or macro, and setting an int to NULL is misleading (NULL is for pointers; use a numeric sentinel like 0).

A simple, reliable pattern is to read inside the loop and validate the stream. This do/while variant keeps prompting until a chosen sentinel (0 here) ends the loop:

#include <iostream>
#include <limits>

int main() {
  int command = -1;
  do {
    std::cout << "Enter command (0 to quit): ";
    if (!(std::cin >> command)) {
      std::cin.clear();
      std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
      continue;
    }
    if (command == 1) std::cout << "You chose 1\n";
  } while (command != 0);
}

That pattern addresses the flow point made (the loop must reach the cin) and implements the safe input loop idea behind ’s while(cin >> ...) suggestion while adding error handling.

Troubleshooting notes: if input fails, call std::cin.clear() and std::cin.ignore(...) before retrying; avoid abusing system("pause") or ambiguous macros named pause/reset because they won’t perform input; initialize variables explicitly; use a switch or small command-dispatch function when commands multiply.

Recommended Answers

All 3 Replies

lines 5 and 7 are do-nothing lines. The don't do a damned thing. You could delete both lines and the loop would work the same way.

>>//resets command to null
Says who???

Think about the flow.

How was the goto working?

Does the while loop take the flow the same way?

You need the flow to reach back to the cin.

For that you need to include the cin also in the loop.

>> But when I run that, the code works other than the fact that it exits out after pause. How do I have it go back to the main "cin"

int cmd = 0;
while( cin >> cmd) {
  if(cmdIsBad()) break; //out of the loop
  /* else logic goes here */
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.