Please help me..
My professor doesn't let me declare row, col and total_number_per_line as a constant. I spent almost 4 hours to figure out how to do it but i can't. Anyone please help me??? I really appreciate your help.
Assignment
Plan and code a program utilizing one file for input and one file for output to solve the following problem:
A file contains 7 numbers per line and contains several records. 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.
Data File
346 130 982 90 656 117 595
415 948 126 4 558 571 87
42 360 412 721 463 47 119
441 190 985 214 509 2 571
77 81 681 651 995 93 74
310 9 995 561 92 14 288
466 664 892 8 766 34 639
151 64 98 813 67 834 369
Here is my code
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
// Delare variable
ifstream infile;
ofstream outfile;
int i,j;
int const row=8; int const col=6;
int const numbers_per_line=7;
double num,highest, lowest,sum,average;
double partial_sum = 0;
infile.open("F:\\Co Sc 575\\Lab5\\datafile.txt");
if (!infile)
{ cout<<"Cannot open the file."; //there was an error on open, file not found
cout<<"Program terminates!"<<endl;
}
cout<<" Please open your output file !\n\n"<<endl;
outfile.open ("F:\\Co Sc 575\\Lab5\\newDatafile.txt"); //create new file for output in the same path F
infile >> num; // Input the first number of datafile
// Set the first number is both highest and lowest number in order to compair with the next input number
highest = num;
lowest = num;
I think that the professor means that you should not suppose that each line of the input file contains exactly 7 numbers and that the file has 8 lines.
So it is better to read the input file line by line. Something as (without testing)
while ( std::getline( infile, line ) )
{
if ( line.erase( 0, s.find_first_not_of( " \t", 0) ).empty() ) continue;
std::istringstream( line ) is;
int lowest = 0, highest = 0;
int sum = 0, average = 0;
size_t count = 0;
int number = 0;
while ( is >> number )
{
if ( count == 0 )
{
lowest = highest = number;
}
elseif ( number < lowest )
{
lowest = number;
}
elseif ( highest < number )
{
highest = numbber;
}
count++; sum += number;
}
if ( count != 0 ) average = sum / count;
// write result values in the output file
}