Sign in to follow this  

problem with cin

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

Hello. I just started C++ programming using the book Beginning C++ Game Programming and I've enjoyed it thus far. I have been using the authors code as a reference but I ran into a problem while trying to compile and run a program the author created to give an example of getting user input. The program works fine until it comes to the part where I have to enter an integer and it will then display my number back to me.. I type an integer in and hit enter. Then the window closes leaving me wondering what happened. I have created a smaller version of the program that (at least I think) isolates my problem. // Game Stats // problem #include <iostream> using namespace std; int main() { int fuel; cout << "\nHow much fuel? "; cin >> fuel; cout << "fuel: " << fuel << endl; return 0; } the display is supposed to look like this (ive given a sample integer): How much fuel? 100 Fuel: 100 But as soon as i type in 100 and hit enter the window closes and i cant see if the program worked correctly. Can anyone please tell me whats wrong with my code and how to fix it?

Share this post


Link to post
Share on other sites
Your codes fine. When you enter your value it runs the last two lines and the program closes because its finished.

To make the program wait before exiting insert
system("pause");
before you return.

Share this post


Link to post
Share on other sites

This topic is 4845 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this