Thread: Trouble with a while loop

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    3

    Trouble with a while loop

    Hey there guys, I'm just learning C so I tend to throw in a bunch of extra stuff just to make sure I know how to use it correctly (so don't hound on me about using a variable's memory address instead of the actual variable heh) and I'm trying to set up this while loop using a bolean expression. Anyway this is the whole program:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int x; //defines x
        int *p;  //defines p as a pointer
        
        p = &x;  //sets the pointer the the memory address of variable x
        
        cout<<"Hey there! My name is Mr. Computer, how are you today?\n";
        cout<<"1. I'm great! :D\n";
        cout<<"2. I'm good.. :)\n";
        cout<<"3. Awful!     :(\n";
        cin>> x;
        cin.ignore();
        while (*p = 1 || *p = 2 || *p = 3) { //loop that will ensure a recognizable answer
              cout<< *p <<" is not a valid input, please input a number between 1-3.";
              cin>> x;
              }
              
        
        switch (*p) {  //Sets up a switch case for variable x based on it's address in memory being displaed as an intenger
        case 1:
             cout<<"Good ........!";
             break;
        case 2:
             cout<<"Cool";
             break;
        case 3:
             cout<<"Well I hope it gets better...";
             break;
        default:
                cout<<"Error\n";
                break;
                }
        cin.get();
        
    }
    Thing is, that it won't combile the while loop and I'm not sure why. I'm sure it's simple syntax I'm messing up but I'm stumped Thanks for taking the time to read and for any input you could provide!

    -Dr^ZigMan

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should change:
    *p = 1 || *p = 2 || *p = 3
    to
    *p != 1 && *p != 2 && *p != 3
    For one thing, you arent assigning integer literals to *p, and for another, you're checking if x is not equal to one of the valid values.

    Also, since you use
    cin>> x;
    cin.ignore();
    prior to that while loop, it makes sense to do the same within the loop.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    3

    and vs or

    Thanks for taking a peak at my little program

    First off, I see what your saying about cin.ignore(); my bad, It should have been there and now it is.

    As far as the and vs or goes I'm having a little trouble with that. In my mind || should have worked because if *p was equal to 1 2 or 3 then one part of the expression would have been true and the whole expression would evaluate to true would it not? i.e.

    (0 || 0 || 1) == 1 correct?

    as for the use of and, wouldn't that break down to..

    (0 && 0 && 1) == 0 false correct?

    I hate to be asking such a basic question with SO MUCH literature out there on the subject but I just need to have a few of the details clear in my head to fully understand them. Thanks again for your time, appreciate the help.
    -Dr^ZigMan

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Get rid of the *p for a moment, and think in terms of x.

    You want x to be set to either 1, 2 or 3.
    Until x is set to either 1, 2 or 3, you keep on looping and asking the user for input.
    i.e. to loop you must ask the question: is x not 1 or 2 or 3?
    Another way of phrasing it (which I used), is to ask: is x not 1, and not 2, and not 3?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2005
    Posts
    3
    Ohhhhhh, I see what your saying... I was thinking too much in terms of intergers, when I should be seeing it as true and false. Thank you for your help, much ablidged
    -Dr^ZigMan

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    EDIT:grr...double post
    Last edited by misplaced; 05-03-2005 at 04:19 AM.
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    EDIT: the next sentence is incorrect
    your orignal code would work fine if you used *p == n instead of *p = n...

    in c/c++, the single = assigns a value. if the value is not 0, it will ALWAYS return true (except perhaps in some cases of operator overloading). == compares 2 values, and returns 1 or 0 ( true or false ).

    effectively, in the statement if(x = 1), it says, assign 1 to x, then test if x is true.

    and please please please close your blocks under the keyword
    Code:
    if()
    {
    	switch()
    	{
    	}
    }
    Last edited by misplaced; 05-03-2005 at 04:22 AM.
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    your orignal code would work fine if you used *p == n instead of *p = n...
    It wouldnt, because the logic would be wrong.
    But it would compile, as far as I can tell.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    Quote Originally Posted by laserlight
    It wouldnt, because the logic would be wrong.
    But it would compile, as far as I can tell.

    that's why i added the "EDIT: the next line is incorrect"
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Rewriting a for loop as a while/do-while loop
    By Ashfury in forum C++ Programming
    Replies: 7
    Last Post: 04-27-2007, 02:20 PM
  2. loop the loop - feeling foolish
    By estos in forum C Programming
    Replies: 2
    Last Post: 04-07-2007, 02:45 AM
  3. loop needed also how to make input use letters
    By LoRdHSV1991 in forum C Programming
    Replies: 3
    Last Post: 01-13-2006, 05:39 AM
  4. trouble with a for loop with an if statement nested in
    By phoenix-47 in forum C++ Programming
    Replies: 4
    Last Post: 12-14-2005, 04:24 PM
  5. I need help as soon as possible.
    By hyrule in forum C++ Programming
    Replies: 7
    Last Post: 11-09-2005, 05:49 PM