Thread: loop problem

    #1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2002
    Posts
    12
    Rep Power
    0

    loop problem


    having a problem with this small c++ program im writing.

    i'm getting an error saying Lvalue required at the first while statement


    this program loops printing just the message until the user enter 'n' or 'N'. If the user enters a character other than 'n' , 'N' , 'y' , 'Y' the message invalid selection is entered and the prompt should be redisplayed

    can anyone please help?


    #include <iostream.h>
    # include <stdlib.h>
    char ch;
    void main()
    {
    while (ch=!'n' || ch=!'N')
    {
    cout << "Do you really want to run this program? (yes/no)\n ";
    cin >> ch ;

    if (ch!='y' || ch!='Y' || ch!='n' || ch!='N')
    {
    cout << "Invalid selection\n" ;
    }

    }

    if (ch=='n' || ch=='N')
    {
    exit(1);
    }

    }
  2. #2
  3. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    792
    Code:
    #include <iostream.h>
    
    int main() {
            char ch='\0';
    
            while (ch!='y' && ch!='Y') {
                    cout << "Do you really want to end this program (y/n)?";
                    cin >> ch;
    
                    if (ch!='Y' && ch!='y' && ch!='N' && ch!='n')
                            cout << "Invalid selection.\n";
            }
            cout << "End of program.\n";
            return(1);
    }
    The following code worked as expected with me.
  4. #3
  5. *bounce*
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Delft, The Netherlands
    Posts
    514
    Rep Power
    42
    while (ch=!'n' || ch=!'N')
    The not-equal operator is "spelled" as != instead of as =!. You seem to use it the right way further down in your code.
    "A poor programmer is he who blames his tools."
    http://analyser.oli.tudelft.nl/
  6. #4
  7. No Profile Picture
    Offensive Member
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2002
    Location
    in the perfect world
    Posts
    622
    Rep Power
    27
    while(toupper(ch)!='Y')//get rid of all lower case
    The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your f***ing mouth shut and hadn't asked any questions.

    Frank Zappa

IMN logo majestic logo threadwatch logo seochat tools logo