Jump to content
  • Advertisement
Sign in to follow this  
Danishpx

problem with exercise in C++ book

This topic is 5046 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 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 this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
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 this post


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

// Lost Fortune
// A personalized adventure

#include <iostream>
#include <string>

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

int main()
{
const int GOLD_PIECES = 900;
int adventurers, killed, survivors;
string leader;

// get the information
cout << "Welcome to Lost Fortune\n\n";
cout << "Please enter the following for your personalized adventure\n";

cout << "Enter a number: ";
cin >> adventurers;

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

survivors = adventurers - killed;

cout << "Enter your last name: ";
cin >> leader;

//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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by MaulingMonkey
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);


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... :)

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!