How to read the data as a whole string?
Quote:
Whitewater High School has contracted you to write a program that will
read a file of student names and test scores in the formation FIRST
NAME LAST NAME: SCORE1 SCORE2 SCORE3, where each of the scores is an
integer. Then your program is to print a report, sorted by last name
with each of the last names fully capitalized (that is, if you read in
Wilkinson you print WILKINSON) followed by a comma, then the first
name, then a colon, then the scores, then the average for the student,
and lastly a letter grade (based upon 90 percent or greater being an
"A," 80 percent or greater being a "B," and so forth). At the end of
the report the averages for each test are to be printed also. Needless
to say, the report must be written to a file and must be "pretty" (in
proper columns and such). (HINT: Read in the whole line as a string,
switch the first and last name, sort the names, then compute the math)
Data file: data.txt
The data file content is:
Mary Jones: 89 90 100
Tom Brown: 100 99 100
John Smith:66 80 98
Englebert Humberdink: 85 87 88
Tom Jones: 76 78 89
Paul McCartney: 88 88 99
Olivia Newton: 77 66 98
Susan Barlow: 87 98 89
Robert Payne: 88 44 76
George Franklin: 77 88 99
Margaret Ibach: 87 89 90
Maggie Chang: 99 99 100
Blaire Bates: 89 87 78
John Lennon: 88 77 99
Silvia Stalone: 66 55 88
Pepper Johnson: 90 80 99
Alfred Newman: 80 80 90
Hugh Walker: 56 90 70
Maureen Ferguson: 100 90 70
Cho Zhang: 99 99 99
Lisa Graham: 80 87 78
Paul Revere: 89 78 90
Franklin Smythe: 88 88 100
Mark Anderson: 100 90 80
Toni Interbitzen: 90 90 90
My question is how to read the data as a whole string?you know for each line,there are firstname,lastname,and 3 scores.They are different type,how could do that?
And also if I do this,how do I switch the firstname and lastname,and how to compute the ave score?(How do I take the 3 scores out of the whole string?)
OK,guys,I already finish my code but it also has some strange errors.Please help me to fix.
Code:
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string>
#include <iomanip>
using namespace std;
void loadData(string firstname[],string lastname[],int score1[],int score2[],int score3[])
{
int i=0;
ifstream in;
in.open("data.txt");
if(in.fail())
{
in.close();
cout<<"Data.txt not found,program ending!"<<endl;
exit(1);
}
while(in >> firstname[i] >> lastname[i] >> score1[i] >> score2[i] >> score3[i])
i++;
}
void ChangeLastname(string &lastname)
{
for(size_t i=0;i<lastname.length();i++)
{
lastname[i] = toupper(lastname[i]);
}
}
void sorting(string lastname[],string firstname[],int score1[],int score2[],int score3[])
{
for(int i=0;i<25;i++)
{
for(int j=0; j<25; j++)
{ { if(lastname[i] < lastname[j])
swapValues2(lastname[i],lastname[j]);
swapValues2(firstname[i],firstname[j]);
swapValues(score1[i],score1[j]);
swapValues(score2[i],score2[j]);
swapValues(score3[i],score3[j]); }
}
}
void swapValues2(string& v1,string& v2)
{
string temp;
temp=v1;
v1=v2;
v2=temp;
}
void swapValues(int& v1,int& v2)
{
int temp;
temp=v1;
v1=v2;
v2=temp;
}
void grade(int score1[],int score2[],int score3[],int ave[],int aveScore[],int sum[],string rank[])
{
for(int i=0;i<25;i++)
{ ave[i]=(score1[i]+score2[i]+score3[i])/3;
if(ave[i]>=90)
rank[i]="A";
else if(ave[i]<90&&ave[i]>=80)
rank[i]="B";
else if(ave[i]<80&&ave[i]>=70)
rank[i]="C";
else if(ave[i]<70&&ave[i]>=60)
rank[i]="D";
sum[0]+=score1[i];
sum[1]+=score2[i];
sum[2]+=score3[i];}
for(int j=0;j<3;j++)
aveScore[j]=sum[j]/25 }
void WriteToFile(string lastname[],string firstname[],int score1[],int score2[],int score3[],int score[],int aveScore[],string rank[])
{
int i=0,j=0;
ofstream out;
out.open("data2.txt")
if(out.fail())
{
out.close();
cout<<"data2.txt not found,program ending!"<<endl;
exit(1);
}
out<<setw(20)<<"NAME"<<setw(4)<<"SCORE1"<<setw(4)<<"SCORE2"<<setw(4)<<"SCORE3"<<setw(4)<<"RANK";
while(out<<setw(20)<<lastname[i]<<","<<firstname[i]<<":"<<setw(4)<<score1[i]<<setw(4)<<score2[i]<<setw(4)<<score3[i]<<setw(4)<<rank[i]<<endl)
i++;
out<<setw(20)<<"TEST AVERAGE"<<setw(4)<<aveScore[0]<<setw(4)<<aveScore[1]<<setw(4)<<aveScore[2]<<endl;}
int main(int argc, char *argv[])
{
string firstname[25], lastname[25];
int score1[25], score2[25], score3[25],aveScore;
loadData(string firstname[],string lastname[],int score1[],int score2[],int score3[]);
ChangeLastname(lastname);
sorting(string lastname[],string firstname[],int score1[],int score2[],int score3[]);
grade(int score1[],int score2[],int score3[],int score[],int aveScore[],int sum[]);
writeToFile(string lastname[],string firstname[],int score1[],int score2[],int score3[],int score[],int aveScore[],string rank[]);
system("pause");
return 0;
}
the complier said:(1.)"implicit declaration of function `swapValues2(...)'"and also for swapValues.
(2.)"temp,v1,v2"undeclared and also"
(3.) warning: cannot pass objects of type `basic_string<char,string_char_traits<char>,__defa ult_alloc_template<false,0> >' through `...'".
for(1)and(3),I really don't know how to do.But for(2),That is really Great!I am sure I declared them,but why?