30/09/2018, 17:05

Why the console printf 3 lines although I set conditional

#include <iostream>
using namespace std;
void main()
{
	
	char one, two;
	
	cout << "Nhap vu khi nguoi choi 1 "<< endl;
	cin >> one;
	cout << "Nhap vu khi nguoi choi 2 "<< endl;
	cin >> two;
	// dieu kien hoa nhau
	if (one == 'b' && two == 'b' || one == 'o' && two == 'o' || one == 'k' && two == 'k'){
		cout << "hoa nhau" << endl;
	}
	else{
		// dieu kien thang thua
		if (one == 'b' && two == 'o'){
			cout << "Nguoi cho thu 2 thang" << endl;
		}
		else {
			cout << "Nguoi choi thu 1 thang" << endl;
		}
		if (one == 'k' && two == 'b'){
			cout << "Nguoi cho thu 2 thang" << endl;
		}
		else {
			cout << "Nguoi choi thu 1 thang" << endl;
		}
		if (one == 'o' && two == 'k'){
			cout << "Nguoi cho thu 2 thang" << endl;
		}
		else {
			cout << "Nguoi choi thu 1 thang" << endl;
		}
	}
	system("pause");
}

Result of program:

Nhap vu khi nguoi choi 1
b
Nhap vu khi nguoi choi 2
k
Nguoi choi thu 1 thang
Nguoi choi thu 1 thang
Nguoi choi thu 1 thang
Press any key to continue . . 

Help me how to fix it, please. Some information about variables:

b = búa; o = bao; k = kéo;
Coulson viết 19:16 ngày 30/09/2018

if (one == ‘b’ && two == ‘o’){
cout << “Nguoi cho thu 2 thang” << endl;
}
else {
cout << “Nguoi choi thu 1 thang” << endl;
}
if (one == ‘k’ && two == ‘b’){
cout << “Nguoi cho thu 2 thang” << endl;
}
else {
cout << “Nguoi choi thu 1 thang” << endl;
}
if (one == ‘o’ && two == ‘k’){
cout << “Nguoi cho thu 2 thang” << endl;
}
else {
cout << “Nguoi choi thu 1 thang” << endl;
}

  • Because you use if … else … condition 3 time, so each time it will print a line even if the condition is true or false

  • To fix this, instead of using 3 if … else … , you should use if … else ladder like this:

	if (one == 'b')
	{
		if(two == 'k')
			cout << "Nguoi cho thu 1 thang" << endl;
		else
			cout << "Nguoi cho thu 2 thang" << endl;
	}
	else if (one == 'k')
	{
		if(two == 'o')
			cout << "Nguoi cho thu 1 thang" << endl;
		else
			cout << "Nguoi cho thu 2 thang" << endl;
	}
	else //if (one == 'o')
	{
		if(two == 'b')
			cout << "Nguoi cho thu 1 thang" << endl;
		else
			cout << "Nguoi cho thu 2 thang" << endl;
	}
Nguyễn Chí Cần viết 19:13 ngày 30/09/2018

Okay thanks, I have already understood my problem, thanks for giving me a solution

Coulson viết 19:14 ngày 30/09/2018

you are welcome, bro.

Mai Anh Dũng viết 19:13 ngày 30/09/2018

@EmCee I’ve just edited your post, you need to use

```

not

``

In order to Markup the code.

Nguyễn Chí Cần viết 19:07 ngày 30/09/2018

Thanks bro I’ll improve.

Quân viết 19:07 ngày 30/09/2018

if you use english to chat or discuss, Why you use vietnamese in your code?

Mai Anh Dũng viết 19:09 ngày 30/09/2018

Why you use vietnamese in your code?

Why not I find it’s ok hehe

Nguyễn Chí Cần viết 19:10 ngày 30/09/2018

I quên :D. In code, i forgot use English to set name for variables.

Nguyễn Chí Cần viết 19:11 ngày 30/09/2018

LOL, i like this hahaha

Bài liên quan
0