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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • midknight5
    New Member
    • Oct 2007
    • 25

    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.
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    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.da t").

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

    Comment

    • midknight5
      New Member
      • Oct 2007
      • 25

      #3
      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.

      Comment

      • Studlyami
        Recognized Expert Contributor
        • Sep 2007
        • 464

        #4
        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.
        Code:
        int i = 0;
        while ( indata >> grade)
        {
        sum+=grade;
        i++;
        }

        Comment

        • midknight5
          New Member
          • Oct 2007
          • 25

          #5
          Studlyami: Thanks, that worked perfectly, I can now calculate the average of the grades.

          Comment

          • midknight5
            New Member
            • Oct 2007
            • 25

            #6
            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.

            [CODE=cpp]while ( student_roster >> last >> first >> middle )
            // Takes the strings from student_roster and assigns them.

            {

            student_file = first + last + ".dat";
            student.open ( student_file.c_ str() );
            cout << first << " " << middle << " " << last << endl;
            numberofnumbers = 0;

            while (student >> grade)

            {

            cout << grade << endl;
            sum+=grade;
            numberofnumbers ++;

            }

            average = sum/numberofnumbers ;
            cout << average << endl;

            }[/CODE]

            I think the problem has to be in the first while loop somewhere because it is skipping the second loop entirely it seems.
            Last edited by Ganon11; Oct 3 '07, 12:47 AM. Reason: Please use the [CODE] tags provided.

            Comment

            • Ganon11
              Recognized Expert Specialist
              • Oct 2006
              • 3651

              #7
              After your second while() loop, use student.close() to close to file.

              Comment

              • midknight5
                New Member
                • Oct 2007
                • 25

                #8
                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!

                Comment

                • midknight5
                  New Member
                  • Oct 2007
                  • 25

                  #9
                  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.

                  Code:
                       while ( student_roster >> last >> first >> middle )
                       // While there is data in the roster to be put into the named variables
                       // do this:
                  {
                             
                        student_file = first + last + ".dat";
                        student.open ( student_file.c_str() );
                        cout << first << " " << middle << " " << last << endl;
                        
                        if (!student)
                        // If student file fails to load, then display an Error-
                   {
                          cout << "Error: No Data Availible" << endl;
                          cout << endl;
                   } 
                        else
                        // otherwise do the rest of the while loop.
                    { 
                            
                            numberofnumbers = 0;
                            sum = 0;
                            average = 0;
                            numberofstudents = 0;
                        
                        while (student >> grade)
                        // While there is data to be placed from student's file into grade
                        // do this:
                     { 
                        if (!grade)
                        {
                         cout << "No Grades Entered" << endl;           
                        }
                        else
                        {
                         sum+=grade;
                         numberofnumbers++;  
                         numberofstudents++;
                        } 
                         
                     } 
                        
                        average = sum/numberofnumbers;
                        cout << fixed << setprecision(2) << "Student Average: " << average << endl;
                        cout << endl;
                        class_average+=average;
                        
                    } 
                        
                        student.close();
                        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?

                  Comment

                  Working...