7 Replies - 291 Views - Last Post: 11 May 2011 - 11:17 AM Rate Topic: -----

#1 ayabrego  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 17-September 10

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?




#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;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Not sure why If statement not right

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5669
  • View blog
  • Posts: 22,517
  • Joined: 23-August 08

Re: Not sure why If statement not right

Posted 11 May 2011 - 08:31 AM

What is Response == 64 > 90 supposed to mean???
Was This Post Helpful? 0
  • +
  • -

#3 ayabrego  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 17-September 10

Re: Not sure why If statement not right

Posted 11 May 2011 - 08:31 AM

View PostJackOfAllTrades, on 11 May 2011 - 08:31 AM, said:

What is Response == 64 > 90 supposed to mean???


Its suppose to mean A to Z. Response is a variable of the user input.
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5669
  • View blog
  • Posts: 22,517
  • Joined: 23-August 08

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?
Was This Post Helpful? 0
  • +
  • -

#5 ayabrego  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 17-September 10

Re: Not sure why If statement not right

Posted 11 May 2011 - 08:39 AM

View PostJackOfAllTrades, on 11 May 2011 - 08:33 AM, said:

Does "response equals 64 greater than 90" make any sense to you in English?


Well then I am not sure on how to validate the user input. How can I say response equals A to z and not Q.
Was This Post Helpful? 0
  • +
  • -

#6 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1831
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

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>.
Was This Post Helpful? 0
  • +
  • -

#7 sk1v3r  Icon User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

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'
Was This Post Helpful? 0
  • +
  • -

#8 RetardedGenius  Icon User is offline

  • >>──(Knee)──►
  • member icon

Reputation: 125
  • View blog
  • Posts: 555
  • Joined: 30-October 10

Re: Not sure why If statement not right

Posted 11 May 2011 - 11:17 AM

Your problem is in line 18:
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. ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1