File I/O and While Statement

I have an assignment due at 11:59PM. 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.

This is the assignment. Changed a little (some misspellings added also) so verbatim copy and paste of the assignment can't happen.

Problem: You are to create a program that will read 50 integers from a file sum them together. Output the sum. You will also output how many numbers were odd, and how many were even (zeros are considered even). Your program should use a while_loop to read the values from the file.

The while _loop should check for 2 things.

This is my code so far. It's basically nothing but I'm not even sure where to start at this point. I will stay with this thread so we could go back and forth with posts. I only have 1hr 15min left.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main ()
{
	// Declaring variables
	int sum(0);
	ifstream num_list
	
	num_list.open("Lab_7_Input_files.txt"); 

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

	
	while (

Last edited on
so far so good.
you are going to read in 50 numeric values, so your while loop needs to keep track of how many numbers have been added to the sum.
With files, there is a special value called end of file, designated by fstreamObject.eof(), so the while loop also needs a way to check whether the end of the file has been reached.

As for the even/odd part of the problem, if a given number is even then it is divisible by 2. If it is an odd number, then dividing it by 2 gives a remainder of 1. Since you are asked to output the number of even and odd integers, you need to declare some variables, one to keep track of the number of even integers and one for the odd integers.


Last edited on
Would it look like this:

1
2
3
4
5
while ( !num_list.eof())
	{
		int sum=0
		while (sum <= 50) 
		{


Can a while loop be inside a while loop? I'm not sure how to put a counter and a eof function inside of a while loop. Also what would I do to have the next integer in my file be added to the sum? Does the program automatically do that and the eof function stops the program?
Here is what I was thinking for keeping track of the evens and oods but i don't know how to actually give the number of evens/odds.

1
2
3
4
5
6
7
8
9
if (2%2=0)
	{
		// I'm not how to count and display the number of evens
	}
	else
	{
		// I'm not how to count and display the number of odds 
	}
Last edited on
Can a while loop be inside a while loop?


Yes. Such loops are called nested loops. You can avoid using a nested while loop by having one loop check for both conditions by using the logical 'and' operator.
1
2
3
4
while ( condition_to_be_checked_1 && condition_to_be_checked_2
{
//statements are executed as long as both of the conditions are true
}


Question: Are you using int sum to keep count of how many integers have been processed?
Last edited on
Something like this:

1
2
3
4
5
6
7
8
9
10
while (num <= 50 && num >=0)
          {
        if (num % 2 == 0)
		even = even + num ;
		else
		odd = odd + num;
		}

	cout << "The sum of your even integers is: " << even << endl;
	cout << "The sum of your odd integers is: " << odd << endl;


Remember to initialize your variables.
I used:
1
2
3
4
 int num = 0;
   int sum = 0;
   int even = 0;
   int odd = 0;
Last edited on
Question: Are you using int sum to keep count of how many integers have been processed?


That was my goal. It seems wrong but I'm thinking of a bunch of different things because I got about a half hour left.

Also I wasn't sure if there was restrictions on what kind of loops/functions could be nested.

To determine if a number is even or odd, divide that number by 2. If the remainder is 0, the number is even, if it's 1, the number is odd.
With that in mind, you need a way to divide each number by 2 as it's read into num_list.
1
2
3
4
5
6
7
8
9
10
11
num_list >> number;
if (number%2==0)
{
//current value in number is even, so we increment a variable representing the totat
//number of even integers by one
}
else
{
//it wasn't even, so it must be odd
//increment the odd counter variable by one
}

Since you have to output the number of even and odd integers at the end of the program, you need a variable to keep count of them. Try declaring two more int variables
1
2
int even
int odd
,
Last edited on
Moved code to next post to save space.

I need to actually create the main while that adds each number.

In class the TA said the modulus could just be done by an if statement. Is the while necessary? It seems like it would be.
Last edited on
The compiler doesn't care what name you give to a variable (most of the time), but to make the code easier for human readers to understand, it's beneficial to use descriptive names.
1
2
3
4
int sum; //'sum' holds the sum of all integers read into num_list so far
int count; //counts how many numbers have been read into num_list
int even; // keeps count of the total number of even integers
int odd; //ditto for the odds 



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), evens(0), odds(0), num(0);

	
	num_list.open("Lab_7_Input_files.txt"); 

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

	
	while ( !num_list.eof())
	{
		number=0;  //remove this, you already declared and initialized number
		while (number <= 50) //remove this while loop and merge the condition
		{                                 //with the other one
		}                                 // i.e. while(number<=50 && !num_list.eof())
	}
			
	
	
	num_list >> num;	//from here to the end of the if..else statement should be
                                                //inside the first while loop	
	while (num >= 0 && num <= 50)  //remove this while loop
	{
		if (num % 2 == 0)
		{
		evens = evens + num; //change this to evens++;
		}
		else
		{
		odds = odds + num; //change this to odds++;
		}
	}

	cout << "The file contains " << evens << "even numbers.\n";
	cout << "The file contains " << odds << "odd numbers.\n";
		
	return 0;
}
 
Last edited on
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
#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);

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


How does the eof function look? Does the counter go in the eof function? How do I calculate the total sum?
Last edited on
Can anybody fix my mistakes and post the code. I hate saying that but I messed up on some labs already and I really need the points.
How does the eof function look? Does the counter go in the eof function? How do I calculate the total sum?


There are several special 'hidden' values (i.e. you rarely see them displayed anywhere) that are returned by istream variables, and eof is one of them. When the end of a file is reached, eof is set and the ifstream stops reading in values.

To calculate the sum, you need to add each number read into num_list to sum.
1
2
num_list >> number;
sum += number;
Last edited on
I did sum += num_list and a bunch of errors popped up. Is num_list just a naming thing? Can I not do use it for calculations?
Heres some psuedocode to demonstrate how this should be solved:

1
2
3
4
5
6
7
8
9
10
11
12
13
int even(0), odd(0), sum(0);
FILE file("file.txt");

LOOP (50x OR file.end)
    int tmp = file.read();
    ADD tmp to sum
    if (tmp%2==0)// even
        Add 1 to even
    else
        ADD 1 to odd
ENDLOOP

DISPLAY even, odd, sum
Well the deadline has passed so now I actually have time to go over the concepts and learn. Grade probably won't be good for this assignment. Thanks to everybody that helped.
Topic archived. No new replies allowed.