Im new at C++ and am trying to teach myself. I want to ask the user of the program why they are feeling a certain way, but not do anything with the sentece. So its like
-how are you feeling?-
user -good-
-why are you feeling good?-
user- blah blah blah-
-ok i understand now-

Recommended Answers

All 10 Replies

Well, unless you're going to add some artificial intelligence to this, simply put a load of predetermined questions / answers in and apply them to an if / switch statement.

i need to know the commands... i got the program all situated.. i just need to know what to do with that one sentence... i have if/while/switch and all that in there..

i got the program all situated.. i just need to know what to do with that one sentence... i have if/while/switch and all that in there..

Post the code. We can't comment on what we can't see.

keep in mind i just started :(, so its probably going to look retarded to most of you.. i wanted to see if i could create a program that would talk to me.. i would like to add more questions, just not yet..
the problem is (well the one i see) is that if the person types in more than one word for the reason, the program wont work..

// Conversation

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string feeling;
    char reason[10];
    string name;
    const int speed=70;
    int actualspeed;
    cout<< "\n\nWho am i talking to? :-) \t";
    cin>>name;
    cout<< "\nHey "<<name<< "! Finally!! Ive been waiting all day to talk to you. "<<endl;
    cout<< "So how are you feeling!?! :-D \n\n";
    cin>>feeling;
    cout<<"\nAnd why are you feeling "<<feeling<<"?\t";
    cin>>reason;
    cout<<"\nOk now I understand."<<endl;
    cout<<"\nDid you do some driving today? ";
    cout<< "\n1 - Yes";
    cout<< "\n2 - No";
    int answer;
    cout<<"\nChoice: ";
    cin>>answer;
    
    switch (answer)
    {
           case 1:
                    cout<< "\n What road did you drive most on "<<name<<"?\n\n";
                    cout<< "1 - Burnt Store Road\n";
                    cout<< "2 - 75\n";
                    cout<< "3 - 41\n";
                    cout<< "4 - Other";
                    
                    int choice;
                    cout<<"\n Choice: ";
                    cin>>choice;
                    
                    switch (choice)
                    {
                           case 1:
                                cout<<"\nYou better be careful with that road!";
                                break;
                           case 2:
                                cout<<"\nSoo.. What speed were you going? ";
                                cin>>actualspeed;
                                if (actualspeed>speed)
                                cout<<"\nYou really shouldnt be speeding.";
                                else if (actualspeed=speed)
                                cout<<"\nNot many people go the actual speed.";
                                else if(actualspeed<speed) 
                                cout<<"\nGoing below the speed limit can cause accidents!";
                                break;
                            case 3:
                                cout<<"I got a couple of warnings on that road. Make sure ";
                                cout<<"you are driving responsibly.";
                                break;
                           case 4:
                                cout<<"Hmm well i have no idea where you are. What do you think i am?";
                                cout<<" a GPS? Well i dont think so!";
                                break;
                           default:
                                   cout<<"You made an illegal choice.";
                    }
                break;
    case 2:
         cout<<"\nWell that stinks!\n";  
         break;
    default:
            cout<<"\n ERROR!";
    }                                                                                             
    return 0;
}

Post the code. We can't comment on what we can't see.

im new at programming and advice is well appreciated
thanks for the website on system("pause")

Yes, code tags and indentation/formatting. See previous link and below:

[code=cplusplus] // code here

[/code]

Here's your code. Much easier to read.

// Conversation

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string feeling;
    char reason[10];
    string name;
    const int speed = 70;
    int actualspeed;
    cout << "\n\nWho am i talking to? :-) \t";
    cin >> name;
    cout << "\nHey " << name << "! Finally!! Ive been waiting all day to talk to you. " << endl;
    cout << "So how are you feeling!?! :-D \n\n";
    cin >> feeling;
    cout << "\nAnd why are you feeling " << feeling << "?\t";
    cin >> reason;
    cout << "\nOk now I understand." << endl;
    cout << "\nDid you do some driving today? ";
    cout << "\n1 - Yes";
    cout << "\n2 - No";
    int answer;
    cout << "\nChoice: ";
    cin >> answer;

    switch (answer)
    {
        case 1:
            cout << "\n What road did you drive most on " << name << "?\n\n";
            cout << "1 - Burnt Store Road\n";
            cout << "2 - 75\n";
            cout << "3 - 41\n";
            cout << "4 - Other";

            int choice;
            cout << "\n Choice: ";
            cin >> choice;

            switch (choice)
            {
                case 1:
                    cout << "\nYou better be careful with that road!";
                    break;
                case 2:
                    cout << "\nSoo.. What speed were you going? ";
                    cin >> actualspeed;
                    if (actualspeed > speed)
                        cout << "\nYou really shouldnt be speeding.";
                    else if (actualspeed = speed)
                        cout << "\nNot many people go the actual speed.";
                    else if (actualspeed < speed)
                        cout << "\nGoing below the speed limit can cause accidents!";
                    break;
                case 3:
                    cout << "I got a couple of warnings on that road. Make sure ";
                    cout << "you are driving responsibly.";
                    break;
                case 4:
                    cout << "Hmm well i have no idea where you are. What do you think i am?";
                    cout << " a GPS? Well i dont think so!";
                    break;
                default:
                    cout << "You made an illegal choice.";
            }
            break;
        case 2:
            cout << "\nWell that stinks!\n";
            break;
        default:
            cout << "\n ERROR!";
    }
    return 0;
}

the problem is (well the one i see) is that if the person types in more than one word for the reason, the program wont work..

Not 100% sure what you mean. Are you worried about something like this?

Enter an integer: 2 3
Enter another integer:

Second question skipped due to the fact that there was an extra response to the first? Is that what you're talking about?

thanks.. sorry for before
i dont understand why getline wont work for my compiler.. i tried to use that as well and it just skipped over the command..

// Conversation

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string feeling;
    char reason[50];
    string name;
    const int speed=70;
    int actualspeed;
    
    cout<< "\n\nWho am i talking to? :-) \t";
    cin>>name;
    
    cout<< "\nHey "<<name<< "! Finally!! Ive been waiting all day to talk to you. "<<endl;
    cout<< "So how are you feeling!?! :-D \n\n";
    cin>>feeling;
    
    cout<<" And why is that?   ";
    cin.getline(reason,50); 
    cout<<"\nOk now I understand."<<endl;
    
    cout<<"\nDid you do some driving today? ";
    cout<< "\n1 - Yes";
    cout<< "\n2 - No";
    int answer;
    cout<<"\nChoice: ";
    cin>>answer;
    
    switch (answer)
    {
           case 1:
                    cout<< "\n What road did you drive most on "<<name<<"?\n\n";
                    cout<< "1 - Burnt Store Road\n";
                    cout<< "2 - 75\n";
                    cout<< "3 - 41\n";
                    cout<< "4 - Other";
                    
                    int choice;
                    cout<<"\n Choice: ";
                    cin>>choice;
                    
                    switch (choice)
                    {
                           case 1:
                                cout<<"\nYou better be careful with that road!\n";
                                break;
                           case 2:
                                cout<<"\nSoo.. What speed were you going? ";
                                cin>>actualspeed;
                                if (actualspeed>speed)
                                cout<<"\nYou really shouldnt be speeding.";
                                else if (actualspeed==speed)
                                cout<<"\nNot many people go the actual speed.";
                                else if (actualspeed<speed) 
                                cout<<"\nGoing below the speed limit can cause accidents!";
                                break;
                            case 3:
                                cout<<"I got a couple of warnings on that road. Make sure ";
                                cout<<"you are driving responsibly.\n";
                                break;
                           case 4:
                                cout<<"Hmm well i have no idea where you are. What do you think i am?";
                                cout<<" A GPS? Well i dont think so!\n";
                                break;
                           default:
                                   cout<<"You made an illegal choice.\n";
                    }
                break;
    case 2:
         cout<<"\nWell that stinks!\n";  
         break;
    default:
            cout<<"\n ERROR!";
    }                                                                                             
    return 0;
}

It's not that getline isn't working, it's that you're using cin & then using getline, what is happening is the \n delimiter used in cin, for example when you put that you're feeling good, you press enter, at that point \n is in the buffer.

When you use getline after cin then it reads the \n that is still in the buffer & skips it, that is what is happening here, to sort the problem, clear the input buffer with cin.ignore();

Alternatively, there is also a thread on the input buffer along with clearing it right here.

commented: Useful post :) +9

thanks everyone.. i know it wasnt that hard of a program probably for you that have been doing this, but it was difficult for me, and i appreciate everone's help.. here is the finished one (well the revised one).. test it out plz :-).. tell me what you think, be honest..
i might be stupid because it took me till 3 am to get the program completely working but i learned a lot of extra information that i can add to future programs :-D.. and now im going to go crash into my bed... thanks again

// Conversation

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string feeling;
    char reason[50];
    string name;
    const int speed=70;
    int actualspeed;
    
    cout<< "\n\nWho am i talking to? :-) \t";
    cin>>name;
    
    cout<< "\nHey "<<name<< "! Finally!! Ive been waiting all day to talk to you. \n"<<endl;
    cout<< "So how are you feeling!?! :-D   \n\n\n";
    cin>>feeling;
    
    cout<<"\nAnd why are you feeling "<<feeling<<"?\t";
    cin.ignore();
    cin.getline(reason,50); 
    cout<<"\nOk now I understand."<<endl;
    
    cout<<"\nDid you do some driving today? ";
    cout<< "\n1 - Yes";
    cout<< "\n2 - No";
    int answer;
    cout<<"\nChoice: ";
    cin>>answer;
    
    switch (answer)
    {
           case 1:
                    cout<< "\n What road did you drive most on "<<name<<"?\n\n";
                    cout<< "1 - Burnt Store Road\n";
                    cout<< "2 - 75\n";
                    cout<< "3 - 41\n";
                    cout<< "4 - Other";
                    
                    int choice;
                    cout<<"\n Choice: ";
                    cin>>choice;
                    
                    switch (choice)
                    {
                           case 1:
                                cout<<"\nYou better be careful with that road!\n";
                                break;
                           case 2:
                                cout<<"\nSoo.. What speed were you going? ";
                                cin>>actualspeed;
                                if (actualspeed>speed)
                                cout<<"\nYou really shouldnt be speeding.\n";
                                else if (actualspeed==speed)
                                cout<<"\nNot many people go the actual speed.";
                                else if (actualspeed<speed) 
                                cout<<"\nGoing below the speed limit can cause accidents!";
                                break;
                            case 3:
                                cout<<"I got a couple of warnings on that road. Make sure ";
                                cout<<"you are driving responsibly.\n";
                                break;
                           case 4:
                                cout<<"Hmm well i have no idea where you are. What do you think i am?";
                                cout<<" A GPS? Well i dont think so!\n";
                                break;
                           default:
                                   cout<<"You made an illegal choice.\n";
                    }
                break;
    case 2:
         cout<<"\nWell that stinks!\n";  
         break;
    default:
            cout<<"\n ERROR!";
    }                                                                                             
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.