Welcome
Welcome to the forums of AntiRTFM's Absolute N00b Spoonfeed C++ Tutorials!

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. Registration is fast, simple, and absolutely free, so please, <a href="/profile.php?mode=register">join our community today</a>!

Nice little "Press any button to continue thing"

Display your little helpful snippets of code that may help us all get something done

Nice little "Press any button to continue thing"

Postby Doctor Salt on Fri Aug 22, 2008 9:52 pm

Okay, so after a little bit of research, i found a nice way to have a function you could call, and pressing ANY button would make it continue

(without having to press enter afterwords)

Also, the headers i included are required, i believe, for it to work. Of course, all other headers are still need too, like #include <iostream>.

Code: Select all
#include <conio.h>
#include <stdio.h>

void wait ()
{
cout << "\nPress any key to continue\n";

while(!kbhit()) ;  //means while no buttons are pressed
return;
}


So add this function in anywhere, then when ever you want, just call
wait ();
then it will temporarily pause until a button is pressed. Hope this helped someone.
I'm a 15 year old kid who has his heart set on Programming, almost as much as Marss++.......

Came here hoping to ask more questions so if you have any free time just say!
Doctor Salt
 
Posts: 147
Joined: Wed Jul 30, 2008 3:32 pm

Re: Nice little "Press any button to continue thing"

Postby Argio on Sun Nov 30, 2008 8:08 pm

Um, is that C? Lawl correct me if I am mistaken but I used to do C and I know one of those headers is C!
I love C++, for reasons I have yet to discover.
All the haters love me so lovers stop hating.
Image
User avatar
Argio
 
Posts: 21
Joined: Fri Jun 20, 2008 5:04 am
Location: Utah. I am jewish.

Re: Nice little "Press any button to continue thing"

Postby antiRTFM on Sun Nov 30, 2008 8:29 pm

About the "kbhit()" function;

"This function is not defined as part of the ANSI C/C++ standard. It is generally used by Borland's family of compilers."

- from some website about c++
User avatar
antiRTFM
Administrator
 
Posts: 462
Joined: Sun Apr 13, 2008 9:10 am

Re: Nice little "Press any button to continue thing"

Postby DKeller4113 on Tue Jun 09, 2009 2:16 am

I found an even easier way to do this. two lines, it even displays the "Press any key to continue . . . " text automatically.

in your declarations, put:

Code: Select all
#include <windows.h>


then, whenever you want the user to press any key to continue, insert this:

Code: Select all
system("pause")


tada =) a bit less technical. easier for n00bs like me =)
DKeller4113
 
Posts: 4
Joined: Tue Jun 09, 2009 2:11 am

Re: Nice little "Press any button to continue thing"

Postby zorro59 on Wed Jul 29, 2009 5:40 am

kbhit()
Does it works an visual c++ express????
It is exactly what i need to make my program shorter...
zorro59
 
Posts: 77
Joined: Tue May 12, 2009 2:28 am

Re: Nice little "Press any button to continue thing"

Postby Meltra Bour on Wed Jul 29, 2009 3:03 pm

ugh old posts ... don't use kbhit, it's not c++ and take a close look at your cpu usage, using a loop like that to pause or wait for key event is euh silly ;)
Meltra Bour
 
Posts: 8
Joined: Tue Jul 14, 2009 6:46 am

Re: Nice little "Press any button to continue thing"

Postby learning in progress on Thu Aug 06, 2009 12:12 pm

doesnt
#include <conio.h>
and using
getch();
do the same thing?
learning in progress
 
Posts: 11
Joined: Thu Jul 09, 2009 4:15 pm

Re: Nice little "Press any button to continue thing"

Postby ryancfcsas on Sun Aug 23, 2009 9:14 am

NEVER EVER USE system("PAUSE") !!!

It calls the system and use more resources than it should. Also it is not portable!!!

On top of that, my firewall always asks if I give it permission to use system calls.

Same goes with system("CLS") and system("title ENTER TITLE")

It is also a habit you don't want to get into, bad coding!

The best thing instead of system("PAUSE") is cin.get();
Note! use cin.ignore(); after inputting data. I.E:

Code: Select all
#include <iostream>
using namespace std;

int main()
{
     int x;
     cin >> x;
     cin.ignore();
     
     cin.get(); //Best thing!
     return 0;
ryancfcsas
 
Posts: 2
Joined: Sun Aug 23, 2009 9:03 am

Re: Nice little "Press any button to continue thing"

Postby Vevix on Mon Oct 19, 2009 6:34 pm

As ryancfcsas said above, I'd implement it like

Code: Select all
std::cin.clear();
std::cin.sync();
std::cin.get();
User avatar
Vevix
 
Posts: 75
Joined: Sat Oct 10, 2009 9:24 pm
Location: ntdll.dll


Return to Snippets

Who is online

Users browsing this forum: No registered users and 0 guests