Why wont this character comparison work?

This is a discussion on Why wont this character comparison work? within the C++ Programming forums, part of the General Programming Boards category; When I enter in n or y it freezes the program. I am wanting it to recognize Y or N... ...

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

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