
October 31st, 2012, 10:47 AM
#16
Re: College Lesson Trouble With Count/Sentinel Control Loops
Originally Posted by GCDEF
I hadn't read the original assignment about performing the tasks separately, but he shows you what you need to do. You will need multiple loops closing and reopening the file between them.
I've answered your question about repeating the last number several times now. You were on the right track earlier when you read the file before your loop, but you need to put the output before the input in the loop.
Ok I got it =P
Code:
total=0;
counter=0;
filein>>num;
while(filein)
{
fileout<<num<<" ";
filein>>num;
total=total+num;
counter++;
}
average = total/counter;
fileout<<fixed<<setprecision(3)<<endl<<"The Average from the input file is: "<<average<<endl;
Chapter 8 assignment is where we do multiple loops using void functions... So I guess I need to do a separate print out for each one.

October 31st, 2012, 10:57 AM
#17
Re: College Lesson Trouble With Count/Sentinel Control Loops
Looks like all the steps, a, b, c, d... he asks you to "reread" the file, which would require separate loops and closing and opening the file between each loop.

October 31st, 2012, 11:00 AM
#18
Re: College Lesson Trouble With Count/Sentinel Control Loops
concerning reading last value twice ... you have
Code:
while (filein)
{
filein>>num;
fileout<<num<<" ";
total=total+num;
counter++;
}
You are reading the last value, and adding to total (so far so good).
Then it goes back to the start of the loop : while (filein)
This still will result in "true", so you enter the body of the loop again.
generally, you should loop as follows:
Code:
while (filein >> num)
{
total += num;
++counter;
}
Last edited by Philip Nicoletti; October 31st, 2012 at 11:02 AM.

October 31st, 2012, 11:07 AM
#19
Re: College Lesson Trouble With Count/Sentinel Control Loops
Whoa... look at me go lol Thanks!
Code:
#include <iostream>
#include <cmath>
#include <iomanip>
#include <fstream>
using namespace std;
void main()
{
int num, fnum, lnum, sum;
float average, total, counter;
char inchar;
ofstream fileout;
ifstream filein;
fileout.open("E:\\Resource Files\\answers.txt");
filein.open("E:\\Resource Files\\datafile2.txt");
total=0;
counter=0;
filein>>num;
while(filein)
{
fileout<<num<<" ";
filein>>num;
total=total+num;
counter++;
}
average = total/counter;
fileout<<fixed<<setprecision(3)<<endl<<"The Average from the input file is: "<<average<<endl;
filein.close();
filein.clear();
filein.open("E:\\Resource Files\\datafile2.txt");
total=0;
counter=1;
while(counter <=12)
{
filein>>num;
total=total+num;
counter++;
}
average=total/12;
fileout<<"The average of the first 12 integers of the input file is: "<<average<<endl;
}
Code:
55 67 458 23 81 33 782 375 528 405 324 950 46 14 864 551 38 167 518 630
The Average from the input file is: 374.200
The average of the first 12 integers of the input file is: 340.083

October 31st, 2012, 11:12 AM
#20
Re: College Lesson Trouble With Count/Sentinel Control Loops
Ok, Now I had friend give me a hint on this next piece for reading every 3rd integer in the input file, but I don't know how to decipher it.
if(num%3==0)
I get that I will need an "if" statement, but what does "num%3==0" mean?!

October 31st, 2012, 11:17 AM
#21
Re: College Lesson Trouble With Count/Sentinel Control Loops
Remember elementary school division? 5 divided by 3 = 1 remainder 2, for example. The modulo operator gives you the remainder, so 5 % 3 would return 2. If it equals zero, the numerator is evenly divisible by the denominator.

October 31st, 2012, 11:20 AM
#22
Re: College Lesson Trouble With Count/Sentinel Control Loops
Originally Posted by GCDEF
Remember elementary school division? 5 divided by 3 = 1 remainder 2, for example. The modulo operator gives you the remainder, so 5 % 3 would return 2. If it equals zero, the numerator is evenly divisible by the denominator.
Ok, so the if statements expression is only true if the remainder of the num%3 equals 0. So if there is no remainder than it is a true statement.

October 31st, 2012, 11:27 AM
#23
Re: College Lesson Trouble With Count/Sentinel Control Loops
Pretty sure I got the code right, but I am coming up with a different answer than what I was expecting
Code:
55 67 458 23 81 33 782 375 528 405 324 950 46 14 864 551 38 167 518 630
The Average from the input file is: 374.200
The average of the first 12 integers of the input file is: 340.083
The sum of every third number is: 3870.000
Code:
filein.close();
filein.clear();
filein.open("E:\\Resource Files\\datafile2.txt");
total=0;
while(filein)
{
filein>>num;
if(num%3==0)
total=total+num;
}
fileout<<"The sum of every third number is: "<<total<<endl;
}
From adding every 3rd number I am getting 3000 as my answer.

October 31st, 2012, 11:27 AM
#24
Re: College Lesson Trouble With Count/Sentinel Control Loops
Originally Posted by commiedic
Ok, so the if statements expression is only true if the remainder of the num%3 equals 0. So if there is no remainder than it is a true statement.
Yes. Typically C and C++ programmers start counters at 0, so if your loop included if(counter % 3) == 0) then every third time through the loop, beginning with the first time, would be true.

October 31st, 2012, 11:29 AM
#25
Re: College Lesson Trouble With Count/Sentinel Control Loops
Originally Posted by commiedic
Pretty sure I got the code right, but I am coming up with a different answer than what I was expecting
Code:
55 67 458 23 81 33 782 375 528 405 324 950 46 14 864 551 38 167 518 630
The Average from the input file is: 374.200
The average of the first 12 integers of the input file is: 340.083
The sum of every third number is: 3870.000
Code:
filein.close();
filein.clear();
filein.open("E:\\Resource Files\\datafile2.txt");
total=0;
while(filein)
{
filein>>num;
if(num%3==0)
total=total+num;
}
fileout<<"The sum of every third number is: "<<total<<endl;
}
From adding every 3rd number I am getting 3000 as my answer.
You really need to think about what you're doing. num could be anything. num isn't the value you want to be testing here.

October 31st, 2012, 12:25 PM
#26
Re: College Lesson Trouble With Count/Sentinel Control Loops
Originally Posted by commiedic
Ok I got it =P
Code:
total=0;
counter=0;
filein>>num;
while(filein)
{
fileout<<num<<" ";
filein>>num;
total=total+num;
counter++;
}
average = total/counter;
fileout<<fixed<<setprecision(3)<<endl<<"The Average from the input file is: "<<average<<endl;
Chapter 8 assignment is where we do multiple loops using void functions... So I guess I need to do a separate print out for each one.
Would an eof() function have worked here as well?

October 31st, 2012, 12:28 PM
#27
Re: College Lesson Trouble With Count/Sentinel Control Loops
Before worrying about every third number, you should get
the very first part correct: getting the average of all the numbers
in the file.
You have 2 ways of doing that:
1) Take away the read outside of the while loop, and use the while
loop that I posted. (best method)
2) keep you loop as is ... but inside the loop check the state
of the stream. If the stream's state is false, do not add num to total.
l
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
This is a Codeguru.com survey!
