Hi, I am writing a program as an assignment for a class, and anyways I've always just used the code I put below to pause my program.
cin.ignore ();
cout << "Press Enter to end the program." <<endl;
cin.get ();
There are no compiler errors/warning when I compile, but when I run it my command prompt immediately closes. I also tried doing system("pause") instead even though I have been taught not to use that if I can avoid it. Here is all of my code so far for the program; it isn't finished so you'll just have to kind of ignore some of the parts.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <fstream>
using namespace std;
//Global
const int SIZE= 12;
//Prototypes
void displayOverview();
void readData (double data [], int qty);
void Total(double data [], int qty);
void Lowest(double data [], int qty);
void Highest(double data [], int qty);
void Average(double data [], int qty);
void Median (double data [], int qty);
//Pause program
cin.ignore ();
cout << "Press Enter to end the program." <<endl;
cin.get ();
return 0;
}
// displayOverview just lets the user know what the program is doing
void displayOverview ()
{
cout << "This program reads the monthly rainfall and then finds the "
<< "total, low, high, and average rainfall for the year. " <<endl;
return;
}//displayOverview
void readData (double data [], int qty)
{
int count;
ifstream inputFile;
inputFile.open ("rainfall.txt");
for (count = 0; count < qty; count++)
inputFile >> data[count];
That does pause it because I have always just used that, and I didn't really mean to include ctime i just copied those from a different array program I had been working on where I needed that to generate random numbers. Anyways there has to be something wrong that system("pause") and the method that has always worked for me before both aren't working now. We haven't ever learned anything like what you said up there, so I'm assuming my teacher wouldn't approve of me doing it that way.
Yeah I know I was just being silly... English semantics. Really it's asking for input but I believe you need to put limits in the ignore as this is what will clear the buffer ( you may have some left newlines in buffer or something which is why it's not working).
basically your program is terminating before the end, putting an ignore (or whatever your preference) in the destructor is going to stop your program to wait for input, no matter if it exits successfully or not.