| | |
getch() problem
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
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:
But can you do something similar with getch()? I thought putting a in those brackets would work but it kept on
. Can anyone help me with this?
Thanks.
For example they have:
C++ Syntax (Toggle Plain Text)
int a; cin >> a; if (a == 1) //blah else if (a ==2) //blah else //blah
But can you do something similar with getch()? I thought putting a in those brackets would work but it kept on
Thanks.
C programmers will get smashed by C++ programmers.
Something like this should compile:
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...
C++ Syntax (Toggle Plain Text)
int a = getch(); 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.
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
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.
And no, that
•
•
•
•
int a = getch();
if (a == '\n') cout << "enter";
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
>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:
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)
#include <iostream> #include <conio.h> const int ESC = 0x1B; int main() { int a; while ( ( a = getch() ) != ESC ) { if ( a == '\r' ) std::cout<< a <<"\tNewline detected\n"; else std::cout<< a <<"\tNot a newline\n"; } }
I'm here to prove you wrong.
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)
const int ESC = 0x1B;
C++ Syntax (Toggle Plain Text)
!= ESC
C++ Syntax (Toggle Plain Text)
while ( ( a = getch() ) != ESC )
•
•
•
•
but is there any reason that I should put this in?
And thisC++ Syntax (Toggle Plain Text)
const int ESC = 0x1B;
in the while loop ?C++ Syntax (Toggle Plain Text)
!= ESC
C++ Syntax (Toggle Plain Text)
while ( ( a = getch() ) != ESC )
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. ![]() |
Similar Threads
- replacement of getch() (C++)
- how to reverse a numbers input by user (C)
- Problem in C program (C)
- reversing number problem. (C)
- bank system problem !!!! (C++ programming) (C++)
- display + library problem (C)
- loop problem (C++)
- Program Problem with a select statement to access Data base (C)
- how to make maze game using "gotoxy" & getch? (C++)
- Problem About Pointers (C++)
Other Threads in the C++ Forum
- Previous Thread: c++ mentors
- Next Thread: Uploading file to a web server
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count data delete desktop developer directshow dll download dynamic encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib library linkedlist linker linux loop looping loops map math matrix memory newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive return string strings struct studio temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






