# problem with exercise in C++ book

This topic is 4959 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have Beginning C++ Game Programming by Michael Dawson and I'm on the exercise titled "I ve completed the code and the program runs fine. It asks for those numbers and its fine but then it asks for your last name. I enter it and when i press enter the program quits. I checked and it seems i have no errors in the code. Why does it exit?

##### Share on other sites
Why do you post asking such a question with absolutely no code to show? How the hell are we supposed to know what it's doing?@@?!?!?!

##### Share on other sites
sorry about that im new at this. Here is my whole code using the compiler Dev C++:

// Lost Fortune

#include <iostream>
#include <string>

using std::cout;
using std::cin;
using std::endl;
using std::string;

int main()
{
const int GOLD_PIECES = 900;

// get the information
cout << "Welcome to Lost Fortune\n\n";

cout << "Enter a number: ";

cout << "Enter a number, smaller than the first: ";
cin >> killed;

cout << "Enter your last name: ";

//tell the story
cout << "\nA brave group of " << adventurers << " set out on a quest ";
cout << "- in search of the lost treasure of the Ancient Dwarves. ";
cout << "The group was led by that legendary rogue, " << leader << " . \n";

cout << "\nAlong the way, a band of marauding ogres ambushed the party. " ;
cout << "All fought bravely under the command of " << leader;
cout << " , and the ogres were defeated, but at a cost. ";

cout << "Of the adventurers, " << killed << " were vanquished, ";
cout << "leaving just " << survivors << " in the group.\n";

cout << "\nThe party was about to give up all hope. ";
cout << "But while laying the deceased to rest, ";
cout << "they stumbled upon the buried fortune. ";
cout << "So the adventurers split " << GOLD_PIECES << " gold pieces. ";
cout << leader << " held on to the extra " << (GOLD_PIECES % survivors);
cout << " pieces to keep things fair of course.\n";

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

return 0;
}

##### Share on other sites
You're not pausing it at the end of the program. If you were to run that in a DOS window it would work. Try this: add #include <conio.h> to the top and at the very end of your program, just before the return 0; add the line
while(!kbhit()) {}
That should fix your problem and not quit until you hit a key.

##### Share on other sites
OMG thank you so much you are such a big help.It works now! Do u mind, just b/c i would like to understand, what that code is and what it means. Is it only used for my problem?

##### Share on other sites
kbhit is a command that checks to see if a keyboard key was pressed, and maybe returns the value (it's not cross platform so I've never used it). A slightly better (more cross platform) solution might be to use getchar() until the char is a new line ('\n'). That way it'll pause until you get the return or enter key's pressed. An even better solution would be to not worry about it until you get into the really fun stuff, like graphics with SDL, Allegro, even GLUT (shudders). Have Fun!

##### Share on other sites
kbhit is nonstandard and won't exist on many systems. I don't exactly know what the heck that book is doing with this line near the end:

 std::cin.ignore(std::cin.rdbuf()->in_avail() + 1);

But if you replace it with:

std::cin.get();

or since you're "using std::cin":
cin.get();

Your code should work as expected (quitting once the user hits enter). This is more standard. However, that line of code could cause problems if you then try to read more input afterwards (same with the kbhit method). To cope with this, one can do this:

std::string unused_string;std::getline( std::cin , unused_string );

Which will put the entire line up to the enter key into unused_string, which you can then promptly ignore.

Also, instead of using '\n', one should prefer to use endl, as some systems will display really weirdly if they get just a \n. Example:

original code:
cout << "\nI like pie\n";

replacement code:
cout << endl << "I like pie" << endl;

##### Share on other sites
Thnk u for all yer help everyone, I think my problems are solved.

##### Share on other sites
Quote:
 Original post by MaulingMonkeykbhit is nonstandard and won't exist on many systems. I don't exactly know what the heck that book is doing with this line near the end: std::cin.ignore(std::cin.rdbuf()->in_avail() + 1);

I think the idea is:

- Check how much data is remaining on cin.
- Skip past that much data, plus 1 byte. This is supposedly a blocking I/O call, so it will wait until that much data is available to be skipped over - which requires one more byte to be put on to cin, i.e. one more keypress.

Maybe it doesn't block though. :s And I certainly agree that a line like that has no business in a book titled "Beginning C++ Game Programming." Although I personally don't think the words "beginning", "programming" and "C++" ought ever be uttered all in the same breath... :)

• 10
• 19
• 14
• 19
• 15