getch() problem

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2008
Posts: 59
Reputation: Dannyo329 is an unknown quantity at this point 
Solved Threads: 6
Dannyo329's Avatar
Dannyo329 Dannyo329 is offline Offline
Junior Poster in Training

getch() problem

 
0
  #1
Sep 4th, 2008
I just knew about getch() and found it better than cin (a lot) but there's one problem bugging me, can you makea n if statement for getch()?
For example they have:
  1. int a;
  2. cin >> a;
  3. if (a == 1)
  4. //blah
  5. else if (a ==2)
  6. //blah
  7. else
  8. //blah

But can you do something similar with getch()? I thought putting a in those brackets would work but it kept on saying too many arguments to function "int getch()". Can anyone help me with this?
Thanks.
C programmers will get smashed by C++ programmers.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,978
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 308
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: getch() problem

 
0
  #2
Sep 4th, 2008
Something like this should compile:

  1. int a = getch();
  2. if (a == '\n') cout << "enter";

But why do you think getch() is better then cin? Getch() is outdated and non-standard, so if you use it on a modern compiler, you'll get a compiler error.

Besides, comparing getch() with cin is like comparing apples with pears. They're both fruit, but the taste is quite diffrent...
Last edited by niek_e; Sep 4th, 2008 at 6:23 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 59
Reputation: Dannyo329 is an unknown quantity at this point 
Solved Threads: 6
Dannyo329's Avatar
Dannyo329 Dannyo329 is offline Offline
Junior Poster in Training

Re: getch() problem

 
0
  #3
Sep 4th, 2008
Well its partly of my friends, when I show them the program I've made, then something tells them to press 1, so they press 1 but not enter....

And no, that
int a = getch();
if (a == '\n') cout << "enter";
doesn't work. Now it somehow skips straight to 'else', missing out the 'if' and the 'else if'......
I'm gonna have to try hard on this one.
Last edited by Dannyo329; Sep 4th, 2008 at 6:34 am. Reason: Forgot to add something
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,850
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: getch() problem

 
1
  #4
Sep 4th, 2008
>Now it somehow skips straight to 'else', missing out the 'if' and the 'else if'......
That's because getch doesn't perform text conversions. It's a straight detection of scan codes from the console. This means that on systems (such as Windows) where a newline is two values before being converted to '\n' by C++'s stream buffer, calling getch once will give you the first of those values.

Assuming you're on Windows, here's what is happening. When you press Return, a Windows newline is signaled and two values are sent to the console: 0xD and 0xA, in that order. This is the CR-LF pair you may have heard about. Let's say that '\n' in C++ represents 0xA. A standard C++ text stream will take 0xD-0xA and compact it into 0xA, but getch won't do anything of the sort and will simply return 0xD.

So the result is that you're comparing 0xD with 0xA, which is clearly not true, so the else statement is performed. You can catch the carriage return using the '\r' escape character:
  1. #include <iostream>
  2. #include <conio.h>
  3.  
  4. const int ESC = 0x1B;
  5.  
  6. int main()
  7. {
  8. int a;
  9.  
  10. while ( ( a = getch() ) != ESC ) {
  11. if ( a == '\r' )
  12. std::cout<< a <<"\tNewline detected\n";
  13. else
  14. std::cout<< a <<"\tNot a newline\n";
  15. }
  16. }
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 59
Reputation: Dannyo329 is an unknown quantity at this point 
Solved Threads: 6
Dannyo329's Avatar
Dannyo329 Dannyo329 is offline Offline
Junior Poster in Training

Re: getch() problem

 
0
  #5
Sep 8th, 2008
There's a compiler error saying something like this:
ISO C++ forbids comparison between pointer and integer
C programmers will get smashed by C++ programmers.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,978
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 308
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: getch() problem

 
0
  #6
Sep 8th, 2008
Post the code that produces this error
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 59
Reputation: Dannyo329 is an unknown quantity at this point 
Solved Threads: 6
Dannyo329's Avatar
Dannyo329 Dannyo329 is offline Offline
Junior Poster in Training

Re: getch() problem

 
0
  #7
Sep 10th, 2008
Oh, wait. I put these "" instead of these '' when I tried out the code you've posted. Thanks it works fine now, but is there any reason that I should put this in?
  1. const int ESC = 0x1B;
And this
  1. != ESC
in the while loop ?
  1. while ( ( a = getch() ) != ESC )
Thanks
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,978
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 308
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: getch() problem

 
0
  #8
Sep 10th, 2008
Originally Posted by Dannyo329 View Post
but is there any reason that I should put this in?
  1. const int ESC = 0x1B;
And this
  1. != ESC
in the while loop ?
  1. while ( ( a = getch() ) != ESC )
Yes.

The first line ( const int ESC = 0x1B; ) is just to improve readability. 0x1B is the ASCII code for an ESC.

Narue put the " != ESC " in the while loop, to avoid that your program enters an infinite loop. When you press ESC, getch() will pick that up and break out of the while loop.

You could replace those 2 lines by this while ( ( a = getch() ) != 0x1B) , but as you can see, it's a lot less readable that way.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 59
Reputation: Dannyo329 is an unknown quantity at this point 
Solved Threads: 6
Dannyo329's Avatar
Dannyo329 Dannyo329 is offline Offline
Junior Poster in Training

Re: getch() problem

 
0
  #9
Sep 10th, 2008
Thanks
C programmers will get smashed by C++ programmers.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 1633 | Replies: 8
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC