I would like the program to repeat the "how many" question until the input is an integer:

int X = 0;
 
number_patients:
cout << "how many?\n";
 if (! (cin >> X))
          {
          X = 0;
          cout << "please enter a number\n";
          cin.clear();
          goto number_patients;
          }

This creates an infinite loop where the two output phrases are constantly repeated.
My main issue is that i can't get the program to continue after a false "cin"

You shouldn't use 'goto' it makes a mess of your program and is responsible for a lot of while(1) loops.

I'm not exactly clear on what you're trying to do, but if you want to keep asking for an input while X = 0, you could try something like:

X= 0;
while (X==0)
{
    cout << "how many?\n";
    cin >> X;
}

This keeps asking for input while X==0 == true.

Edited 6 Years Ago by Nick Evan: n/a

I would like the program to repeat the "how many" question until the input is an integer:

int X = 0;
 
number_patients:
cout << "how many?\n";
 if (! (cin >> X))
          {
          X = 0;
          cout << "please enter a number\n";
          cin.clear();
          goto number_patients;
          }

This creates an infinite loop where the two output phrases are constantly repeated.
My main issue is that i can't get the program to continue after a false "cin"

There's no need to use goto here, a loop is better suited. The infinite loop happens when cin fails. You clear the error state but don't remove the characters that caused the stream to enter into an error state. They aren't removed automatically, you need to do it yourself or you'll keep reading the same bad input ad infinitum.

int x = 0;

while ( true ) {
  cout << "How many? ";

  if ( cin >> x )
    break;

  // Clean up a dirty stream
  cin.clear();
  cin.ignore( [I]some big number[/I], '\n' );
  cout << "Please enter a number\n";
}
This article has been dead for over six months. Start a new discussion instead.