Jump to content
  • Advertisement
Sign in to follow this  
Jampol

Press enter key to exit

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

Hey everyone, brand new aspiring game programmer here and I have a question of curiosity. I'm working out of a book for C++ right now and so I won't post the whole code as it's someone else's originally. But basically when you hit the enter key after inputting the "fuel" integer the program activates the last line and ends the program immediately, obviously I can put +2 but that's just putting duct tape on the problem and I learn nothing that way. So why does it activate the final line before it's reached? Thanks for any input!


int fuel;
cout << "\nHow much fuel? ";
cin >> fuel;
cout << "fuel: " << fuel << endl;

typedef unsigned short int ushort;
ushort bonus = 10;
cout << "\nbonus: " << bonus << endl;

cout << "Press the enter key to exit";
cin.ignore (std::cin.rdbuf()->in_avail() + 1);
return 0;
}

Share this post


Link to post
Share on other sites
Advertisement

Hey everyone, brand new aspiring game programmer here and I have a question of curiosity. I'm working out of a book for C++ right now and so I won't post the whole code as it's someone else's originally. But basically when you hit the enter key after inputting the "fuel" integer the program activates the last line and ends the program immediately, obviously I can put +2 but that's just putting duct tape on the problem and I learn nothing that way. So why does it activate the final line before it's reached? Thanks for any input!


int fuel;
cout << "\nHow much fuel? ";
cin >> fuel;
cout << "fuel: " << fuel << endl;

typedef unsigned short int ushort;
ushort bonus = 10;
cout << "\nbonus: " << bonus << endl;

cout << "Press the enter key to exit";
cin.ignore (std::cin.rdbuf()->in_avail() + 1);
return 0;
}


With this code you could ignore the next input all day long, but unless you follow it with a "get" you are just going to terminate. Just add a :


cin.get();


line after your ignore and before your return.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!