multi if staments

it out puts the else instead of else when i do "good" and "fine" the explanation is under //

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
#include <iostream>
#include <string>

using namespace std;

int main()
{
string ans, name;
		
		cout << "Hello what is your name" << endl;
		cin >> name;
		cout << "Nice to meet your " << name << endl;
		cout << "How is your day " << name << endl;
		cin >> ans;
// if (ans.eguals("good"))
	if (ans == "good")
		{
			cout << "Thats good, enjoy the rest of your day" << endl;
		}
// if (ans.equals("fine"))
	if (ans == "fine")
		{
			cout << "Just fine not good" << endl;
		}
// if (ans.equals("bad"))
	if (ans == "bad")
		{
			cout << "Thats too bad, i hope you have a better day tomarrow" << endl;
		}
//if (ans.equals("great"))
	if (ans == "great")
		{
			cout << "thats awesome to hear" << endl;
		}
//if (ans.equals("terible"))
	if (ans == "terible")
	{
		cout << "thats terible to here, im sorry" << endl;
	}
	else
	{
		cout << "sorry thats none of the choices try good, fine, bad, great, or terible, thank you" << endl;
	}
return 0;
}
Last edited on
problem in last else, because this else is only for last if not for all.thats why else will execute always when last if condition will fails.

Sol :- You should use else if instead of if.

see this:-

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
int main(int argc, _TCHAR* argv[])
{

string ans, name;
		
		cout << "Hello what is your name" << endl;
		cin >> name;
		cout << "Nice to meet your " << name << endl;
		cout << "How is your day " << name << endl;
		cin >> ans;

	if (ans == "good")
		{
			cout << "Thats good, enjoy the rest of your day" << endl;
		}

	else if (ans == "fine")
		{
			cout << "Just fine not good" << endl;
		}
	else if (ans == "bad")
		{
			cout << "Thats too bad, i hope you have a better day tomarrow" << endl;
		}
	else if (ans == "great")
		{
			cout << "thats awesome to hear" << endl;
		}
	else if (ans == "terible")
	{
		cout << "thats terible to here, im sorry" << endl;
	}
	else
	{
		cout << "sorry thats none of the choices try good, fine, bad, great, or terible, thank you" << endl;
	}

	getch();
	return 0;
}

Topic archived. No new replies allowed.