Just started and NEED help!

When it comes to the part where you have yo answer yes or no, many different answers come after that instead of one. Could you explain what's wrong and how to fix it?

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
  #include <iostream>
#include <string>
using namespace std;
int main()

{
	cout<<"Hello! \n"
	<<"What is your name? \n";

	string name;
	cin>>name;

	cout<<"Nice to meet you, "<<name<<"! \n";
	cout<<name<<", are you a boy or a girl? \n";
	
	string sex;
	cin>>sex;

	cout<<"Oh, ok. \n";
	cout<<"Can I please ask you one more question? (Yes or No) \n";

	bool yn; //"yn" - user's answer Yes/No
	cin>>yn;
	
	int Yes;
		Yes = 1;

	int No;
		No = 0;

	int Maybe;
	    Maybe = 2;

	
	if(yn=1)
	{
		cout<<"Ok. How old are you, "<<name<<"? \n";
	}

	else if(yn=0);
	{
		cout<<"Oh come on! Please, only one question. \n";
	}

	if(yn=2)
	{
		cout<<"I'll ask. How old are you? \n";
	}

	else 
	{
		cout<<"Huh? Can I ask or not? \n";
	}

	bool age; bool answer;
	cin>>age, answer;

	if(age>0)
	{
		cout<<"That's cool! Thanks for telling me. \n";
	}

	else if(answer=1)
	{
		cout<<"So, how old are you? \n";
	}

	if(0)
	{
		cout<<"Fine then, I am not going to ask. \n";
	}

	


	system("pause");
	return 0;
}
Last edited on
First of all you need to know that

= is assignment
== is comparison (note two =)

This means that line 35/40/45... you assign the values.

if checks only whether a value is different from 0, hence line 35 will always be true.

Next thing is the wrongfull ; on line 40. This ends the if clause and everything after (line 41...) will be executed regardless.
bool yn; //"yn" - user's answer Yes/No
A bool object can be in one of two states. TRUE or FALSE. You cannot store a string in it, so this:
cin>>yn;
is impossible. You cannot store a string in a bool. Do not try to store a string in a bool.
A bool object can be in one of two states. TRUE or FALSE. You cannot store a string in it, so this:
cin>>yn;
is impossible.

Not true. You can extract a bool from a stream. Legal values in the stream are 0 and 1.
http://www.cplusplus.com/reference/istream/istream/operator%3E%3E/
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

int main()
{
    bool b;
    if (std::cin >> b) {
        std::cout << b << '\n';
    } else {
        std::cout << "stream is bad\n";
    }
}

dhayden@DEV-47JGKX1 ~/tmp
$ ./foo
0
0

dhayden@DEV-47JGKX1 ~/tmp
$ ./foo
1
1

dhayden@DEV-47JGKX1 ~/tmp
$ ./foo
true
stream is bad
Dimm, everything dhayden just said is true but it's not something you need to do anything about so don't worry about it. Do not try to store a string in a bool.
Last edited on
Line 56 has another problem. The comma operator doesn't work the way you think it does (age is evaluate/dismissed only answer is use for the stream).

Change line 56:

cin>>age >> answer; // Note the second >>
Topic archived. No new replies allowed.