let me start with the assignment so you can see what I am suposed to do.
1. Use sentinel, nested and EOF loops to do file I/O.
2. Create a program file to read a data file and find the section number(first four digits) in a sequence of data(each line).
3. Then find the maximum, minimum and average of each section.
4. Then store these results in an output file.
5. create an outter loop that uses an end of file loop to find the average of each section. an store it in the same file.
I am stuck on the last part I cant figure out how to make the file stay in the outer loop (EOF) long enough to take all of the data. I think that I am writing the end of file loop wrong, any advice?
my code so far.
cin>>currchar
if (currchar > maxchar)
maxchar = currchar;
else if (currchar < minchar)
minchar = currchar;
else
currchar = 0
return 0
}
while (number > 0)
{
count = 0; //Quantity of values
sum = 0; //Total of values
positive = true; //Loop control flag
while (positive) //Stop for end value
{
cin>>number
if (number < 0)
positive = false;
else sum = sum + number;
count++ //increase count
}
average = sum / count;
cout<<maxchar<<minchar<<average<<setprecision(2)<<endl
return 0
}
if (!infile)
{
infile.close
ifstream outfile;
counto = 0;
numbero.substr(26,5);
sumo = sumo + numbero;
counto++
}
averageo = sumo / counto;
cout>>"Average for all sections">>averageo>>endl;
}
--------------------------------------
and the data (scores.dat)
ok this is what I have now after taking the weekend to work on it.
and there are no compiling errors but when I go to open the program file nothing happens am I doing something wrong?
using namespace std;
int main()
{
string Section;
ifstream infile; // input file
ofstream outfile; // output file
int count; // number of vlaues on the line
float sum; // sum of numbers on line
int number; // number on line
float average; // Average of line
int currvalue; //Current data value
int minvalue; //Minimum data value
int maxvalue; //Max data value
float sumo; //Sum of averages
float averageo; //average of averages
float numbero; //single average to total
int counto; //number of averages
string section; // Section number
bool positive;
infile.open("scores.txt");
if (!infile) //Test the file
{
{cout<<"Cannot open inputfile."; //Failure
system ("pause");
return 1;
}
outfile.open("out.txt");
if (!outfile) //Test the file
{
cout<<"Cannot open outputfile."; //Failure
system ("pause");
return 1;
}
while (currvalue > 0)
{
counto = 0;
sumo = 0;
while(infile)
{
cin>>currvalue;
if (currvalue > maxvalue)
maxvalue = currvalue;
else if (currvalue < minvalue)
minvalue = currvalue;
else
currvalue = 0;
return 0;
}
while (number > 0)
{
count = 0; //Quantity of values
sum = 0; //Total of values
positive = true; //Loop control flag
while (positive) //Stop for end value
{
cin>>number;
if (number < 0)
positive = false;
else sum = sum + number;
count++; //increase count
}
average = sum / count;
cout<<maxvalue<<minvalue<<average<<setprecision(2)<<endl;
return 0;
}
sumo = sumo + numbero;
counto++;
averageo = sumo / counto;
cout << "Average for all sections " << averageo << endl;
}
}
}