Jump to content
  • Advertisement
Sign in to follow this  
Aureaux

Beginnings C++ Question. . ?

This topic is 3785 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 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 <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;

    std::cout << "Press Enter to exit";
    std::cin.ignore(std::cin.rdbuf()->in_avail() + 1);
       
    typedef unsigned short int ushort;
    ushort bonus = 10;
    cout << "\nbonus: " << bonus << endl;

    std::cout << "Press Enter to exit";
    std::cin.ignore(std::cin.rdbuf()->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 this post


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


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


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


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


f_DoubleSnippm_da7513b.jpg
Caption; After pressing enter once.

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

Regards,
Casey McCormack

Share this post


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


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


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


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

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!