Jump to content
  • Advertisement
Sign in to follow this  
Da cobra

getch() and getchar() = c++?

This topic is 5035 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Maybe a strange question, but are the functions getch() and/or getchar() C++ functions or just C functions? Because I have to make a dos menu in C++ and not C. Thanx in advance for an answer

Share this post


Link to post
Share on other sites
Advertisement
getch() and getchar() are technically C -- though, exist in C++. C++ is backwards compatible.

you can tell because they are procedural methods, and are not objects. cin and cout are objects. Instead, you could use "cin.get()".

Share this post


Link to post
Share on other sites
With cin.get() I need to press enter before the character is read.
Isn't there another way, so I just have to press 1 button on my keyboard?

Share this post


Link to post
Share on other sites
getchar() and getc() are C functions. However, the C++ standard explicitely includes by reference the C standard library. Which makes any standard C library function a standard C++ library function.

Therefore getchar() and getc() can be considered C++ functions.

getch() is not a C function. It's a 3rd-party API function. The ability to read a single character (without waiting for a carriage return) is not covered by the standard C library, as it is dependent on the properties of the terminal you are reading from, which may (or may not) only send the data to the program on a carriage return.

If you are not trying to write portable code, feel free to use getch(). Otherwise, get familiar either with cross-platform APIs, or with each platform's API (e.g. curses and termio on Linux).

edit - visage, there is no such thing as cin.getch().

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!