int main()
{
again: // a label
int i;
cout<<"enter i "<<endl;
cin>>i ;
if ( cin.fail())
{
cin.ignore(256);
goto again; // if fail then goto again
}
else
{
cout<<"i is : "<<i<<endl;
}
return 0;
}
when suppose i enter a into i which does the stream bad
and when exucation goes to again ... then the programm sort of goes in an infinite loop ,,, meaning it dsnt take cin from the console agan :(
can sm bdy plz help
I'm not sure what the point was in this program but try running this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
int main()
{
int i = 0;
cout << "Enter i: ";
cin >> i;
cin.ignore(); // ignore the newline character left in the stream from cin
if(cin.fail())
cout << "\nThe failbit flag has been set\n";
else
cout << "The stream is good to use\n";
cin.get();
return 0;
}
You're right it can. By would you when you have other more contollable means of performing the same task. The goto statement makes it hard for you to follow your program whilst using the control structures implemented in the C++ language makes it much easier. So yes, there is an issue.