Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

getch() equivalent in C++ STL


  • Please log in to reply
5 replies to this topic

#1 Romeo29

Romeo29

    Learning To Bleep


  • Members
  • 3,194 posts
  • OFFLINE
  •  
  • Gender:Not Telling
  • Location:127.0.0.1
  • Local time:01:12 PM

Posted 24 February 2009 - 10:46 PM

Is there any getch() equivalent in C++ STL?

I dont want to use getch() or _getch() in C++. But I cannot find some good equivalent method of generating the same effect.

Please enlighten me.

BC AdBot (Login to Remove)

 


#2 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:11:12 AM

Posted 24 February 2009 - 10:56 PM

Hello :thumbsup:

I dont want to use getch() or _getch() in C++.

Any particular reason why?

But I cannot find some good equivalent method of generating the same effect.

That's because they don't exist. There is no standard way of accessing unbuffered console input. Even _getch() and _getche() are NOT part of C or C++... however they are implemented by most compilers, they are NOT in the C spec.

http://books.google.com/books?id=GyJcz1Cnj...4&ct=result

C console and port I/OC provides additional I/O routines that have no C++ equivalent, such as _getch( ), _ungetch( ), and _kbhit( )


Just because something is not in the "C++" "objectified" library does not mean that you can't use it... nor does it imply that there is any bad reason to use it. The reason things like cout<< is recommended over c equivalents such as printf, is that printf has problems with type safety. In addition, you can't use printf on a std::basic_string.

When you're looking for something like unbuffered console I/O... why not use those functions?

If you need a class that does it then write your own wrapper around the C api.

Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#3 Romeo29

Romeo29

    Learning To Bleep

  • Topic Starter

  • Members
  • 3,194 posts
  • OFFLINE
  •  
  • Gender:Not Telling
  • Location:127.0.0.1
  • Local time:01:12 PM

Posted 25 February 2009 - 04:54 AM

Thank you Billy :thumbsup:

I thought getch() could be used in C++ but not considered good c++ programming. Just like you can use goto in C or C++, but experts advise against its use.

Thank you for explanation :flowers:

#4 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:11:12 AM

Posted 25 February 2009 - 06:01 AM

GoTo is different... the reasons GoTo should be avoided are due to the fact that it creates unstructured and relatively difficult to follow code.
EDIT: It's not good to use in C either.

Sometimes the C library is better... for example if you allocate an array of 100 CHAR objects:

char *somepointer = new char[100];
char *somepointer = malloc(100*sizeof(char));

New is supposed to always be better -- and in fact when you're dealing with user defined types, it is, because it will call class constructors for you. However, malloc() gives you something new cannot: Reallocation. Realloc() does things new cannot do.

Just more tools in your toolbox to play with :thumbsup:

Billy3

Edited by Billy O'Neal, 25 February 2009 - 06:02 AM.

Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#5 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:12:12 PM

Posted 25 February 2009 - 06:21 AM

Just more tools in your toolbox to play with


Or blow your foot off, as the case may be.

#6 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:11:12 AM

Posted 25 February 2009 - 06:23 AM

LOL! Good point :thumbsup:

char shortchar[5];
cin >> shortchar; //Hope they don't enter more than 4 characters.....

Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users