Thread: y or n option?

  1. #1

    y or n option?

    Could some one give me a yes or no example. But with y and n. Also could you add something to make it where case doesn't matter (if you want to)? Something like-

    Code:
    cout << "Again?: ";
    cin >> again;
    if again = "y"
         cout << "\nYou said yes.";
    if again = "n"
         cout << "\nYou said no.";
    else
         cout << "\nNot a valid option.";
    I appologize as I do not know the usage of a if statement in C++.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Code:
    #include <iostream>
    #include <cctype>
    
    using std::cin;
    using std::cout;
    
    int main()
    {
      char yn;
    
      cout<<"Again? (y/n): ";
      cin>> yn;
    
      yn = tolower ( yn );
    
      if ( yn == 'y' )
        cout<<"You said yes!"<<std::endl;
      else if ( yn == 'n' )
        cout<<"Party pooper :("<<std::endl;
      else
        cout<<"I said \'y\' or \'n\' silly."<<std::endl;
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    Thank you so much for that, but I have two questions. What is tolower? And in your if statement, why do you use std::endl instead of just using std::endl at the top?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >What is tolower?
    It's a standard library function that converts a char to its lower case equivalent or does nothing if there is no lower case equivalent. It's located in the cctype header.

    >why do you use std::endl instead of just using std::endl at the top?
    I forgot, adding the using statements was a last minute decision.

    -Prelude
    My best code is written with the delete key.

  5. #5
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    tolower returns the letter argument in lowercase, regardless of it's case going in.

    She does this because ('y' == 'Y') is false in C++.

    As for not adding a using directive for std::endl, I can only tell you that it must be correct, since prelude is perfect, but I don't have a clue as to her reasoning.

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    for std stuff, you could do any of the following

    Code:
    using namespace std;
    OR
    using std::endl;
    OR
    //like what prelude did
    std::cout << std::endl;
    all of them work, it's up to the user as to how he or she wants to do it.

    I now learned about the tolower fxn...I probably would have done a switch, like this:

    Code:
    switch(yn) {
        case 'y':
        case 'Y':
            cout << "You said yes" << endl;
            break;
        case 'n':
        case 'N':
            cout << "You said no" << endl;
            break;
        default:
            cout << "Invalid letter." << endl;
            break;
    }
    but the tolower is easier.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >all of them work, it's up to the user as to how he or she wants to do it.
    There are distinct differences though. I prefer explicit std:: resolution most of the time because the notational convenience of using statements really isn't worth cluttering the global namespace. I never use

    using namespace std;

    because it's like saying "I'll only use cout and endl, but give me everything just in case". That's poor practice even though I know better than to use a standard name for something I write.

    -Prelude
    My best code is written with the delete key.

  8. #8
    Originally posted by Imperito
    She does this because ('y' == 'Y') is false in C++.
    Yea, you could have just said C++ is case sensitive. Or if you looked at my post and see where I said

    add something to make it where case doesn't matter
    and that gives you a little clue that i already knew C++ is case sensitive. But thanks for that anyways.

  9. #9
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    well ii use something like this.. as long as the user enters 'y as the option the program keeps running;;;;



    Code:
    # include <iostream.h>
    
    int main()
    {
    char option='y';
    
    while(option=='y')
           {
    
              // the program you want to run....
               
              cout<<"Do you want to run the prog again (y/n) > ";
              cin>>option;
    
    if(option!=n && option!=y)
    option='y';
    
           }
    
    
    return 0;
    }

  10. #10
    Why not step it up a bit:

    Code:
    #include <iostream>
    #include <conio>
    #include <cctype>
    int main()
    {
     cout<<Again (y/n): ";
     char yn = getch();
     yn = tolower ( yn );
     if(yn == 'n')
        cout<<"Loser";
     if(yn == 'y')
        cout<<"DUDE!!!";
     else
        return 0;
    }
    Dev C++
    Win XP/2k/98

    I DO NOT TAKE CLASSES I DONT GET HOMEWORK THIS IS NOT A HOMEWORK QUESTION!!!

    He's lean he's keen... He's the spank machine!

  11. #11
    Registered User
    Join Date
    Dec 2002
    Posts
    103

    getch() is a non-standard function

    Hi,

    getch() is a non-standard function.... hence its preferred that you don't use getch() in your programs.. unless you explicitly work with Turbo C / C++ and are stuck to DOS or windows platform.
    Have a wonderful day.... and keep smiling... you look terrific that way
    signing off...
    shiv... as i know him

  12. #12
    Originally posted by devour89
    Why not step it up a bit:

    Code:
    #include <iostream>
    #include <conio>
    #include <cctype>
    int main()
    {
     cout<<Again (y/n): ";
     char yn = getch();
     yn = tolower ( yn );
     if(yn == 'n')
        cout<<"Loser";
     if(yn == 'y')
        cout<<"DUDE!!!";
     else
        return 0;
    }
    On line 6 you have no opening quotation mark. Just thought I would say something.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. Replies: 3
    Last Post: 12-06-2008, 07:54 PM
  3. For some reason I end up with memory leak...
    By RaDeuX in forum C Programming
    Replies: 10
    Last Post: 11-26-2008, 12:43 AM
  4. Binary Search Tree
    By penance in forum C Programming
    Replies: 4
    Last Post: 08-05-2005, 05:35 PM
  5. problem passing an option as command line argument
    By papous in forum C Programming
    Replies: 3
    Last Post: 11-22-2001, 06:12 PM