User prompt question

This is a discussion on User prompt question within the C++ Programming forums, part of the General Programming Boards category; I'm trying to prompt for Y,y,Yes,yes - and I need help. Here is what definitly doesn't work: Code: #include <iostream> ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    5

    User prompt question

    I'm trying to prompt for Y,y,Yes,yes - and I need help.

    Here is what definitly doesn't work:

    Code:
    #include <iostream>
    
    using std::cout;
    using std::cin;
    
    
    int main()
    {
       
    char c = ' ';
    
       while( c != 'y' || c != 'Y' || c != 'Yes' || c != 'yes' )
       {
          cout << "Continue? (Y/N): ";	
          cin.get(c);                           // Get character from user
          cin.ignore(1, '\n');	// Skip newline
    
          if( c == 'y' || c == 'Y' || c == 'yes' || c == 'Yes')
             cout << " you entered yes";
          else 
    		  cout << " you entered no";
    	  }
    	  
    	  return 0;
             
    }
    Thanks

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    This shows how to test for lower and upper case at the same time.

    Down side - it accepts any word beginning with 'y' as being yes, so it needs a little refinement in that area.

    Code:
    #include <iostream>
    #include <cctype>
    using namespace std;
    
    int main() {
        char buff[100];
        cin.getline(buff,sizeof buff);
        if ( toupper(buff[0]) == 'Y' ) {
            cout << "yes" << endl;
        }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Why not just use getline to read the entire string and then simply test the first character?
    Code:
    #include <cctype>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    bool keep_going()
    {
      string reply;
    
      if ( getline ( cin, reply ) && tolower ( (unsigned char)reply[0] ) == 'y' ) {
        return true;
      }
      return false;
    }
    
    int main()
    {
      while ( keep_going() ) {
        cout<<"Still going\n";
      }
    }
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    5
    would either of these work if the user entered: Ynoblaablaablaa?

    They should only "continue" if the user entered 1 of 4 things:
    Yes,yes,Y,y

    Thanks again

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Code:
    string test = "y";
     
    while(test == "y" ||
    		test == "Y" ||
    		test == "Yes" ||
    		test == "yes")
    {
    //do something
    cout << "to continue enter y, Y, Yes, or yes" << endl;
    cin >> test;
    }

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >would either of these work if the user entered: Ynoblaablaablaa?
    Yes, with minor changes:
    Code:
    bool keep_going()
    {
      string reply;
    
      if ( getline ( cin, reply ) ) {
        if ( reply == "y" || reply == "Y" || reply == "yes" || reply == "Yes" ) {
          return true;
        }
      }
      return false;
    }
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Apr 2004
    Posts
    5
    Thank you guys for your time - I think either of these will work great, and while waiting, I actually came up with a solution of my own that is similar-

    Hopefully one day I'll be the one replying with solutions instead of posting all the questions...

    Code:
    cout << "enter response";
    cin >> response;
    
    string a = response;
    
    if (a == "yes" || a == "Yes" || a == "y" || a == "Y")
    cout << "you entered yes";
    
    else cout << "you entered no";

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. prompt user for number of loops
    By wonderpoop in forum C Programming
    Replies: 26
    Last Post: 11-15-2006, 05:58 AM
  2. Structure and Linked List User Input Question
    By kevndale79 in forum C Programming
    Replies: 16
    Last Post: 10-05-2006, 11:09 AM
  3. I need help to compile this code...
    By wise_ron in forum C Programming
    Replies: 17
    Last Post: 05-07-2006, 12:22 PM
  4. Yet another free() question: user breakpoint called
    By ArchitectureinH in forum C Programming
    Replies: 3
    Last Post: 07-20-2005, 10:47 AM
  5. Question about arrays?? PLease read..
    By foofoo in forum C Programming
    Replies: 3
    Last Post: 06-24-2002, 02:40 PM

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