Input and Output file

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;

for (j=0; j<row; j++)
{for (i=0;i<col; i++)
{outfile<<" "<<num<<" ";
partial_sum += num;
infile >> num;
if (num > highest)
highest = num;
if (num < lowest)
lowest = num;
}

outfile<<num<<endl;
sum = partial_sum + num;

average = sum/total_number_per_line;
outfile << "\t Highest number: " << highest<<"\t\t";
outfile << "\t Lowest number: " << lowest<<endl;
outfile << "\t Total: "<<sum<<"\t\t\t";
outfile << "\t The average: " << average << endl;
outfile<<"\n";

infile >> num;

// Reinitialize all variable in order to start the new row.
partial_sum = 0;
highest = num;
lowest = num;
}

infile.close(); // Close the input file
outfile.close (); // Closing the output file
system("pause");
}

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)

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
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;
      }
      else if ( number < lowest )
      {
         lowest = number;
      }
      else if ( highest < number )
      {
         highest = numbber;
      }

      count++; sum += number;
   }

   if ( count != 0 ) average = sum / count;

   // write result values in the output file
}
   
Last edited on
Thanks so much. But Is there any way else of doing this? Cus i just study "if ", for loops, while loops and do while.
Since you're not using anything that requires a const just use non-const variables if that's what's required.

Jlb: if so, how can the computer know how many row, col and even the total number per line?
You either supply the value by assignment, as you currently are, without the const qualifier, or you ask the user for this information.

Another way would be to read the entire line at a time, then use a stringstream to process that string.

Jim

Topic archived. No new replies allowed.