I have to read a text file and then output the information from that text file. I have gotten the file to be read, but I need to format it under a heading using iomanip, then I need to take the data from the file and create a letter grade from it. I am looking for hints not a complete answer.
You didn't give an example of what the data looks like. I am assuming that each line has four fields per line -- with each field being a score. If so,
1 2 3 4 5 6 7
For each line
Parse the fields and convert the data in the field to a double and assign it to one of your
doubles (scr1, scr2, scr3, or scr4).
Add up all four fields and assign the sum to g.
Divide g by four and assign this value to avg.
Assign a letter grade to grade based upon avg.
Using iomanip, output each value under the appropriate column.
I am confused on how exactly I can separate the fields, if I can get each line divided into fields for the score variables I think I can get the, grade, the negative argument and formatting done.
Part of the confusion I think is that this is a continuation of a assignment from last week where instead of having a file to input, I had the user directly input the data. Is there any way to reuse the old file?
So I have each line being read into a array now, how do I get each field to be a array so I can convert it into a integer so I can sum each line and divide? Thanks for your patience I am not very good at this.
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cmath>
usingnamespace std;
int main ()
{
string array[7]; // creates array to hold names
string sum,g;
short loop=0; //short for loop for input
cout << std::right << setw(8) << "score1" << setw(8) << std::right << "score2" << std::right << setw(8) << "score3" << std::right << setw(8)
<< "score4" << std::right << setw(8) << "average" << std::right << setw(8) << "grade" <<endl;
string line; //this will contain the data read from the file
ifstream myfile ("c:\\temp\\grades.txt"); //opening the file.
if (myfile.is_open()) //if the file is open
{
while (! myfile.eof() ) //while the end of file is NOT reached
{
getline (myfile,line); //get one line from the file
array[loop] = line;
sum=line;
cout << array[loop] << endl; //and output it
loop++;
}
system("pause");
exit(0);
}
}
Don't use "getline(...)" use the extraction operator ">>" when you get the data from the file. Also you should read them into double values so that you don't have to cast them later. So from Line 16 on:
//Line 10:
double array[7]; //Changed Type On Your Array
//...
//Line 16:
double line; //Changed The Type On Your Variable
ifstream myfile("c:\\temp\\grades.txt", ios_base::in); // What You Have Is Fine
if(!myfile.is_open()) //Slight Change In Style
{
cout << "Could Not Open File\n"; //Error Message To User
cin.ignore(); //Hold Window Open While User Reads Message
return 1; //Return 1 To OS Indicating Failure At First Error Check Point
} //End Of If
while(myfile >> line) //While Valid Data Is Being Read From File
{
array[loop] = line;
sum = line; //What Does "sum" Do?
cout << array[loop] << endl; //This Is Fine
loop++; //This Is Right
}