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:

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 saying too many arguments to function "int getch()". Can anyone help me with this?
Thanks.

Recommended Answers

All 8 Replies

Something like this should compile:

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...

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.

>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:

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

There's a compiler error saying something like this:
ISO C++ forbids comparison between pointer and integer

Post the code that produces this error

:) 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?

const int ESC = 0x1B;

And this

!= ESC

in the while loop ?

while ( ( a = getch() ) != ESC )

Thanks

but is there any reason that I should put this in?

const int ESC = 0x1B;

And this

!= ESC

in the while loop ?

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.

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.