Oct 20, 2012 at 5:08am UTC
I am doing a input/output program for homework for one of my classes from my c++ book. Our teacher just want us to change the output into columns instead of rows.
Input File:
Andrew Miller 87.50 89 65.75 37 98.50
Here is the problem.
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
using namespace std;
int main()
{
ifstream inFile;
ofstream outFile;
double test1, test2, test3, test4, test5, average;
string firstName;
string lastName;
inFile.open("test.txt");
outFile.open("testavg.txt");
if(!inFile){
cout<<"Input failure\n";
system("pause");
return 1;
}
if(!outFile){
cout<<"Output failure\n";
system("pause");
return 1;
}
outFile<<fixed<<showpoint;
outFile<< setprecision(2);
cout<<"Processing data"<< endl;
inFile>>firstName>>lastName;
outFile<< "Student Name: " << firstName << " "<< lastName <<endl;
inFile >> test1 >> test2 >> test3
>>test4 >>test5;
outFile<< "Test scores: "<< setw(6) << test1
<<setw(6)<<test2<<setw(6)<<test3
<<setw(6)<<test4<<setw(6)<<test5<<endl;
average = (test1+test2+test3+test4+test5)/5.0;
outFile<< "Average Test Score: "<< setw(6)
<< average << endl;
inFile.close();
outFile.close();
system("pause");
return 0;
}
When I execute the program, it is in rows like this.
Output file looks like this:
Student Name Andrew Miller
Test Scores 87.50 89.00 65.75 37.00 98.50
Average Test Scores: 75.55
How do I get it to look like this:
Student Name Test Scores Avg Test Scores
Andrew Miller 87.50,89,65.75,37.00,98.50 75.55
In other words, how do I line it up to be column like instead of being in a row?
Any help would be appreciated.
Thank You