Thread: if statement in while loop

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    4

    Question if statement in while loop

    Hello, i am wondering if there is a better way to have this same thing happen with out butchering this code:

    Code:
       
            //Race
       cout << "Race choices:  human, elf\n";
       cout << "Race: \n";
       cin >> race;
       cin.ignore(100, '\n');
       
       bool boolRace = 0;
       while((strcmp(race, "human")!=0)&&(boolRace == 0))
        {      
           if (strcmp(race, "elf")!=0)
           {
               cout << "YOU FAIL! \n...Race: \n";
               cin >> race;
               cin.ignore(100, '\n');
           }
           else
           {
               boolRace = 1;
           }
        }   
      cout << "Name:  " <<  charName <<  "\n" 
           << "Race:  "  <<  race << "\n";
    Im refering to the "if statement" inside the while loop.
    thanks
    -Scott

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Maybe this:

    Code:
    if (( race != "human" ) || ( race != "elf" )) // I refer to C++ strings here, not C style ones
    {
       cout << "You fail!" << endl;
    }
    Double Helix STL

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Yes,

    Use std::string instead

    Also, reevaluate how you are testing things. In plain english it goes more or less like this:

    While race is not 'human' and boolRace is 0, if race is not elf, request for new race, else boolrace becomes 1.

    I'm almost sure that is not what you want to do. If it doesn't work in plain english, it won't work in the code.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    A better way is saying what you mean rather than that funky logic:
    Code:
    bool is_valid = false;
    
    cout << "Race choices:  human, elf\n";
    cout << "Race: ";
    
    while ( !is_valid ) {
      if ( !cin.getline ( race, sizeof race ) )
        break;
    
      if ( strcmp ( race, "human" ) == 0
        || strcmp ( race, "elf" ) == 0 )
      {
        is_valid = true;
      }
      else
        cout<<"Invalid race. Please try again: ";
    }
    My best code is written with the delete key.

  5. #5
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Read the statement in your head, like Mario said, it does not make sense ( your theroy ).
    It should say to you:

    "if race is not equal to human OR race is not equal human, then print YOU FAIL"

    Always read a complex statement back in your head to be sure it makes sense
    Double Helix STL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Visual Studio Express / Windows SDK?
    By cyberfish in forum C++ Programming
    Replies: 23
    Last Post: 01-22-2009, 02:13 AM
  2. infinite while loop inside a switch statement.
    By tummala_005 in forum C Programming
    Replies: 6
    Last Post: 12-15-2008, 05:46 PM
  3. Switch statement = infinite loop
    By Lucid003 in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2005, 12:46 AM
  4. for loop or while loop
    By slamit93 in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2002, 04:13 AM
  5. explain this loop statement?
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 05-05-2002, 02:46 AM