entering 0s and negative values into while

This is a discussion on entering 0s and negative values into while within the C++ Programming forums, part of the General Programming Boards category; Im working on a robot project for my intro to computing science class and I'm stuck in quite a sticky ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    22

    entering 0s and negative values into while

    Im working on a robot project for my intro to computing science class and I'm stuck in quite a sticky situation. The robot reads only values from 1 to 4 to move north, east, south, west, 1,2,3,4 respectively. 1 = north, 2 = south...
    But the catch here is if I enter a 0 or negative number then my program should read "end of journey" how do I actually implement that into my statement?

    this is what I have so far,
    Code:
    while ( direction > 0)
              {//opens bracket for the loop
                     if (direction == NORTH)
                           {
                           cout << "Move north\n";
                           }
                     else if (direction == EAST)
                            {
                             cout << "Move east\n";
                            }
                     else if (direction == SOUTH)
                            {
                             cout << "Move south\n";
                            }
                     else if (direction == WEST)
                            {
                             cout << "Move west\n";
    
                            }
                     else
                            {
                             cout << "Invalid positive input for direction\n";
                            }
                             cout << "Enter direction values again\n";
                             cin >> direction;
    what am I doing wrong? Please and thank you, anything is appreciated.
    Last edited by Salem; 04-14-2010 at 11:49 PM. Reason: Added [code][/code] tags, learn to use them yourself

  2. #2
    Registered User
    Join Date
    Apr 2010
    Posts
    22
    before the direction is actually checked, I have also writted
    cout << "Enter direction your robot wishes to move, 1 for north, 2 for south, 3 for east, and 4 for west\n";
    cin >> direction;
    and then the while loop begins.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    A 0 or negative dumps you out of the loop. So when you get out of the loop, there you are.

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    22
    yes, if I enter a 0 or a negative I'm out of the loop, but then the program just ends there, so where can I actually write the code to report to the user "end of journey", without the loop asking asking me "enter the direction values again"?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So don't print "Enter again", print "End of journey".

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    22
    I think I fixed the error now, i just wrote cout << "End of journey\n";
    after the closing bracket for the while loop, so now when I enter a few valid values, and enter a neg or zero it works.

Popular pages Recent additions subscribe to a feed

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