I am just beginning to learn how to program. I'm using the "Beginning C++ Game Programming" book written by Michael Dawson. One of the programs in the book isn't working out like it should. Would this be the forum I use to get some advice on what I might be doing wrong?

This is a forum for help with C or C++ programming. There is an announcement or two at the top of it.

Alright, I just wanted to make sure.

The book has me write a code that will display certain gaming stats. The goal of this section is to teach me about variables. Here is a copy of the code that I used.

// Game Stats
// Demonstrates declaring and initializing variables

#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 << "Play again? "    << playAgain << endl;
    //skipping shieldsUp since you don't generally print Boolean values
    cout << "lives: "    << lives << endl;
    cout << "Aliens Killed: "    << aliensKilled << endl;
    cout << "Engine Temperature: "   << engineTemp << endl;
    
    int fuel;
    cout << "\nHow much fuel is left? ";
    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;
}

This code first displays a couple of common game stats, and then asks me to enter a value for the amount of fuel I have. After I enter an amount, it is suppose to display that amount, and then it will display a bonus number. In this case it would be 10. The problem I have is that once I enter the fuel and press enter, the console immediately closes. I tried erasing the program from the int fuel; line to the cout << "\nbonus: " << bonus << endl; line and it seemed to work okay. I would get the message that says press enter to exit and it would work okay. As soon as I put that section of the code back in, it would stop working properly. So, the problem is there somewhere, I'm just not experienced enough to know what it could be.

just change the

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

to

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

Sounds kinda like this.

Well, it cold be that dave, but he is using cin.ignore(std::cin.rdbuf()->in_avail() + 2); as a system("pause"); and there can be code afterthe pause, and then it would be a bug.

I was most certainly not recommending using system("pause").

The issue is with the window disappearing -- which itself is not a programming problem; it is an issue with the shell in which the program is run.

That said, the "problem" is that the last cin takes a number followed by a newline. The newline is left in the input buffer. So getting a single character wouldn't work because it would find the newline already there and not wait. Getting a second character would. Something akin to this:

cout << "Press the enter key to exit";
    cin.get(); // get the one left over from the numeric input
    cin.get(); // wait for the user to press enter

But I was mostly trying to point out the fact the OP ought to understand what the issue is instead of slinging code at it.

But I was mostly trying to point out the fact the OP ought to understand what the issue is instead of slinging code at it.

I don't really understand what you mean by this statement.

I've been programming for all of about one day now. I don't even know how to write my own program yet. The program that I wrote was just something I copied out of the book to help me remember the different types of code I can put in.

Have you ever launched a program like a virus scanner that closes after it is finished? (At least if you've selected such an option.) It's easy to see something happening when it takes minutes or hours to complete. But if it scanned your entire disk drive in a millisecond and disappeared, you might have missed the fact that it ran at all. This is not to say that just because you didn't see it that it didn't actually happen. So it wouldn't be an issue of the virus scanner not working, rather an issue with the person running the code.

Similar concept. Because a command-line program is run from somewhere other than the command line, a similar thing might occur. Just like you wouldn't call to curse at the virus scan people if their software ran really well, you don't need to look for code changes to correct code.

But alas, this is the age of the GUI and instead we tell young padawans to put kludges in their code and hope that they learn not to use them later.

Have you ever launched a program like a virus scanner that closes after it is finished? (At least if you've selected such an option.) It's easy to see something happening when it takes minutes or hours to complete. But if it scanned your entire disk drive in a millisecond and disappeared, you might have missed the fact that it ran at all. This is not to say that just because you didn't see it that it didn't actually happen. So it wouldn't be an issue of the virus scanner not working, rather an issue with the person running the code.

Similar concept. Because a command-line program is run from somewhere other than the command line, a similar thing might occur. Just like you wouldn't call to curse at the virus scan people if their software ran really well, you don't need to look for code changes to correct code.

But alas, this is the age of the GUI and instead we tell young padawans to put kludges in their code and hope that they learn not to use them later.

There are a few terms you used that I'm not familiar with so I may be way off base on the interpretation of what you're trying to say.

It seems like you're saying that the trouble I was having wasn't something directly related to the code itself. Instead, it was in how that code is used by the rest of the computer. Since we are used to using a GUI interface, the solution has been to change the code as a quick fix, with the hope that later on we won't do this. The focus instead, should be on finding a solution that doesn't involve changing the code that is already correct.

I'll try to simplify: run command-line programs from a command line.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.