Ah! im stuck and cant figure this out! Help! :)

Hi! Well im new to programming in general and have super basic knowledge so im pretty confused right now. You see whenever I run the code below, I can go through all of the questions normally until after the user inputs the answer to the "Ah i see, you have much to learn" cout. It suddenly skips all other user inputs after that and im not sure why. See what I want it to do is if you say yes to the "did you know you were special?" part and "Are you sure?" part then it would use the "If" "else" statement. But instead it skips all the through and ends the program. Ive tried taking out return 0; and other things and rewriting but it doesnt work. Super confused! Help? :) Thanks!

heres a link to what it looks liek when i run it.
http://imgur.com/aGVjULT

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

#include <iostream>

using namespace std;

int main()



{
string name;
string feel;
string answer;

int yes;
int no;

    cout << "How are you today?" << endl;

    cin >> feel;

    cout << "What is your name?" << endl;

    cin >> name;

    cout << "Ah I see. You have much to learn." << endl;

    cin >> answer;

    cout << "Well, you see, you are very special, did you know that? Type 1 for yes or 2 for no." << endl;

    cin >> yes;

    cout << "Are you sure? Type 1 for yes or 2 for no." << endl;

    cin >> yes;

    if(yes == yes) {

    cout << "Alright. Then lets get started " << name;

    }else {

    cout << "Well then I must be mistaken. Sorry";

    }




}



Here if(yes == yes)
i must be so if(yes == 1)
Also why do you insert yes two times?

1
2
3
4
5
6
7
cout << "Well, you see, you are very special, did you know that? Type 1 for yes or 2 for no." << endl;

    cin >> yes;

    cout << "Are you sure? Type 1 for yes or 2 for no." << endl;

    cin >> yes;
Last edited on
Because its the way i set it up for the program to make a decision. if 1=1 then its going to say okay lets get started if 1=2 then that means i must be mistaken. And I dont understand what you are trying to tell em today. Can you be more clear please?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <string>
using namespace std;

int main()
{
    cout << "How are you today?" << endl;
    string feel;
    cin >> feel;

    cout << "What is your name?" << endl;
    string name;
    cin >> name;

    cout << "Ah I see. You have much to learn." << endl;
    string answer;
    cin >> answer;

    cout << "Well, you see, you are very special, did you know that? Type 1 for yes or 2 for no." << endl;
    int first_answer ;
    cin >> first_answer;

    cout << "Are you sure? Type 1 for yes or 2 for no." << endl;
    int second_answer ;
    cin >> second_answer;

    const int yes = 1 ;
    // const int no = 2 ;

    if( first_answer == yes && second_answer == yes )
    {
       cout << "Alright. Then lets get started " << name;
    }
    else
    {
        cout << "Well then I must be mistaken. Sorry";
    }
}
@JLBorges It worked once then went back to doing what it did before. Is it possible its my compiler? im GNU compiler.

Edit: Okay nevermind I got it working. thank you @JLBorge. :)
Last edited on
Make sure when you give it user input you only use 1 word or number.

Right now if you were to type "Good Bob Yes 1 1" without the " 's when first prompted for input it would run through the entire program.

cin>>someString will grab all the text before some white space. It treats the next text after white space as the next input being supplied.

to get around this you can look into using getline(), or just be careful when giving the input.
Ohhhhhh okay so thats what i must have been doing. because when it would ask me something I would reply with "Im doing quite well." Lol. Okay so that makes sense now. thank you.
> when it would ask me something I would reply with "Im doing quite well."

To read in a complete line, use std::getline()
http://www.cplusplus.com/reference/string/string/getline/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <string>
using namespace std;

int main()
{
    cout << "How are you today?" << endl;
    string feel;
    std::getline( cin, feel ) ; // // type in: "Thank you, I'm fine," <enter>

    cout << "What is your name?" << endl;
    string name;
    std::getline( cin, name ); // type in: "My name is Bleh101" <enter>

    cout << "Ah I see. You have much to learn." << endl;
    string answer;
    std::getline( cin, answer ); // type in: "Yes, I know that." <enter>

    cout << "Well, you see, you are very special, did you know that? Type yes or no." << endl;
    string first_answer ;
    std::getline( cin, first_answer );

    cout << "Are you sure? Type yes or no." << endl;
    string second_answer ;
    std::getline( cin, second_answer ) ;

    const string yes = "yes" ;

    if( first_answer == yes && second_answer == yes )
    {
       cout << "Alright. Then lets get started " << name;
    }
    else
    {
        cout << "Well then I must be mistaken. Sorry";
    }
}
@JLBorge; Yeah I figured it out. i rewrote it last night and it came out perfect. Love these forums, sure ill be posting more on here as well! Thanks a lot:)
closed account (iAk3T05o)
You could also use if (yes = "yes") if you set up the program like that.
Is getline(cin, mystring) the same as std::getline(cin, mystring) . I know the latter.
Last edited on
Topic archived. No new replies allowed.