943,769 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 3596
  • C++ RSS
Sep 4th, 2008
0

getch() problem

Expand Post »
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:
C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 20
Solved Threads: 8
Junior Poster in Training
Dannyo329 is offline Offline
79 posts
since Apr 2008
Sep 4th, 2008
0

Re: getch() problem

Something like this should compile:

C++ Syntax (Toggle Plain Text)
  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 Nick Evan; Sep 4th, 2008 at 6:23 am.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Sep 4th, 2008
0

Re: getch() problem

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
Quote ...
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
Reputation Points: 20
Solved Threads: 8
Junior Poster in Training
Dannyo329 is offline Offline
79 posts
since Apr 2008
Sep 4th, 2008
1

Re: getch() problem

>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:
C++ Syntax (Toggle Plain Text)
  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. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Sep 8th, 2008
0

Re: getch() problem

There's a compiler error saying something like this:
ISO C++ forbids comparison between pointer and integer
Reputation Points: 20
Solved Threads: 8
Junior Poster in Training
Dannyo329 is offline Offline
79 posts
since Apr 2008
Sep 8th, 2008
0

Re: getch() problem

Post the code that produces this error
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Sep 10th, 2008
0

Re: getch() problem

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?
C++ Syntax (Toggle Plain Text)
  1. const int ESC = 0x1B;
And this
C++ Syntax (Toggle Plain Text)
  1. != ESC
in the while loop ?
C++ Syntax (Toggle Plain Text)
  1. while ( ( a = getch() ) != ESC )
Thanks
Reputation Points: 20
Solved Threads: 8
Junior Poster in Training
Dannyo329 is offline Offline
79 posts
since Apr 2008
Sep 10th, 2008
0

Re: getch() problem

Click to Expand / Collapse  Quote originally posted by Dannyo329 ...
but is there any reason that I should put this in?
C++ Syntax (Toggle Plain Text)
  1. const int ESC = 0x1B;
And this
C++ Syntax (Toggle Plain Text)
  1. != ESC
in the while loop ?
C++ Syntax (Toggle Plain Text)
  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.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Sep 10th, 2008
0

Re: getch() problem

Thanks
Reputation Points: 20
Solved Threads: 8
Junior Poster in Training
Dannyo329 is offline Offline
79 posts
since Apr 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: c++ mentors
Next Thread in C++ Forum Timeline: Uploading file to a web server





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC