Question on While Loop

This is a discussion on Question on While Loop within the C++ Programming forums, part of the General Programming Boards category; Pretty new to programming. It runs but it doesn't display the error message: Please enter a positive number. Any suggestions? ...

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    11

    Question 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;
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    2,287
    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);

  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    11
    thanks !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. While loop question.
    By darren78 in forum C++ Programming
    Replies: 8
    Last Post: 07-01-2010, 11:06 AM
  2. while loop question
    By nicz888 in forum C++ Programming
    Replies: 3
    Last Post: 11-28-2007, 07:08 AM
  3. question about for loop
    By panfilero in forum C Programming
    Replies: 3
    Last Post: 09-27-2005, 05:59 AM
  4. loop question..
    By Balana in forum C Programming
    Replies: 4
    Last Post: 06-01-2005, 01:44 AM
  5. for loop question
    By neo505 in forum C Programming
    Replies: 6
    Last Post: 03-16-2003, 03:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21