if statement in while loop

This is a discussion on if statement in while loop within the C++ Programming forums, part of the General Programming Boards category; Hello, i am wondering if there is a better way to have this same thing happen with out butchering this ...

  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,475
    Maybe this:

    Code:
    if (( race != "human" ) || ( race != "elf" )) // I refer to C++ strings here, not C style ones
    {
       cout << "You fail!" << endl;
    }
    I'm just trying to be a better person - My Name Is Earl

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,487
    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.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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,796
    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,475
    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
    I'm just trying to be a better person - My Name Is Earl

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, 01:13 AM
  2. infinite while loop inside a switch statement.
    By tummala_005 in forum C Programming
    Replies: 6
    Last Post: 12-15-2008, 04: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

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