1. ## How to read the data as a whole string?

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?)

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();
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();
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 said1.)"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?

2. Originally Posted by Sentral
sorry,I don't know your meaning.I don't think I did something wrong!

3. As Sentral noted, try to do it first, then come back with what you have tried.

4. Originally Posted by laserlight
As Sentral noted, try to do it first, then come back with what you have tried.
I have the whole idea,but I need to know how to read the data in a string.This is the beginning point.

5. Originally Posted by tx1988
I have the whole idea,but I need to know how to read the data in a string.This is the beginning point.
After this,I can do the project by myself.

6. There's an imaginatively named function called 'getline'.

7. Originally Posted by Salem
There's an imaginatively named function called 'getline'.
But after that,how do I take the lastname out and sorting for it?since the whole information in one string now.

8. Once you have it in a string, then you can parse it. So what can you do with a string object? How about googling for something like "C++ string" or "C++ class string"?

Also check for ways of extracting data from strings. There's some class in C++ that can do this, similar to sscanf() in C.

9. Originally Posted by MacGyver
Once you have it in a string, then you can parse it. So what can you do with a string object? How about googling for something like "C++ string" or "C++ class string"?

Also check for ways of extracting data from strings. There's some class in C++ that can do this, similar to sscanf() in C.
But I am a very super beginner,and our professor's problem is much harder than the book we used(Problem Solving with C++,6e).And I am only learn C++ 2 months,and have no foundation.I really don't know many functions and class.In fact,we didn't learn class yet!Our professor is just showing his program in class and didn't explain to us very clearly.I can just learn from the book which is much more easier than his lecture.
Ai,GOD!

10. Maybe your professor doesn't know how to teach you, or maybe the fault lies with you. Either way, if you want to get this assignment done, you have to kick it into gear and research this.

Since you say you're a beginner, I hope you take seriously and remember these two things.

1) Experience teaches you better sometimes than just reading a fact from a book or a reference.

2) Programming is not just about writing code. You need to learn how to learn, and be able to meet different challenges as they arise. Important tools: Google, MSDN (for Windows coding), man pages (for *nix coding). If you can't use at least one of those at this point, get cracking and figure it out. It's not so much knowing the answer to a problem as much as knowing how to find out the answer to a problem that will most likely help you later on.

11. I would ditch using getline since every line has the same format and you need to parse it anyway.

I would basically read each line, item by item with the >> operator. Look up ifstream for more inf

Code:
`file >> fname >> lname >> score1 >> score2 >> score3;`

12. Originally Posted by Darryl
I would ditch using getline since every line has the same format and you need to parse it anyway.

I would basically read each line, item by item with the >> operator. Look up ifstream for more inf

Code:
`file >> fname >> lname >> score1 >> score2 >> score3;`
I am doing the same way you did instead of reading into one string.

13. Originally Posted by MacGyver
Maybe your professor doesn't know how to teach you, or maybe the fault lies with you. Either way, if you want to get this assignment done, you have to kick it into gear and research this.

Since you say you're a beginner, I hope you take seriously and remember these two things.

1) Experience teaches you better sometimes than just reading a fact from a book or a reference.

2) Programming is not just about writing code. You need to learn how to learn, and be able to meet different challenges as they arise. Important tools: Google, MSDN (for Windows coding), man pages (for *nix coding). If you can't use at least one of those at this point, get cracking and figure it out. It's not so much knowing the answer to a problem as much as knowing how to find out the answer to a problem that will most likely help you later on.
Thank you,man!I will try to do that and hope I can do better!

14. Another question is how to sorting an string array by their first letter?(I mean how to compare
the first letter of each word?)