Public Group

Beginnings C++ Question. . ?

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

Recommended Posts

I recently embarked on my venture into programming, starting with very little previous experience; However, I have stumbled already and need some assistance. Upon reaching as far as "Chapter 1: Game Stats" of Michael Dawson's "Beginning C++ Game Programming", I cannot get the executable to work properly. This is the code (I am un-aware of typographical limits so I apologize in advance): Note: The general problem is after entering "fuel amount" and pressing enter the console closes too quickly for me to see the output/outcome. The only solution I've had is to add the following (in bold); ~
#include &lt;iostream&gt;
using namespace std;

int main()
{
int score;
double distance;
char playAgain;
bool shieldsUp;

short lives, aliensKilled;

score = 0;
distance = 1200.76;
playAgain = 'y';
shieldsUp = true;
lives = 3;
aliensKilled = 10;

double engineTemp = 6572.89;

cout &lt;&lt; "\nscore: "         &lt;&lt; score &lt;&lt; endl;
cout &lt;&lt; "distance: "        &lt;&lt; distance &lt;&lt; endl;
cout &lt;&lt; "playAgain: "       &lt;&lt; playAgain &lt;&lt; endl;
// Skipping shieldsUp since you do not generally print Boolean values.
cout &lt;&lt; "lives: "           &lt;&lt; lives &lt;&lt; endl;
cout &lt;&lt; "aliensKilled: "    &lt;&lt; aliensKilled &lt;&lt; endl;
cout &lt;&lt; "engineTemp: "      &lt;&lt; engineTemp &lt;&lt; endl;

int fuel;
cout &lt;&lt; "\nHow much fuel? ";
cin &gt;&gt; fuel;
cout &lt;&lt; "fuel: " &lt;&lt; fuel &lt;&lt; endl;

std::cout &lt;&lt; "Press Enter to exit";
std::cin.ignore(std::cin.rdbuf()-&gt;in_avail() + 1);

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

std::cout &lt;&lt; "Press Enter to exit";
std::cin.ignore(std::cin.rdbuf()-&gt;in_avail() + 1);

return 0;
}


~ I've also tried the following code to no success (between return 0; and the above text): std::cout << "Press Enter to continue.\n"; std::cin.ignore(INT_MAX, '\n'); I'm certain there is an easier solution than having the two "Press Enter to exit" code. . . Um. . . Parts?~ I'd rather not use the .bat solution, if applicable. Thanks for your time. Regards, Casey McCormack Postscript: I've forgotten the text-editing code for forums of this sort, I apologize, otherwise I'd change font size. [Edited by - Aureaux on February 7, 2008 9:00:11 PM]

Share on other sites
Nice to meet you, and thank you for your quick reply. :)

I have tried both codes from the beginner guide, neither seem to work.

Maybe I apply them wrongly. . ?~

Regards,
Casey McCormack

Share on other sites
Instead of std::cin.ignore(std::cin.rdbuf()->in_avail() + 1);, you can use system("PAUSE"), which has much cleaner syntax.

Share on other sites
You can just write:

cout << "Press Enter to quit...";cin.get();

BTW, why did you write that code snippet twice? It seems to me that it's only needed the second time.

Also, I've heard that using system("PAUSE") has some caveats, but I don't remember them.

Share on other sites
After trying each of all methods suggested, the program still does not work.

Each time, I'd type in x amount of fuel, hit Return and the console closes.

Quote:
 BTW, why did you write that code snippet twice? It seems to me that it's only needed the second time.

Quote:
 I'm certain there is an easier solution than having the two "Press Enter to exit" code. . . Um. . . Parts?~

Sorry for my non-descriptive questioning, anyhow, the only success so far has been to have the code snippet in there twice, it is shown below ( or at least linked ). :3

Caption; After pressing enter once.

If I don't have the "double snippet", the console just exits immediately.

Regards,
Casey McCormack

Share on other sites
I just typed in your program, and it works properly with just the second copy of your snippet (the one before the return statement).

Also, my suggestion indeed didn't work, but this did:

cout << "Press Enter to quit...";cin.get();cin.get();

Share on other sites
#include <iostream>using namespace std;int main(){int score;double distance;char playAgain;bool shieldsUp;short lives, aliensKilled;score = 0;distance = 1200.76;playAgain = 'y';shieldsUp = true;lives = 3;aliensKilled = 10;double engineTemp = 6572.89;cout << "\nscore: " << score << endl;cout << "distance: " << distance << endl;cout << "playAgain: " << playAgain << endl;// Skipping shieldsUp since you do not generally print Boolean values.cout << "lives: " << lives << endl;cout << "aliensKilled: " << aliensKilled << endl;cout << "engineTemp: " << engineTemp << endl;int fuel;cout << "\nHow much fuel? ";cin >> fuel;cout << "fuel: " << fuel << endl;system("PAUSE");typedef unsigned short int ushort;ushort bonus = 10;cout << "\nbonus: " << bonus << endl;system("PAUSE");return 0;}

Share on other sites
You shouldn't have to put anything in your code. I don't know what IDE you are using, but Visual Studio has an option to "Run without debugging" which leaves the window open. I imagine other IDE's have something similar.

Share on other sites
Quote:
 I just typed in your program, and it works properly with just the second copy of your snippet (the one before the return statement).

I presume it's my IDE that is mucking up my progress (;p), I currently use Bloodshed Dev-C "PLUS PLUS" (the plug sign won't show up. . ?) that came with the book, just as to follow the tutorials faithfully.

I'll find another. . . But what are your recommendations?~

Regards,
Casey McCormack

1. 1
2. 2
3. 3
Rutin
18
4. 4
JoeJ
14
5. 5

• 14
• 10
• 23
• 9
• 32
• Forum Statistics

• Total Topics
632630
• Total Posts
3007521
• Who's Online (See full list)

There are no registered users currently online

×