GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 28-May-2007, 13:07
blue1988tx blue1988tx is offline
New Member
 
Join Date: May 2007
Posts: 5
blue1988tx is on a distinguished road
Question

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?)
  #2  
Old 28-May-2007, 13:27
ahbi82 ahbi82 is offline
Member
 
Join Date: Jul 2006
Posts: 114
ahbi82 will become famous soon enough

Re: How to read the data as a whole string?


Quote:
Originally Posted by blue1988tx
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?)


You can read up reference API for
CPP / C++ / C Code:
fscanf 
to read individual fields. Or you can you can use
CPP / C++ / C Code:
fgets 
to read the whole string on a line. And use the string tokenizer method to get the indivual fields.

Hope this helps!
  #3  
Old 28-May-2007, 14:19
Jednorozec Jednorozec is offline
New Member
 
Join Date: May 2007
Location: NY
Posts: 11
Jednorozec is on a distinguished road

Re: How to read the data as a whole string?


The reason for homework is to get you to think not to try to get someone to do it for you. What are you going to do when you get a job?
  #4  
Old 28-May-2007, 16:02
blue1988tx blue1988tx is offline
New Member
 
Join Date: May 2007
Posts: 5
blue1988tx is on a distinguished road
Unhappy

Re: How to read the data as a whole string?


Quote:
Originally Posted by Jednorozec
The reason for homework is to get you to think not to try to get someone to do it for you. What are you going to do when you get a job?

I am trying to do it,but I really don't know how to do.I think I put my question here is just the same as I ask it to my professor,the point is you can get the right answer no matter whom you asked.I think I am doing right and I am working hard on it.
Let me ask you:when you are in trouble,do you always just try it by yourself even you can't finish it without anybody's help and never ask for help?I am not cheating,and I am not let you to do it for me.I just want to get some hint!
Even I am getting my job,if I am in trouble,I will ask for help.I don't think that is so bad,by this way,you can get more and more experience and at last you can do your programming by yourself.Can you say you never ask for help in your programming learning?
  #5  
Old 28-May-2007, 16:19
ahbi82 ahbi82 is offline
Member
 
Join Date: Jul 2006
Posts: 114
ahbi82 will become famous soon enough

Re: How to read the data as a whole string?


To save ou some time in searching. I have provided links to the reference API.

fscanf
http://www.cplusplus.com/reference/c...io/fscanf.html

fgets
http://www.cplusplus.com/reference/c...dio/fgets.html

fread
http://www.cplusplus.com/reference/c...dio/fread.html

Now its up to you to determine which is better for u.
I would reckon you try both fscanf and fgets. One difference is the delimiter and how they get the values from the buffer. If your data is multi-format( eg. %s, %d, %f), fscanf would be better as lesser code is needed to convert from char to integer, double etc.

Another point to take note is that if the number of entries. This will determine whether to use a for loop ofr while loop. I believe all this will be delivered in your assignment specifications.

Hope this helps.
  #6  
Old 28-May-2007, 16:43
blue1988tx blue1988tx is offline
New Member
 
Join Date: May 2007
Posts: 5
blue1988tx is on a distinguished road

Re: How to read the data as a whole string?


Thank you ,man!My other question is how to do "sorted by last name
with each of the last names fully capitalized (that is, if you read in
Wilkinson you print WILKINSON) ",change the letters to fully capitalized?

Why it is wrong?

CPP / C++ / C Code:
void ChangeLastname(string lastname[],string LASTNAME[])
{
for(int i=0;i<25;i++)
LASTNAME[i]=toupper(lastname[i]);
}

Last edited by LuciWiz : 31-May-2007 at 12:01. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #7  
Old 28-May-2007, 19:08
ahbi82 ahbi82 is offline
Member
 
Join Date: Jul 2006
Posts: 114
ahbi82 will become famous soon enough

Re: How to read the data as a whole string?


Well at least tell us what is wrong so we can address it. From the look of the source code,
CPP / C++ / C Code:
toupper
only converts a char to upper case. The parameter LASTNAME is an array of strings not an array of char. To convert a string type to uppercase, use

c_str() in <string> to get the C string equivalent and then use toupper to convert each char to uppercase. This is the trival method.

About sorting, you can search in STL <algorithms> tp find a suitable sorting algorithm.

Hope this helps.
 
 

Recent GIDBlogMeeting the populace by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Message Class TransformedBG C++ Forum 5 29-Nov-2006 21:28
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 13:12
Help wit my source code compiler errors Krandygrl00 C++ Forum 1 06-Jun-2005 08:14
Using getline to read in data krisbot C Programming Language 4 24-Oct-2004 15:57
[CONTEST?]Data Structure Test dsmith C Programming Language 2 06-Jun-2004 15:13

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 11:40.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.