Program Flashing and Disappearing Problem When Executed,

When I try to start my program without debugging, the command prompt screen flashes and disappears, and I was wondering whether there is any way to keep the screen without using cin.get(), and to get the "press any key to continue" to display. I want this to happen so I can select all and copy the output from the command prompt screen.

My code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*Arooj Mohammed	C++Ch13Ex20

Average price: $28.07

*/
//Ch13Ex20.cpp- calculates and displays the average price of the company's inventory items.

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>

using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::ifstream;
using std::ios;
using std::setiosflags;
using std::setprecision;


int main()
{
	//declare variables
	double total = 0.0;
	double average = 0.0;
	double price = 0.0;
	int x = 0;

	//create file object and open the file
	ifstream inFile;
	inFile.open("prices.txt", ios::in);

	//determine whether the file was opened
	if (inFile.is_open())
	{
		inFile >> price;
		inFile.ignore();
		//read a line from the file
		while (!inFile.eof())
		{
			total = total + price;
			x = x + 1;
			inFile >> price;
			inFile.ignore();
		} //end while
		average = total / x;
		cout <<  "Average price: $" << setprecision(4) << average << endl;
		//close the file
		inFile.close();
	} //end if
	else 
		cout << "File could not be opened." << endl;
	//end if
	return 0;
}	//end of main function 


I used a text file with the following inputs:

10.50
15.99
20.00
76.54
17.34
you can always export the text to another text file...

or you can use, which isn't recommended...

system("PAUSE");
Last edited on
or you can add a conditional loop statement that takes input from the user to exit or do another test...

Check the post in the beginners forum for a lot of different way to code what you need.
Topic archived. No new replies allowed.