C++ if loop question

gabego's Avatar, Join Date: Feb 2008
Newbie Member
This program will terminate for an input < 0 using this particular exit() function. Is there a function or a different statement to use that will loop back to the first cout statement so the user could input a new number? In this current form, the user has to re-execute the program after making a mistake.

int p, k = 5;

cout << "enter a number greater than or equal to 0" <<;
cin >> p;
cout << p + k;
if (p < 0)
cout << "error try again"
exit(EXIT_FAILURE) ;

Thanks for any comments!!
-Gabe
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
You need to use loops/.
gabego's Avatar, Join Date: Feb 2008
Newbie Member
yea, directly after the cout , do cin again instead of exiting. That would be a loop.