File I/O and While Statement

I have an assignment due at 8:00PM. I've had a ton of work to do this week and haven't really got to learn the topic before the assignment. I'm just trying to get the points for the assignment and learn the stuff after its due.

Problem:

You are to create a program that will read 50 integers from a file and sum them together. Output this sum to an output file. You will also output (to the file) how many numbers were odd, and how many were even (zeros may be considered even). Your program MUST employ a while loop to read the values from the file.

The while loop should check for TWO things (what are they?). Hint: you know you need to read 50 numbers, but for correct file processing, what else should you check for? Your grade will be based on whether or not you correctly identify both conditions to be used in the while loop.

here's my code so far:

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
#include <iostream>		// Preprocessor directive for cin and cout
#include <fstream>
using namespace std;

int main ()
{
	// Declaring variables
	ifstream num_list;
	int number(0), even(0), odd(0), count(0);
    int sum, num;

	num_list.open("Lab_7_Input_files.txt");

	if (num_list.fail())
	{
		cout << "Error opening file\n";
	}


	while ( !num_list.eof())
	{
		count=0;
		while (count <= 50)
		{

			sum += num_list;
		}
	}



	num_list >> number;
	while (number >= 0 && number <= 50)
	{
		if (num % 2 == 0)
		{
		even = even + number;
		}
		else
		{
		odd = odd + number;
		}
	}

	cout << "The file contains " << even << "even numbers.\n";
	cout << "The file contains " << odd << "odd numbers.\n";

	return 0;
}


I am getting error: no match for 'operator+=' in 'sum += num_list'

I have been trying for hours, but can't seem to get it just right. I hate to ask this, but if someone could fix this and post it that would be great, as I really need the points.

Thanks in advance if anyone tries to help!
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
   	count=0;
	while ( !num_list.eof())
	{
		
		
		while (count <= 50)
		{
                        num_list >> number;
			sum += number;
			count++;
		}
	}
Last edited on
Thank you for the help! It is very much appreciated.

The program runs now, however, nothing is being displayed (empty black box).
1
2
3
4
5
6
7
8
9
10
11
12
13
	while ( !num_list.eof())
	{
		while (count <= 50)
		{
                        num_list >> number;
			sum += number;
			if (number % 2 == 0)
				even++;
			else
				odd ++;
			count++;
		}
	}
Last edited on
Technically, you should have
if (num_list >> number)
and then execute those lines, else cout << "Non-numeric input" or some other error exception. Also, what if there are fewer than 50 numbers? Remove the while (count <= 50), and use if (count == 50) break; and if eof is reached output whether count < 50 or not. I think those are the two things you should check for.
Don't read a file like this:

1
2
3
4
5
while (!num_list.eof())
{
     num_list >> number;
     //other code
}


The reason is that when num_list >> number reaches eof, the rest of your loop code still executes until the next test of the while condition, and you are guaranteed invalid data.

This is how it's commonly done:

1
2
3
4
while(num_list >> number)
{
     //code to do something with number, etc.
}


Your loop condition could look something like this: while(num_list >> number && ++count <= 50).
Last edited on
Topic archived. No new replies allowed.