#include <iostream>
#include <conio.h>
#include <cctype>
using namespace std;
int main()
{
int Response;
cout << "Enter A Character\n";
cin >> Response;
getch();
if (Response == 64 > 90 && Response != 81)// using asci but right here is the problem
{
cout << "good";
getch();
}
else{
cout << "bad";
getch();
}
return 0;
}
Not sure why If statement not right
Page 1 of 17 Replies - 292 Views - Last Post: 11 May 2011 - 11:17 AM
#1
Not sure why If statement not right
Posted 11 May 2011 - 08:23 AM
Hey there guys. I am creating a keypad that will output the corresponding letter to number. Kinda like a keypad on a cell phone. Well I am having trouble on validating the input from the user. The input has to be A to Z (not Z hence to not through)and not Q or any non letter. Whats wrong with my logic?
Replies To: Not sure why If statement not right
#2
Re: Not sure why If statement not right
Posted 11 May 2011 - 08:31 AM
What is Response == 64 > 90 supposed to mean???
#3
Re: Not sure why If statement not right
Posted 11 May 2011 - 08:31 AM
#4
Re: Not sure why If statement not right
Posted 11 May 2011 - 08:33 AM
Does "response equals 64 greater than 90" make any sense to you in English?
#5
Re: Not sure why If statement not right
Posted 11 May 2011 - 08:39 AM
#6
Re: Not sure why If statement not right
Posted 11 May 2011 - 09:24 AM
Deal with A-Z or a-z and not 'Q' and not 'q' separately.
Or use the isalpha function in <cctype>.
Or use the isalpha function in <cctype>.
#7
Re: Not sure why If statement not right
Posted 11 May 2011 - 09:32 AM
if it is between a and z, then it is more than or equal to 'a', and less than or equal to 'z'
#8
Re: Not sure why If statement not right
Posted 11 May 2011 - 11:17 AM
Your problem is in line 18:
That's not how boolean expressions are evaluated in an if statement, it simply means nothing. What you need to do is separate them into two different comparisons.
E.g to ensure that 5 ≤ x < 10 you would write the following:
You may find the code easier to understand if you put each separate boolean expression inside parenthesis i.e.
Although this is not necessary, because of the hierarchy of operators.
if (Response == 64 > 90 && Response != 81)
That's not how boolean expressions are evaluated in an if statement, it simply means nothing. What you need to do is separate them into two different comparisons.
E.g to ensure that 5 ≤ x < 10 you would write the following:
if ( 5 <= x && x < 10)
You may find the code easier to understand if you put each separate boolean expression inside parenthesis i.e.
if ( (5 <= x) && (x < 10) )
Although this is not necessary, because of the hierarchy of operators.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote







|