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 (
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.
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?
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?
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;
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
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
#include <iostream> // Preprocessor directive for cin and cout
#include <fstream>
usingnamespace 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;
}
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.
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.