-
Question on While Loop
Pretty new to programming. It runs but it doesn't display the error message: Please enter a positive number. Any suggestions? This is just the fragment that won't go.
Code:
do
{
cout <<"The estimated miles driven per year?" << endl;
cin >> miles_per_year;
}
while ( ! ( miles_per_year > 0))
{
cout <<"Please enter a positive number." <<endl;
}
-
You do not have the correct syntax for a "do-while" loop. There should only be one body for this loop; the "do" preceding the body, and the "while" after. Here is a link with a further description.
One way you might implement this loop is as follows:
Code:
do
{
// prompt
// receive input
if(input_not_valid)
// print error message
} while(input_not_valid);
-