Jump to content
  • Advertisement
Sign in to follow this  
cj270608

While loop needed...

This topic is 3671 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

I want to make it so that my game begins when someone presses enter, and want do do this with a while loop. How do while loops work?
#include <cstdlib> 
#include <ctime> 
#include <iostream>
#include "Array of numbers.h"

using namespace std;


int main ();

cout << "Welcome to Speedy Addition!";
cout << "Press enter to play!";

The above is my source so far

Share this post


Link to post
Share on other sites
Advertisement


do
{
// Something
} while ( /*Key not pressed*/ );



Your something and key not pressed will depend on os and what you want to do whilst waiting.

Share this post


Link to post
Share on other sites
i strongly suggest that you open a C/C++ book and start reading a few chapter, or watch some tutorials on youtube before start making a game.


This guy is wrong in some points, but the tutorial maybe good for you to start with.

Share this post


Link to post
Share on other sites
You really need to read some tutorials and follow them through - the one i suggested in your other post is pretty good, and is how I started out c++

Mathmo

Share this post


Link to post
Share on other sites
If your using windows:

GetAsyncKeyState()
http://msdn.microsoft.com/en-us/library/ms646293.aspx
Would be easiest for checking if a key is pressed when using a console.

WM_KEYDOWN
http://msdn.microsoft.com/en-us/library/ms646280(VS.85).aspx
Would be the best choice if you had a proper window.

SetWindowsHookEx
http://msdn.microsoft.com/en-us/library/ms644990.aspx
Local keyboard hook might be worth it if your using the console and want to do a lot of key press detection.

Share this post


Link to post
Share on other sites
A really simple, but slightly dated approach is as follow:



#include <cstdlib>
#include <ctime>
#include <iostream>
#include <conio.h> // NEW LINE
#include "Array of numbers.h"

using namespace std;


int main ()
{

cout << "Welcome to Speedy Addition!" << endl; // added endl to make a new line
cout << "Press enter to play!";

while( !_kbhit( ) ) // NEW LINE
; // NEW LINE

}




What the above code does is wait until any key is hit. This is a simple, but effective approach. Again, this is assuming that you are using Windows to run your game on (and that the game is a console app).

I hope this helps!

Share this post


Link to post
Share on other sites
It seems like you need to go try to learn some more. You are clearly a beginner, which is ok, but you need not get ahead of yourself. A basic topic in beginning C++ books/tutorials is cin, and my hint to you is that this might be useful. This tutorial might not be the best, but this page certainly holds the answer you seek. And another hint, hitting enter to begin the game should not be part of the while loop. Once the user hits enter, the program should then enter the while loop.

Read that, try to figure it out, and come back with questions. The hardest part about programming is that there is no one way to do things, and there is not always someone to show you how to do them. You have to be able to create your own solutions, though asking for help is quite acceptable and good.

Share this post


Link to post
Share on other sites
Quote:
Original post by cj270608
Good idea, but gives me a C3861 error


Googling your error gives this. If you read this, you will see that the error means the compiler does not recognise an identifier (a variable or function name).

There are a number of possibilities, you might have mistyped "_kbhit()", or "endl" (some people read this as end-one due to the font, but it is endL , as it stands for endline).

You may have forgotten to include <conio.h>.

This is why you should always post the exact errors you get. The exact error probably includes the name of the identifier the compiler is having trouble with. Where possible, you should also post the source, highlighting the line that is causing the error using a comment (// Here).

Share this post


Link to post
Share on other sites
The easy way to do this is with system("pause"), like they tell you to do at the end of a console app. It will continue on any key, but it will work:

int main(){
cout << "Welcome to SpeedyAddition!" << endl;
system("pause");

//game goes here
}

You will get output like this:
Welcome to SpeedyAddition!
Press any key to continue


A loop is probably how I would do this in a graphical program, but not in a console.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!