473,320 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

C++ Program to read in names and grades and output final grade

Heya everyone, I am a freshman in college and am taking C++. This is not a begging thread for someone to solve my problem because I am interested in learning this myself.

So here is my question:

I am constructing a program that opens a file that has the names of students in it. It must then list the students on the screen in this format.

Last First Middle

That much I have accomplished.
It must then open up a file (that is already made) for each student listed. the files are in the format of (firstlast.dat - Ex. JohnDoe.dat) The files have a list of an unknown amount of grades from 0 - 100.

My question is, how do I tell the program what the name of the student is so that it may open it? Since each student's name is split into the variables first, middle, and last, is there a way I can combine strings then tell the program to .open (combined string.dat)?

Thanks for the help.
Oct 2 '07 #1
8 9897
Ganon11
3,652 Expert 2GB
OK, to summarize:

You have three strings - first, middle, and last - with the Student's name (i.e. "John", "Wilford", "Doe").

You have a file consisting of a first name and last name, followed by ".dat" (i.e. "JohnDoe.dat").

Then I would form a new string, filename. To give it the value, add first + last + ".dat"

The string class has been designed in a way that you can use + on it as if it were a number. When you do, the two strings are concatenated, or, well, added together. So, in our example, first + last is the same as "John" + "Doe" == "JohnDoe".

You'll get to learn all about how this is done - which is called operator overloading - either later in this first-year course, or in the next level course (if you choose to take it).
Oct 2 '07 #2
That solved my first problem, but now my second is that there is an unknown number of grades in each student's individual files so I cant make a certain number of variables to assign them to. I need to find the average. Ive been thinking and tinkering with my code and came up with a way to find the sum using:

while ( indata >> grade)
{
sum+=grade;
}

where grade is the first double in the file. But all this does is give me the sum correct, it also doesnt give me how many numbers are in the file so I dont know what to divide the sum by.
Oct 2 '07 #3
Studlyami
464 Expert 256MB
you could add a counter in the loop and increment the value by one each time. Then after you finished the loop you have the number of items added.
Expand|Select|Wrap|Line Numbers
  1. int i = 0;
  2. while ( indata >> grade)
  3. {
  4. sum+=grade;
  5. i++;
  6. }
  7.  
Oct 2 '07 #4
Studlyami: Thanks, that worked perfectly, I can now calculate the average of the grades.
Oct 3 '07 #5
I seem to have run into another problem that I cant figure out. When outputting to the screen I successfully get the first student's grades and average. But the other three students in my roster file only get their names displayed as well as a crazy number below them but I know thats because the variable that I use to divide my sum by gets reset to zero and you cant divide by zero.

Since describing is more difficult, im just going to show the code fragment that I think the problem is happening in.

Expand|Select|Wrap|Line Numbers
  1. while ( student_roster >> last >> first >> middle )
  2. // Takes the strings from student_roster and assigns them.
  3.  
  4.     {
  5.  
  6.      student_file = first + last + ".dat";
  7.      student.open ( student_file.c_str() );
  8.      cout << first << " " << middle << " " << last << endl;
  9.      numberofnumbers = 0;
  10.  
  11.      while (student >> grade)
  12.  
  13.      {
  14.  
  15.       cout << grade << endl;
  16.       sum+=grade;
  17.       numberofnumbers++;
  18.  
  19.      }
  20.  
  21.      average = sum/numberofnumbers;
  22.      cout << average << endl;
  23.  
  24.     }
I think the problem has to be in the first while loop somewhere because it is skipping the second loop entirely it seems.
Oct 3 '07 #6
Ganon11
3,652 Expert 2GB
After your second while() loop, use student.close() to close to file.
Oct 3 '07 #7
ok, turns out I needed to add student.clear() after the student.close().

Thanks for the help everyone, if I get stumped on something else ill let yall know!
Oct 3 '07 #8
Ok, I seem to have hit another wall. Randomly my professor decided to add a stipulation to the program.
It needs to display error messages if there are no grades in each student's file or if the student doesn't have a file at all.

Expand|Select|Wrap|Line Numbers
  1.      while ( student_roster >> last >> first >> middle )
  2.      // While there is data in the roster to be put into the named variables
  3.      // do this:
  4. {
  5.  
  6.       student_file = first + last + ".dat";
  7.       student.open ( student_file.c_str() );
  8.       cout << first << " " << middle << " " << last << endl;
  9.  
  10.       if (!student)
  11.       // If student file fails to load, then display an Error-
  12.  {
  13.         cout << "Error: No Data Availible" << endl;
  14.         cout << endl;
  15.  } 
  16.       else
  17.       // otherwise do the rest of the while loop.
  18.   { 
  19.  
  20.           numberofnumbers = 0;
  21.           sum = 0;
  22.           average = 0;
  23.           numberofstudents = 0;
  24.  
  25.       while (student >> grade)
  26.       // While there is data to be placed from student's file into grade
  27.       // do this:
  28.    { 
  29.       if (!grade)
  30.       {
  31.        cout << "No Grades Entered" << endl;           
  32.       }
  33.       else
  34.       {
  35.        sum+=grade;
  36.        numberofnumbers++;  
  37.        numberofstudents++;
  38.       } 
  39.  
  40.    } 
  41.  
  42.       average = sum/numberofnumbers;
  43.       cout << fixed << setprecision(2) << "Student Average: " << average << endl;
  44.       cout << endl;
  45.       class_average+=average;
  46.  
  47.   } 
  48.  
  49.       student.close();
  50.       student.clear();
In lines 10 - 15 I successfully added an if statement to display an Error when student fails to load and the program runs smoothly, however I cannot find where to place another if statement (if that is the best way) to display an error if there is no grades in the file. I thought it would be between lines 28 and 38 but that doesn't seem to work at all. I also need to be able to find the Max average and Min average in the program but that can be solved with a flag os ill be working on that for now.
Any ideas?
Oct 4 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: hjc | last post by:
i am trying to created a program that will write a grading program for a class with the following policies there are 2 quizzes each graded on the basis of 10 points there is 1 midterm and 1...
21
by: asif929 | last post by:
I need immediate help in writing a function program. I have to write a program in functions and use array to store them. I am not familiar with functions and i tried to create it but i fails to...
1
by: sparkid | last post by:
I need immediate help in writing a function program. I have to write a program in functions and use array to store them. I am not familiar with functions and i tried to create it but i fails to...
9
by: gdarian216 | last post by:
I have written a c++ program that takes input from a file and outputs the average. The program uses structs and I need to convert the struct to a class. I just dont know how to get started and if...
2
by: gdarian216 | last post by:
the program reads input from a file and then outputs the averages and grade. for some reason it is reading in the same line twice and it doesn't print out the grade. everything else is correct, if...
1
by: tonytj | last post by:
This program will read in studentID (3 digit integer number), three quizzes score, four assignments score and two tests scores. Each of the quizzes, assignments, and tests has up to 100 points. The...
1
by: gator6688 | last post by:
I have to write a program that allows the user to enter the information for up to 20 students. The info then has to be displayed and written to a file. After I enter the first students info and hit...
1
by: Sleepwalker817 | last post by:
Hello, I am trying to create a program that is supposed to calculate and print the average of several grades entered by the user. The output is supposed to look something like this:...
14
by: xtheendx | last post by:
I am writing a gradbook type program. It first allows the user to enter the number of students they want to enter. then allows them to enter the first name, last name, and grade of each student. The...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.