Program won't pause

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);

int main()
{
double rain[SIZE];

//Calls
displayOverview();
ifstream inputFile;
inputFile.open("rainfall.txt");
if (inputFile.fail())
{
cout << "Error opening file." << endl;
exit(0);
}
readData(rain, SIZE);




//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];

inputFile.close();

//debug
for (count = 0; count < SIZE; count++)
cout << "Month " << count+1 << ": " << data[count] << endl;
return;
}
Last edited on
Is anybody going to bother to respond..?
yes edit: on more serious note, maybe ignore(1000,'\n');
Last edited on

//Pause program
cin.ignore ();
cout << "Press Enter to end the program." <<endl;
cin.get ();


This is not a pause, I thought you wanted a pause..... in fact you even include ctime.
ok on the clock page: http://cplusplus.com/reference/clibrary/ctime/clock/

is a good example, for making the program actually pause.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/* clock example: countdown */
#include <stdio.h>
#include <time.h>

void wait ( int seconds )
{
  clock_t endwait;
  endwait = clock () + seconds * CLOCKS_PER_SEC ;
  while (clock() < endwait) {}
}

int main ()
{
  int n;
  printf ("Starting countdown...\n");
  for (n=10; n>0; n--)
  {
    printf ("%d\n",n);
    wait (1);
  }
  printf ("FIRE!!!\n");
  return 0;
}
Last edited on
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.
Last edited on
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).

ignore (1000,'\n');
It still just closes :s
Well, I'm glad to see that you enjoy wasting OP's time, but what he wants is a pause. What you have there is a delay.
Doesn't anybody have a reason why it isn't pausing like something I messed up in my code?
I can only guess, I have never needed to use it before.

try this:

cout << "Press ENTER to exit";
cin.ignore(1000,'\n');

(trim the get())

If that still fails, are you sure this is not simply a compile error? eg. you are definately getting program output?

try some of the other methods here:
http://cplusplus.com/forum/beginner/1988/
Last edited on
It didn't work. There has to be something wrong that even system("pause") doesn't pause it.
bump, I edited.

If that still fails, are you sure this is not simply a compile error? eg. you are definately getting program output?
Last edited on
This is why...

cout << "Error opening file." << endl;
exit(0);

Instead of the exit, put in the "Press Enter!"; cin.ignore(1000,'\n'); exit(1);

Read the thread here: http://cplusplus.com/forum/beginner/1988/

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.
Last edited on
Okay, thanks a lot! I'm not sure why it is doing that though I have the file named rainfall.txt in the same folder as it. Thanks for your help.

Edit: problem solved thanks
Last edited on
your welcome. Did you figure out why the file was not opening?
Topic archived. No new replies allowed.