need C++ help

Started by
7 comments, last by GameDev.net 18 years, 7 months ago
I'm about to go crazy!!! I recently purchased a book called " Beggining C++ game programming " and i am stuck. i just finished writing the program that the chapter 1 questions tell you to write as a test. i wrote the program and it takes three numbers that i input and finds the average. its kind of simple but when i input the third number and hit enter the program automaticly shuts off and i cant see the product of my work(the average of the three input number's average). I can see the average show up for a split second but i cant seem to make it stay so i can see it
Advertisement
Just make a temp variable and throw a cin statement before return 0, if you're using a Windows-based operating system you can include stdlib.h and put system("PAUSE") before return 0 at the end of the program instead of cin.
Your easiest option is to run the program from the DOS prompt -- use the "cd path\to\directory" command to navigate to the directory the program is in, and then type the name of the program to execute it. Other options include the system PAUSE command, using cin to force the user to press enter before it exits, or using getch() from conio.h to force the user to press any key before it exits.
{[JohnE, Chief Architect and Senior Programmer, Twilight Dragon Media{[+++{GCC/MinGW}+++{Code::Blocks IDE}+++{wxWidgets Cross-Platform Native UI Framework}+++
thank you very much you have solved my problem.

alas i have a new problem. just finished creating a small game using random number generators. the game itself works fine but i want to make it ask me to play again and have a y/n input system. the code for play again i already know but i cant seem to combine it with my game to where if you hit y then its starts over and if you hit n it stops. when i hit y it doesnt do any thing can you help me
I have that book for C++ stdlib reference. And he actauly states on the top of page 9 how to prevent that....
Related to your secound problem... Read on in the book he tells you how to do all that in a while loop.
In all games there's a game loop. This game loop handles many things such as input, output etc. but it basically checks whether or not the game is finished.

Here's a very quick example of how to create a game loop:
int done = 0;while ( done == 0 ){/* TODO: Code for your game loop goes here.This area holds everything your game does whilst it's running. */}
Just use a while loop and some if's and you can do that. I take it you don't know how to create functions yet(you might know how to.) If you do, then create function for plat again, and to quit. If you don't then do it a different way. Just full around, and you might beable to fix it.
Use a do {} while () loop. Have:

char yesNo = 'y';

do {
// Play the game...

cout>yesNo;
} while (yesNo != 'n' || yesNo != 'N')


Do all that atleast once(the do loop) and update the variable.

This topic is closed to new replies.

Advertisement