If statement checking chars

Jan 11, 2012 at 12:59am
Hey all, Im having a little trouble with this program. I'm trying to get my if statements to check 2 different char inputs, but for some reason It just displays the content of the first if statement. I built this from scratch, and I dont know if Im going about this the wrong way or if Im even going in the right direction. Any help would be appreciated.

#include <iostream>

using namespace std;

int main()
{
char p, q;

cout <<"Enter T or F for p='A is truthful': " ;
cin >> p;

cout <<"Enter T or F for q='B is truthful': " ;
cin >> q;

if((p == 'T' || 't') && (q == 'T' || 't'))
{
cout <<"A says 'We are both telling the truth' is TRUE" << endl;
cout <<"B says 'A is lying' is FALSE" << endl;
system("PAUSE");
return 0;
}

if((p == 'F' || 'f') && (q == 'T' || 't'))
{
cout <<"A says 'We are both telling the truth' is FALSE" << endl;
cout <<"B says 'A is lying' is TRUE" << endl;
system("PAUSE");
return 0;
}
if((p == 'T' || 't') && (q == 'F' || 'f'))
{
cout <<"A says 'We are both telling the truth' is FALSE" << endl;
cout <<"B says 'A is lying' is FALSE" << endl;
system("PAUSE");
return 0;
}

if(p == 'F' && q == 'F')
{
cout <<"A says 'We are both telling the truth' is FALSE" << endl;
cout <<"B says 'A is lying' is TRUE" << endl;
system("PAUSE");
return 0;
}

else
{
cout <<"Please use only 'T' or 'F' next time." << endl;
system("PAUSE");
return 0;
}

return 0;
}
Jan 11, 2012 at 1:09am
closed account (zb0S216C)
oiramih wrote:
(p == 'T' || 't') (sic)

See the 't' condition? That's your problem. Allow me to elaborate.

The bool type has 2 possible values: true, and false. When in an if statement, each condition is tested for true or false (a proposition. A proposition is a statement that is either true of false). If the condition evaluates to a value greater than or less than 0, the condition is true; otherwise, it's false.

Like all conditions, 't' is implicitly converted to a bool by the compiler. The numerical value of 't' is greater than 0 (In C++, a single character is a numerical type in disguise. The true value of each character can be found by looking at ASCII table (look for the octal equivalent value of the corresponding character)); thus, the condition is true; always.

Altered a spelling mistake.

Wazzak
Last edited on Jan 11, 2012 at 3:57pm
Jan 11, 2012 at 2:23am
I see, so then would I have to take the users input and convert it into an int in order to get the if statements to properly check? If so, would you mind giving me an example?
Jan 11, 2012 at 4:38am
No, he's saying that you have to individually check both letters. All a boolean has to be to return true is be a NON ZERO number, which 't' obviously is. So the statement if (p == 'T' || 't') is equivalent to if (p == 'T' || true). Change it to if (p == 'T' || p == 't') and you should be fine. Although I didn't even read your code, only Frameworks response, so there may be more problems. Also, there's no point in checking both in the first place as you can simply do if (tolower(p) == 't') which will work the same way.
Last edited on Jan 11, 2012 at 4:40am
Jan 11, 2012 at 5:07am
Ok so I avoided the use of 't' in my if statements, and used only 'T' and 'F'. The problem now is everytime I input, it jumps straight to the else statement. How can I get this to check for user inputs in the if statements, if it can't read char? Am I doing something wrong?
Jan 11, 2012 at 5:31am
oiramih, I think you aren't fully understanding what Framework and codingshark, are explaining. The checking would be more like this. if((p == 'T' ) && (q == 'T')) Changing the 'T''s and F's in each if statement for the p and q variable checking. If you're still having problems, post the code as you have now, so that we can better help you.
Jan 11, 2012 at 5:40am
whitenite1, that was it! thank you for the help. The problem was I didn't have the if(p == 'T' && q == 'T') > if((p == 'T' ) && (q == 'T')) inner parenthesis. After all the changing I guess I forgot to put them in, and thought the program wasn't reading. Thanks for clearing that up, I can't believe it was something that small.
Jan 11, 2012 at 5:46am
Your code is incredibly confusing the way it's written. You also don't do a very efficient job at comparing input values. Don't take that the wrong way as it's clear that you a new and trying to learn. With that in mind, I decided to just write a little program that does this for you using the proper method. By the way that is the most CONFUSING program to teach someone how to work with input, if statements and booleans, so if you got that from a book then PLEASE get a new book.

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

void main()
{
	// You should always initialize variables just to be safe
	char szPlayerOne = NULL;
	char szPlayerTwo = NULL;

	// Let's get input from the first player
	std::cout << "Player one, are you calling player two a liar [T/F]: ";
	std::cin >> szPlayerOne;

	// Let's get input from the second player
	std::cout << "Player two, are you calling player one a liar [T/F]: ";
	std::cin >> szPlayerTwo;

	// Let's check if both values are the same
	bool bBoth = szPlayerOne == szPlayerTwo;

	// Go ahead and print this if both players entered the same input
	if ( bBoth )
		if ( tolower( szPlayerOne ) == 't' ) // Force lower case to make it more simple
			std::cout << "You are both calling each other liars" << std::endl;
		else
			std::cout << "You are both agreeing that you're telling the truth" << std::endl;


	// Switch statements look cleaner than if else  blocks
	switch ( szPlayerOne )
	{
	// We haven't set up tolower on this value so let's check both
	case 't':
	case 'T':
		std::cout << "Player one is calling player two a liar" << std::endl;
		break;
	// Remember that the switch statement doesn't stop until it gets to a break
	case 'f':
	case 'F':
		std::cout << "Player one is NOT calling player two a liar" << std::endl;
		break;
	default:
		std::cout << "Error: You have entered an invalid value" << std::endl;
	}

	// Let's do the same thing for player two
	switch ( szPlayerTwo )
	{
	case 't':
	case 'T':
		std::cout << "Player two is calling player one a liar" << std::endl;
		break;
	case 'f':
	case 'F':
		std::cout << "Player two is NOT calling player one a liar" << std::endl;
		break;
	default:
		std::cout << "Error: You have entered an invalid value" << std::endl;
	}
}
Last edited on Jan 11, 2012 at 5:51am
Jan 12, 2012 at 2:30am
It was a truth table assignment for discrete mathematics. The main assignment was the efficient use of truth tables. Here is the final program, and I am satisfied with it. I appreciate your help.

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

using namespace std;

int main()
{
	char p, q;

	cout <<"You come across two inhabitants of Smullyan's Island. A says We are both telling the truth, and B says A is lying.\n" << endl;

	cout <<"Enter T or F for p='A is truthful': " ;
	cin >> p;
			
	cout <<"Enter T or F for q='B is truthful': " ;
	cin >> q;
	cout <<"\n" ;

	if((p == 'T') && (q == 'T'))
	{
		cout <<"A says 'We are both telling the truth' is TRUE" << endl;
		cout <<"B says 'A is lying' is FALSE" << endl;
		system("PAUSE");
	}

	if((p == 'F' ) && (q == 'T'))
	{
		cout <<"A says 'We are both telling the truth' is FALSE" << endl;
		cout <<"B says 'A is lying' is TRUE" << endl;
		system("PAUSE");
	}
	if((p == 'T' ) && (q == 'F'))
	{
		cout <<"A says 'We are both telling the truth' is FALSE" << endl;
		cout <<"B says 'A is lying' is FALSE" << endl;
		system("PAUSE");
	}

	if((p == 'F' ) && (q == 'F'))
	{
		cout <<"A says 'We are both telling the truth' is FALSE" << endl;
		cout <<"B says 'A is lying' is TRUE" << endl;
		system("PAUSE");
	}

	return 0;
}
Last edited on Jan 12, 2012 at 2:37am
Topic archived. No new replies allowed.