i'm trying to figure out exactly how data is displayed from files. i've read all the pages on here about it but it's still not totally making sense. i have an assignment where a user enters a file destination, the program echoes the data, and then computes 4 different types of averages from the data.
i know i need to do some loops in there, but i don't know which kind would make the most sense, or how to code this. the teacher isn't much help at all. my code so far is:
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
usingnamespace std;
constint COL_WID = 10;
int main()
{
ifstream in;
ofstream on;
string filePath;
string names;
char yesNo;
int age;
double score;
cout << endl << "Please enter the file address: ";
cin >> filePath;
in.open(filePath.c_str());
if(!in)
{
cerr << endl << filePath << " is not a valid directory."
<< endl << endl;
system("pause");
exit(1);
}
in >> names;
while(!in.eof())
{
in >> yesNo;
in >> age;
in >> score;
//data processing
cout << setw(COL_WID) << names << setw(COL_WID) << yesNo << setw(COL_WID) << age << setw(COL_WID) << score << endl;
in >> names;
}
in.close();
return 0;
}
as you can see i'm stuck after the data echo. can i use the istream variables ("in") to manipulate the data or the regular variable declared at the beginning of int main?
for reference, the data file it is echoing is set up like:
NAME Y/N AGE SCORE
and the loops for the averages i need to computer are something like
1 2 3 4 5 6 7 8 9 10 11 12
if(yesNo == 'Y'){
if(age >= 18)
//find average
elseif(age < 18)
//find average
}
if(yesNo == 'N'){
if(age >= 18)
//find average
elseif(age < 18)
//find average
}