Question in reading a file and writing a report. (File I/O)

Posted on 2003-03-20
Medium Priority
Last Modified: 2012-05-05
My instructor give me this problem, but I have no idea how to start it...
Can someone give me a big picture or outline on how to solve it?

Read in the input file below and produce the report shown.

Input file
Sylvester,De La Rosa,555555555,25,25,24,20,25,25,21,44,80

                            Class Grades Report

Student Name          --- SSN ---  ---- Lab Grades ----  Mid  Fin  Pts   Perct  G
--------------------  -----------  -- -- -- -- -- -- --  ---  ---  ---   -----  -
Doe, John             123-45-6789  20 21 22 23 16 19 16   50   75  246   82.0%  B
Washington, Francisc  987-65-4321  10  0 20 13 18 19 16   30   70  196   65.3%  D
Nguyen, Tom           111-11-1111  18 23 24 25 17 22 20   38   90  260   86.7%  B
Black, Victoria       333-33-3333  22 21 22 21 20 22 21   45   64  238   79.3%  C
Seinfield, Sally      444-44-4444  17 12 19 23 24 12 11   34   94  234   78.0%  C
De La Rosa, Sylveste  555-55-5555  25 25 24 20 25 25 21   44   80  269   89.7%  B
O'Neill, George       666-66-6666  21 12  3 14 21 14 17   45   99  243   81.0%  B
Smart, Sylvia         777-77-7777  20 21 22 23 24 20 25   44   78  257   85.7%  B
Yang, Judy            888-88-8888  16 19 22 24 25 20 25   45  100  280   93.3%  A
Black, Charles        222-22-2222  20 21 22 22 21 25 16   40   86  253   84.3%  B

•     Use C++ input/output techniques and file I/O, no stdio.  You should declare one ifstream and one ofstream object.
•     Create at least 3 classes:
1.     Name consists of a first and last name.
2.     StudentInfo consists of a Name, SSN, 7 lab grades, a midterm, final, and whatever else you want.
3.     Class consists of an array of StudentInfo or an array of StudentInfo pointers
•     The rules for calculating points and grades are exactly like the 1st assignment or what was stated on the course syllabus.  Remember to discard the lowest lab grade, but not the last one.
•     Use the same input file shown.
•     Produce exactly the report, with the same spacing, formatting and text.  Turn in a copy of the report file along with your program listing.
•     If you are unsure of any program detail, ask the instructor for clarification.

1.     Sort the report by Name (major sort by last name, minor sort by first name)
2.     Handle a missing lab score instead of a 0.  Use this record for Francisco Washington: Francisco,Washington,987654321,10,,20,13,18,19,16,60,140
3.     Change the lab grades in the input file to octal, but print them out as hexadecimal.
Question by:naturechan
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 30

Accepted Solution

Mayank S earned 152 total points
ID: 8179457
Ya, we can help you out but that's by far too much of work! As for the basics:

class Name
  char fname[20] ; // private by default
  char lname[20] ;

// define constructors and other public member functions

} ; // class definition over

class StudentInfo
  Name name ;
  char ssn[12] ;
  int grades[7] ;
  int midterm ;
  int final ;
  int points ;
  float perc ;
  char grade ;

// define constructors and other public member functions

} ; // class definition over

class ClassOfStudents
  StudentInfo array[60] ;

// define constructors and other public member functions

} ; // class definition over

Thats's to help you get started. As for using file I/O, you can use ifstream objects to read from a file as:

ifstream infile ( "filename" ) ;
char str[80] ;

while ( ! infile.eof () )
  infile.getline ( str, 80 ) ; // this will read one string from the file into 'str'

// Now, you can separate the fields by delimiting them at the commas using strtok () and storing them infname[], lname[], etc

} // end while

Sameway, for writing to a file, use ofstream:

ofstream outfile ( "file" ) ;

while ( <whatever-condition> )
  outfile << str << endl ; // write the string 'str' to the file

} // end while

Hope that helps!

LVL 12

Assisted Solution

Salte earned 148 total points
ID: 8180564
This problem sounds like an obvious perl problem, so I suggest you should use perl.

What? You have to use C++, it's homework?

well, we can't help you with homework here.

We can help you with specific questions.

You esentially want to read a line, format it and print it out.

I don't see any sorting involved so you don't actually have to store the file's data in your program, just input an item, print it out in the proper format and then read the next item print it out, change to new line when it is end of line of the current item etc.

The input seem to be comma separated values so strchr(str,',') is probably the function you should use to locate the next ',', when this funciton return NULL pointer you have a string that doesn't contain any more commas and it is the last field.

Each field must be read and parsed on its own and output accordingly. The SSN is particularly easy to parse, it has a fixed number of digits, not sure if you have to check that they really are digits - you should probably complain if they aren't etc once verified you just break it into substrings.

if you have a string like this:


you can format that to "foo-bar-baz" by doing:

char buf[12];
const char * src = "foobarbaz";
buf[3] = '-';
buf[7] = '-';

buf is now "foo-bar-baz"

Note that buf must have room for 12 characters, the 9 characters from source string, the 2 '-' characters inserted and the null byte.

Then all you need to know is how to compute the fields of output that aren't copied from input. These are most likely computed based on selected input fields and so if you remember to store those input fields you probably should be well equipped to compute those values. One advice here is that you don't actually have to store all the input data.

For example if you want to compute an average you don't need to store 15 values to compute the average of 15 values. Storing the sum of them and storing a count of how many values you read is enough. Even if you want to compute the average of 1000 values you only need 2 variables.

Similarly if you ever need to compute the standard deviation you only need to store 3 variables:

one value remember how many values you've read so far.
one value keep the sum of all input values.
one value keep the sum of the square of all input values.

That is all you need to compute stddev.

ALso, the same variable can probably be used for several fields, if you have one value that keep the sum it can be used to compute the average and using the average you also possibly know what grade etc to give.

In any event the instructions appear to be very detailed and clear and you should have no problem doing the assignment based on the info given here and from the instructor.

One point is that you don't tell us the formula for computing the grades and the text doesn't tell that either, but it refers to it. The fact that you didn't provide that as additional info indicate to me that you haven't even read the text you just copy and paste it form the teacher directly to here and that just isn't the way you do your homework.

If you want us to provide help and give you of our time the least you could do is to actually read the assignment text before you copy and paste it to EE. Probably if you also read the agreement with EE You should know that we won't help you with homework and so you probably would have also tried to make an attempt to do something about it and then come to us with specific problems.

Not even bothering to read the assignment and just copy and paste it to EE is really shameful indeed. If you did read the assignment you would know that the formula was missing from the text and was only referenced in it and you would have provided it for us as additional info.


Expert Comment

ID: 9517234
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Split points between mayankeagle & Salte

Please leave any comments here within the next seven days.


EE Cleanup Volunteer

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question