College Lesson Trouble With Count/Sentinel Control Loops
Oct 22, 2012 at 4:20pm Oct 22, 2012 at 4:20pm UTC
Hi,
This is my first post here and I am in need with some help(might need a lot) with a lesson my professor has given me to help me understand loops.
Here is an imgur link to my current homework.
http://i.imgur.com/9Mcja.jpg
As you can see on there I have them listed on which ones are supposed to be sentinel and which ones are supposed to be count control loops.
This is currently what I have:
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
#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;
filein.open("E:\\*****\\datafile2.txt" );
fileout.open("E:\\*****\\answers.txt" );
while (filein)
{
filein>>num;
fileout<<num<<" " ;
}
total=0;
counter=0;
while (filein)
{
filein>>num;
total=total+num;
counter++;
average = total/counter;
fileout<<fixed<<setprecision(3)<<endl<<"The Average from the input file is: " <<average<<endl;
}
}
This class has me stumped currently and I am having a hard time breezing through the class at my instructors speed. So I don't want to get too far behind.
Right now this program is giving me an error that opens CMD and gives me infinite lines of "0's".
Last edited on Oct 30, 2012 at 5:15pm Oct 30, 2012 at 5:15pm UTC
Oct 22, 2012 at 4:28pm Oct 22, 2012 at 4:28pm UTC
First of all instead of
1 2
filein.open("C:\Users\commiedic\Desktop\New folder\datafile2.txt" );
fileout.open("C:\Users\commiedic\Desktop\New folder\answers.txt" );
you shall write
1 2
filein.open("C:\\Users\\commiedic\\Desktop\\New folder\\datafile2.txt" );
fileout.open("C:\\Users\\commiedic\\Desktop\\New folder\\answers.txt" );
Secondly this loop
1 2 3 4 5
filein>>num;
while (num!=999)
{ sum=sum+num;
cout<<fileout;
}
is infinite.
Oct 22, 2012 at 4:55pm Oct 22, 2012 at 4:55pm UTC
Well, to be more accurate, the loop either is infinite or doesn't run at all, depending on user input.
Oct 22, 2012 at 5:36pm Oct 22, 2012 at 5:36pm UTC
Redacted
Last edited on Oct 30, 2012 at 5:11pm Oct 30, 2012 at 5:11pm UTC
Oct 30, 2012 at 5:15pm Oct 30, 2012 at 5:15pm UTC
Ok, after doing some more work I have made my program read and write the document to the correct file. Although now in stage 2 of my lesson I need to make it compute the averages of the number. This although is my current output:
55 67 458 23 81 33 782 375 528 405 324 950 46 14 864 551 38 167 518 630 630
For some reason it isn't computing the average and is giving me the last number twice.
For more information here is the list of numbers I am using and the format they are in on the original file:
1 2 3 4
55 67 458 23 81 33
782 375 528
405 324 950 46
14 864 551 38 167 518 630
Topic archived. No new replies allowed.