PDA

Click to See Complete Forum and Search --> : Keeping a DOS Window Open


DavidP
Jul 4th, 2001, 11:12 PM
I have a small program to help my son practice multiplication.

At the end, I want it to print a report on how he went, giving number of correct answers as a percentage.

I want the program to operate as a stand alone app and not through the VC++ IDE (he's only 8 yrs old.) My problem is that the DOS window closes too quickly at the end of the program and you cannot see the results.

I have looked at some texts which suggest using:

cout << "Press enter to continue";
cin.get();

which I tested on a "Hello World" app and was OK.

The code is:

#include <iostream>

using std::cout;
using std::cin;
using std::endl;
using std::ios;
using std::flush;

#include <cmath>

#include <cstdlib>

#include <ctime>

#include <iomanip>
#include <cstdio>

using std::setprecision;
using std::setiosflags;

int main()
{
int num1, num2, answer, selection;
double right, wrong, total;
char name[20];

right = 0;
wrong = 0;
total = 0;

srand(time(0));
cout << "What is your name?\n";
cin >> name;

cout << "What times table do you want to practice "
<< name << "? (Press '0' to exit)\n\n";

cout << "\nEnter the number you wish to practice here->";
cin >> selection;

while (selection != 0 && answer != 0)
{
while (answer != 0)
{
num1 = selection;
num2 = 1 + rand() % 12;

cout << "\nWhat is " << num1 << " x " << num2 << " ?";
cin >> answer;

while (answer != num1 * num2 && answer != 0)
{
cout << "Sorry " << name << ", wrong answer - try again\n";
wrong++;
cout << "\nWhat is " << num1 << " x " << num2 << " ?";
cin >> answer;
}

if (answer == num1 * num2)
right++;

if(answer == 0) //Exit and goodbye
{
double percent;
total = right + wrong;
percent = (right/total) * 100;

cout << "\n\nBye " << name <<
" and thanks for practicing your times tables!!\n\n";

cout << "You got " << right << " out of " << total
<< setiosflags(ios::fixed) <<
" (" << setprecision(0) << percent << "%)\n\n";

cout << flush;
cout << "\n\nPress enter to continue........";
cin.get();

}
else //Next Question??
cout << "Well Done " << name << "\n";

}


}


return 0;

}

I know this is simple (almost foolish - I'm still learining!) code, but I'd like to find out why the window will not stay open long enough to read the results.

Any help is appreciated.

denniswrenn
Jul 4th, 2001, 11:27 PM
Use:


//includes:
#include <stdlib.h>
//...
//...

system("pause");


It brings up the message "Press any key to continue..." and when you push a key, the program ends(unless you place more code after the call).

-D

Yonatan
Jul 5th, 2001, 11:03 AM
A more common (and more platform-independent) solution:
#include <conio.h>

...

cout << "Press a key..." << endl;
getch();
Oh, and by the way:
// Instead of this:
using std::cout;
using std::cin;
using std::endl;
// etc.

// You can do this:
using namespace std;
Enjoy :rolleyes: