|| operator in while loop for C++

Discussion in 'Programming' started by ChristianEater, Dec 21, 2011.

  1. #1
    Is it not possible to use an "or" statement in a "while" loop? Take a look at this and tell me what I did wrong please.


    choice2:
    string choice2;
    cin >> choice2;
    while (choice2 !="north" and choice2 !="North" and choice2 !="NORTH" and choice2 !="N" and choice2 !="n")
    {
    while (choice2 ="south" or choice2 ="South" or choice2 ="SOUTH" or choice2 ="S" or choice2 ="s")
    {
    wrongway();
    cin >> choice2;
    }
    incorrect();
    cin >> choice2;
    }
    if (choice2=="north" || "North" || "NORTH" || "N" || "n")
    {
    system("cls");
    cout << endl << "A goblin is in front of you. " << endl << "The goblin attacks you. " << endl;
    cout << endl;
    } // end choice2 "north"
     
    ChristianEater, Dec 21, 2011 IP
  2. mmerlinn

    mmerlinn Prominent Member

    Messages:
    3,197
    Likes Received:
    819
    Best Answers:
    7
    Trophy Points:
    320
    #2
    We are not mind readers. Tell us what is happening. Tell us what results you want. Tell us what results you are getting. Tell us any error messages that you are getting.

    There should be much better ways of doing whatever you are doing WITHOUT using all of those ANDs and ORs and ||s.

    For example use only the first letter in choice2. Make that letter upper case. Then do something like:

    while (choice2 != "N")

    while (choice2 = "S")

    if (choice2 = "N")


    No more problems with ANDs, ORs, nor ||s.

    And no problems if someone types noorth or sout, instead of north and south.
     
    Last edited: Dec 22, 2011
    mmerlinn, Dec 22, 2011 IP
  3. mmerlinn

    mmerlinn Prominent Member

    Messages:
    3,197
    Likes Received:
    819
    Best Answers:
    7
    Trophy Points:
    320
    #3
    And after looking at your code, it appears that you have AT LEAST FOUR IDENTICAL functions to do the SAME EXACT thing, one for NORTH, one for SOUTH, one for WEST, and one for EAST. That increases your workload and your debugging time by a factor of four. I am sure you have much better things to do rather than spending your time doing the same thing over and over and over and over again. Combine all of the functions into ONE function and save your brain for more important things.
     
    mmerlinn, Dec 22, 2011 IP