Thread: Why wont this character comparison work?

  1. #1
    Unregistered
    Guest

    Why wont this character comparison work?

    When I enter in n or y it freezes the program. I am wanting it to recognize Y or N...

    cout << "Are these stats acceptable? (Y/N)"<<endl;
    gets(yesno);
    for( int i = 0; i < 3; ++i ) toupper( yesno[i] );
    if (strchr(yesno,'Y')==0)
    {
    cout << "You are now ready to enter the game. Entering game now..."<<endl;
    //enter the game.
    };
    if (strchr(yesno,'N')==0)
    {
    cout << "You are very hard to please. Very well. We will roll again."<<endl;

  2. #2
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    Don't use gets().

    This should work:
    Code:
    char yesno;
    
    cout << "Acceptable? << endl << ":> ";
    yesno = cin.get();
    
    if( yesno == 'y' || yesno == 'Y' )
       cout << "Then we will proceed.." << endl;
    else
       cout << "Then we will re-roll.." << endl;
    Try it out.

    EDIT/PS:: Use code tags next time

  3. #3
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    or just

    cin >> yesno;

    where yesno is type char...
    Blue

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  3. about wide character and multiple byte character
    By George2 in forum C Programming
    Replies: 3
    Last Post: 05-22-2006, 08:11 PM
  4. pipe
    By smart girl in forum C Programming
    Replies: 4
    Last Post: 04-30-2006, 09:17 AM
  5. character occurrence program not working
    By Nutshell in forum C Programming
    Replies: 6
    Last Post: 01-21-2002, 10:31 PM