making a yes and no responce question

This is a discussion on making a yes and no responce question within the C++ Programming forums, part of the General Programming Boards category; I'm trying to make an program that starts a convo and asks questions by reacting to what the user inputs. ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    18

    need help with spaces and punction marks

    I'm trying to make an program that starts a convo and asks questions by reacting to what the user inputs. I'm only planning on using yes and no questions. I know how to make the conversation, I just don't know how to make it so you can inout a yes or no and have it respond differently between the two. I know I should use the if statements and completely understand that but something just isnt clicking with me when it comes to putting in the yes and no. Please help.
    Last edited by Sebastion; 03-14-2006 at 11:20 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,497
    std::string answer;

    cin >> answer;

    if ( answer == "yes" )
    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
    Registered User
    Join Date
    Mar 2006
    Posts
    18
    That's what does it? ....yeah the quotes would explain why it wasnt working. thanks, sorry i just woke up.

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    18

    help with spaces and punction marks?

    Ok i got the yes and no working. So i put a esle if statement saying that if someone input something other than yes or no that it would say something else but if they something something with a space it wont work and shuts down the program. SO, how do enable it to accept an answer with a space (and punction marks) in it?
    Last edited by Sebastion; 03-14-2006 at 11:19 AM.

  5. #5
    Registered User Aalmaron's Avatar
    Join Date
    Jan 2004
    Location
    In front of a monitor
    Posts
    48
    a string can get more than just one word, you could use getline, you can also fin substrings within it. here...

    Code:
    string answer;
    string yes;
    string no;
    string special;
    
    <set those strings = to what you want here>
    
    getline(answer);
    
    if (answer.find(yes, 0) != string::npos) // this checks to see if the contents of the yes tring are contained in the answer string
    	cout<<.... // this  is your responce if so
    else if (answer.find(no, 0) != string::npos) // if not...
            blah blah
    else if(... // etc etc

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    25
    Code:
    if ( answer == "yes" )
    {
    cout << "Yes";
    }
    
    else if( answer == "no" )
    {
    cout << "No";
    }
    
    else
    {
    cout << "User typed something other than Yes or No.";
    }
    // I think that's what you were trying to do, if not please explain a little more.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Using getline instead of operator>> should be the only change you need to make. The syntax is:

    std::getline(std::cin, answer);

    If you want to accept "yes." with the period as a valid answer, then you'd need something more complicated like using the find method.

    Note that if you use operator>> earlier in your program, you might have trouble with getline because of a newline left in the stream. Make sure to call cin.ignore() after a call to cin >> and before a call to getline.
    Last edited by Daved; 03-14-2006 at 11:47 AM.

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    18
    This is what my program looks like.


    Code:
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    string answer;
    
    int main()
    
    {
        char firstname[25];
        char lastname[25];
        char fullname[50];
        int age;
        
        cout<<"Demetri : Hey, I'm Demetri.\n";
        cout<<"Demetri : What's your name? \n";
        cout<<"User : ";
        cin.getline ( firstname, 25 );
        cout<<"Demetri : What's your last name, "<< firstname <<"?\n";
        cout<< firstname <<" : ";
        cin.getline ( lastname, 25 );
        fullname[0] = '\0';
        strcat ( fullname, firstname );
        strcat ( fullname, " " );
        strcat ( fullname, lastname );
        cout<<"Demetri : "<< fullname <<"?\n";
        cout<<"Demetri : Are you related to Jamie "<< lastname <<"?\n";
        cout<< firstname <<" : ";
        cin>> answer;
        cin.ignore();
        
        if (answer == "Yes" || answer == "yes") {
        cout<<"Demetri : Tell her I said Hi next time you talk to her.\n";
        }
        else if (answer == "No" || answer == "no") {
        cout<<"Demetri : Oh, nevermind then.\n";
        }
        else {
             cout<<"Demetri : You should find out if you are or not.\n";
             cout<<"Demetri : Jamie and I are really good friends.\n";
             }
        cout<<"Demetri : Anyways, How old are you, "<< firstname <<"?\n";
        cout<< firstname <<" : ";
        cin>> age;
        cin.ignore();
        
        if (age <= 25) {
        cout<<"Demetri : You're pretty young!\n";
                        }
        else if (age >= 26 && age <= 50) {
        cout<<"Demetri : I remember those days.\n";
        }
        else if (age >= 51 && age <= 75) {
        cout<<"Demetri : You're getting old.\n";
        }
        else if (age >= 76 && age <= 100) {
        cout<<"Demetri : You're really old!\n";
        }
        else if (age >= 101 && age <= 125) {
        cout<<"Demetri : Why are you still alive>\n";
        }
    
        cin.get();
    }

  9. #9
    Registered User
    Join Date
    Mar 2006
    Posts
    18
    I just started learning like 2 days ago and havent made it past if statements because I've been messing around with this too learn and familirize my self with the material

  10. #10
    Registered User
    Join Date
    Mar 2006
    Posts
    25
    Code:
    else if (age >= 26 && age <= 50) {
        cout<<"Demetri : I remember those days.\n";
    lol those days? How old are you actually lol.

  11. #11
    Registered User
    Join Date
    Mar 2006
    Posts
    18
    Quote Originally Posted by MidnightlyCoder
    Code:
    else if (age >= 26 && age <= 50) {
        cout<<"Demetri : I remember those days.\n";
    lol those days? How old are you actually lol.
    haha I'm not demetri. thats just a character
    im 19 and my name is Sebastion

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    In C++, the string class is better for strings than character arrays (C style strings) that you are using. So, change <cstring> to <string>, change char firstname[25]; to string firstname; and the same for the other variables, change cin.getline(firstname, 25) to getline(cin, firstname) and the same for the other variables, and change cin >> answer to getline(cin, answer) so you can read in the whole line looking for yes or no.

    One of the benefits of using string is that you don't have to specify a size, so if the person's first name is more than 25 characters, it will still work. Another benefit is that it is easier to concatenate the strings, so instead of all those calls to strcat, all you need is fullname = firstname + " " + lastname.

  13. #13
    Registered User Aalmaron's Avatar
    Join Date
    Jan 2004
    Location
    In front of a monitor
    Posts
    48
    instead of using getline(cin, answer) like if being sugested, you could just use getline(answer) (for the whole line, which works the way i recommended), or you can use getline(answer, " "), and that will make it wait for a space to cancel the getline.

    also, you should make a <125 thing for age, its not good practice to leave something unchecked. currently if they are over 125 years old, then your program does nothing.

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> instead of using getline(cin, answer) like if being sugested, you could just use getline(answer) (for the whole line, which works the way i recommended).

    Actually, getline(answer) and getline(answer, " ") are both compile errors.

    getline(cin, answer) is how you get an entire line into a string. There is also a form of getline that works with C style strings, but it requires different arguments as well. Also, getline takes an optional character (that is the newline by default), so if you want it to stop at a space you would use a character literal in single quotes: getline(cin, answer, ' ').

Popular pages Recent additions subscribe to a feed

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