file output

I have this data

1 1000.25
2 55.25
3 9999.99
4 33.45
5 2000.00
6 1588.88
7 1699.99
8 14898.25
9 13734.21
10 13523.24

i need to find the max, min and average between those and output the results on a file, at the end my output file gives me everything 0.
what am i doing wrong?

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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
using namespace std;

int main(){

	string filename = "prices.dat";   
	ofstream outFile;
	int counter = 0, numItems, id;
	double sum = 0, average, max = 0, min = 0, price;

	
	ifstream inFile(filename);

	if (!inFile)
	{
		cout<< "Input file opening failed.\n";
		exit(1);
	}

	inFile >>  numItems;
	cout.precision(2);
	cout.setf(ios::fixed);

	while(inFile >> id >> price){
		cout << setw(3) << id << " " << setw(10) << price << '\n';
		counter++;
	}


	outFile.open("output.dat");

	while (inFile >> price)
	{
		if (price > max)
		     max = price;
		
		if (price < min)
		    min = price;
            sum = sum + price;
	}

    average = sum / counter;
	
	outFile << "Number of price readings: "<< counter << endl;
	outFile << "Maximum price: " << max << endl;
	outFile << "Minumum price: " << min << endl;
	outFile << "Average price: " << average << endl;
	
	
	inFile.close();
	outFile.close();
}
After the loop on line 27, the input file will be in a failed state (the loop exits only when input has failed).
We can't read anything more from the input stream unless we
a. clear the failed state b. seek to the beginning of the stream.

For this program, one pass through the input file would be sufficient, For example:

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
#include <iostream>
#include <fstream>
#include <iomanip>

int main() {

    const std::string input_filename = "prices.dat";
    const std::string output_filename = "output.dat";

    std::ifstream in_file(input_filename) ;
    if( !in_file.is_open() ) { /* report error: can't open input file */ return 1 ; }

    int num_items, id ;
    double price ;

    // if at least one price was successfully read
    if( in_file >> num_items && num_items > 0 && in_file >> id >> price ) {

        std::cout << std::setw(3) << id << ' ' << std::setw(10) << price << '\n';

        double sum = price, min = price, max = price ;
        int cnt = 1 ;

        while( cnt < num_items && in_file >> id >> price ) {

            std::cout << std::setw(3) << id << ' ' << std::setw(10) << price << '\n';

            ++cnt ;
            sum += price ;
            if( price < min ) min = price ;
            else if( price > max ) max = price ;
        }

        const double average = sum / cnt ;

        std::ofstream(output_filename) << std::fixed << std::setprecision(2)
                                    << "Number of price readings: "<< cnt << '\n'
                                    << "Maximum price: " << max << '\n'
                                    << "Minumum price: " << min << '\n'
                                    << "Average price: " << average << '\n' ;
    }

    else { /* report error in reading from input file */ return 1 ; }
}
Last edited on
Topic archived. No new replies allowed.