HI all;

i'm a new in c++ programing and i have some problem that i don't know how to fix it. i have been trying to fix it for severaldays and i it isn't working.

this is my program which is A file contains 7 numbers per line and there are 8 lines and contains several records. i have to Write a program to input each of the numbers, find the highest number, the lowest number, their total and average. Output the numbers, the highest ,lowest , total and average for each set of 7 numbers to another file.

her is my program in c++;

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

int main()

{
int lowest=0, highest=0, sum=0, num=0, row=0;
int average=0, col=0, newline=0,x=0, y=0;
int input_number=0;

ifstream infile;
ofstream outputfile;

infile.open("f:\\input.txt");
outputfile.open("f:\\output.txt");

if(!infile)
{
cout<<"cannot open the input file."<<endl;
outputfile<<"cannot open the input file."<<endl;

return 0;
}

while (!infile.eof())
{
infile>>input_number;

cout<< input_number<<"\t";
outputfile<< input_number<<"\t";
sum = input_number + sum;
newline++;

for(row=1; row<=7; row++)
{
for(col=1;col<=8;col++)
{
if(newline>=6)
{
do
{
x=input_number;
infile>>input_number;
y=input_number;

if(x>y)
highest=x;
else
highest=y;

if(x<y)
lowest=x;
else
lowest=y;
}while (newline==7);

cout<< input_number<<"\t";
outputfile<< input_number<<"\t";

sum = input_number + sum;

newline++;

cout<<endl;
cout<<"The sum is: "<<sum<<endl;
outputfile<<"The sum is: "<<sum<<endl;
average = sum/ newline;
cout<<"The average is: "<<average<<endl;
outputfile<<"The average is: "<<average<<endl;
cout<<"The highest is: "<<highest<<endl;
outputfile<<"The highest is: "<<highest<<endl;
cout<<"The lowest is: "<<lowest;
outputfile<<"The lowest is: "<<lowest;
cout<<endl<<endl;
newline=0;
sum=0;

}
}
}

outputfile<<endl;
}
infile.close();
outputfile.close();
return 0;

}

3
Contributors
2
Replies
4
Views
11 Years
Discussion Span
Last Post by Lerner

HI all;
this is my program which is A file contains 7 numbers per line and there are 8 lines and contains several records. i have to Write a program to input each of the numbers, find the highest number, the lowest number, their total and average. Output the numbers, the highest ,lowest , total and average for each set of 7 numbers to another file.

if you want to input numbers then why are you trying to open a file in a program.
Make a file name input.txt in your f: drive and add the required data to it....
Here is the code for sum...rest do urself

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

int main()

{
int lowest=0, highest=0, sum=0, num=0, row=0;
int average=0, col=0, newline=0,x=0, y=0;
int input_number=0;

ifstream infile;
ofstream outputfile;

infile.open("c:\\input.txt");
outputfile.open("c:\\output.txt");

if(!infile)
{
cout<<"cannot open the input file."<<endl;
outputfile<<"cannot open the input file."<<endl;

return 0;
}

while (!infile.eof())
{
infile>>input_number;

cout<< input_number<<"\t";
outputfile<< input_number<<"\t";
sum = input_number + sum;
newline++;
}
outputfile<<endl<<"Sum Is:"<<sum;
cout<<endl<<"Sum Is:"<<sum;
infile.close();
outputfile.close();
return 0;
}

If you can try to write your program in skeletal form before even trying to write the code I think you will do better. Using the program description, here's how I might
write out the general outline of what I wanted to do.

declare input file
declare output file
make sure both files open

declare other variables I am going to use and initialize as appropriate

while you can read in first number in a line   //this will fail when there are no further
//lines read.  don't use return value of
//of eof() as a terminating condition.
increment total
repeat the following six times //the file has a fixed format style
input next number from file
increment total
if input number exceeds previous high number found on this line
save it
if input number undercuts previous low number found on this line
save it

when all seven numbers on this line have been read in then
calculate average
send highest, lowest, average for this line to output file
reset all variables to default value
go to next line

determine why input loop stopped
if input stopped because reached end of file
you know you have reached end of file and all numbers read in successfully
clear stream and reset if you are going to use it again, OR
close stream if you are done with it
otherwise
something else happened.  This is harder to figure out.  Maybe the file was
corrupt or the hard drive sneezed.  Who knows.  But it failed, so do something
about, like notify the user and close the program