Help with calculating sum from integers read from a text file

I have the following code. Everything compiles but when it runs the sums are not correct. I need to calculate the sums for all the odd integers, the even integers, and all the integers.

my infile was provided with the following integers. I am not supposed to assume the infile has 20 integers.

Also, I wanted to the average to calculate with two decimal place precision and that is also incorrect??? setprecision(2) I thought would work???

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cmath>

using namespace std;

int main( )
{
	//Accept user inpput to open file
	ifstream infile;	
	string file_name;
	
	//Set precision
	std::fixed;
	
	//Declare variables
	int i = 0;
	int odd = 0;
	int even = 0;
	int sumOdd = 0;
	int sumEven = 0;
	int sumAll = 0;
	int max = 0;
	int min = 0;
	int myArray[20] = {0};
	double average = 0.00;
	double fileName = 0;
	
	//Set precision
	std::fixed;

	//Displaly and ask for user input
	cout << "Welcome to Wake's Integer processing Agency" << endl;
	cout << "What is the name of the input file? ";
	cin >> file_name;
	
	//Read input from infile
	infile.open(file_name.c_str(),ios::in);
	
	//Read, sort, and calculate
	while (infile >> myArray[i])
	{
		if (myArray[i]%2 != 0)
		{
			odd++;
			sumOdd = odd + myArray[i];
		}
		if (myArray[i]%2 == 0)
		{
			even++;
			sumEven = even + myArray[i];
		}
		if (i == 0)
		{
			max = min = myArray [i];
		}
		if (myArray[i] > max)
			max = myArray[i];
		if (myArray[i] < min)
			min = myArray[i];
		++i;
		
		sumAll = sumOdd + sumEven;
	}
		
	//Display
	cout << "Number of integers in the file is " << i << endl;
	cout << "There are " << odd << " odd integers and " << even << " even integers" << endl;
	
	//Calculate sum of Odd, Even, and All integers
	cout << "The sum of the odd integers is " << sumOdd << endl;
	cout << "The sum of the even integers is " << sumEven << endl;
	cout << "The sum of all the integers is " << sumAll << endl;
	
	//Display largest and smallest integer
	cout << "The largest integer is " << max << endl;
	cout << "The smallest integer is " << min << endl;
	
	//Calculate the average of the largest and smallest integers
	average = (max + min) / 2;
	cout << "The average of " << max << " and " << min << " is " << setprecision(3) << average;
	cout << endl;
	
	//Close
	infile.close();

	system("pause"); 
	return 0;
}
Last edited on
Please use the code tags to make the code more readable. See http://www.cplusplus.com/articles/jEywvCM9/
(You can edit your post.)


Integer divided by integer returns an integer. Storing the result in a double variable occurs "too late".

The setprecision is too late too: You show average before changing the precision of the stream. Set std::fixed property too.


You can use two different strategies:

1. Count things as you read values. You don't need to store any values.

2. Read and store all values first. Then count.
Thank you, this is the first time I've asked for help! I formatted using code tags and also edited my post. I actually put in the wrong code. I have been stuck on this code for days and have written from scratch several times, I have many copies. The above is the most complete. I am still have trouble with the precision even after trying your suggestion???
Lines 49 and 54: what are you computing there?

Line 66 you can do after the loop.

Odd and even are mutually exclusive, so an if else would do on lines 46-55.

Line 17: the fixed manipulator should go to a stream, shouldn't it?

Line 83 is still integer division. Divide with 2.0 to force floating point division.


You do use strategy 1. You do not need an array nor use it after the loop, a mere int variable is enough in its place.
Last edited on
The precision for decimal places is floor(x*100+.005)/100 the precision you used is for place precision ex. 1560 becomes 15 with precision of 2. Another way ot doing this is to use cout << fixed << setprecision << x;
Last edited on
Topic archived. No new replies allowed.