Grading Program Error

This is a discussion on Grading Program Error within the C++ Programming forums, part of the General Programming Boards category; I'm writing this grading program that calculates and displays a students grades and after all the students have been entered, ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    9

    Grading Program Error

    I'm writing this grading program that calculates and displays a students grades and after all the students have been entered, it displays(or at least it should) display the group totals. The problem is, everytime i run the program, the program crashes. I can't seem to figure out why it does that. I'd really appreciate it if someone could help me out a bit. Any advice would be really helpful. Thanks!

    ps. i wasn't able to calculate the
    classes average grade yet.




    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    struct studentRecord //structure definition
    {
           int studentID;
           char first_name[21];
           char last_name[11];
           int quiz_1;
           int quiz_2;
           int midterm_score;
           int final_score;
           int totalpts;
           double percent;
           char grade;
    };
    
    studentRecord student; //structure variable student
    
    //declaring constants
    const int maxpts = 200;
    
    //declare function prototypes
    bool moreStudents();
    void getInput(studentRecord &myStudent);
    double computePercent(int totalpts, int);
    int computePointsEarned(int quiz_1, int quiz_2, int midterm_score, int final_score);
    void displayRecord(studentRecord);
    void displayTotals(int totalStudents, double highest, double lowest,
                       double avgPoints);
    int computeHighestPoints(int numbers1, int numbers2);
    int computeLowestPoints(int numbers1, int numbers2);
    int computeClassPoints (int numbers1, int numbers2);
    double computeAveragePoints(int allPoints, int totalStudents);
    char computeGrade(double input);
    
    
    int main()
    {
        //declare variables
        int totalStudents = 0;
        int highest = 0;
        int lowest = 0;
        int allPoints;
        double avgPoints;
        
        //adjusts number output
        cout.setf(ios::fixed);
        cout.setf(ios::showpoint);
        cout.precision(1);
        
        do
        {
             getInput(student);
             displayRecord(student);
             student.percent = computePercent(student.totalpts, maxpts);
             student.totalpts = computePointsEarned(student.quiz_1, student.quiz_2, 
                                         student.midterm_score, student.final_score);
             student.grade = computeGrade(student.percent);
             highest = computeHighestPoints(highest, student.totalpts);
             lowest = computeLowestPoints(lowest, student.totalpts);
             allPoints = computeClassPoints(allPoints, student.totalpts);
             avgPoints = computeAveragePoints(allPoints, totalStudents);
             totalStudents++; //add 1 to student counter
             
        }while(moreStudents()); //calls the moreStudents() function
        
        displayTotals(totalStudents, highest, lowest, avgPoints);
        
        system("PAUSE");
        return 0;
        
    } //end main()
    
    //functions
    
    bool moreStudents()
    
    {
         bool more = false;
         char anotherStudent = 'n';
         
         cout << "\n\nWould You like to enter another student? enter 'y' or 'Y' and press ENTER; \n";
         cout << "if not, enter any other character and press ENTER ";
         
         if(anotherStudent == 'y' || anotherStudent == 'Y')
                more = true;
         else
                more = false;
                
         
         return more;
    
    } //end anotherStudent
    
    void getInput (studentRecord &myStudent)
    
    {
         int ID;
         
         string message = "\n\nEnter Student ID: ";
         
         do
         {
                cout << message;
                cin >> ID;
                
                message = "\n\nError! Please Re-Enter Student ID (1-99999): ";
         
         } while (ID < 1 || ID > 99999);
         
         myStudent.studentID = ID;
         
         
         //asks user to input first and last name
    
                cout << "Enter First Name: ";
                cin >> myStudent.first_name;
                
                
                cout << "Enter Last Name: ";
                cin >> myStudent.last_name;
    
    
    
        //determines quiz #1 score
        int Quiz1;
        
        string message1 = "Please enter Quiz #1 score: ";
        
        do
        {
               cout << message1;
               cin >> Quiz1;
               
               message1 = "\n\nError! Please Re-Enter Quiz #1 score: ";
        } while (Quiz1 < 0 || Quiz1 > 25);
        
        myStudent.quiz_1 = Quiz1;
        
        //determines quiz #2 score
        int Quiz2;
        
        string message2 = "Please enter Quiz #2 score: ";
        
        do
        {
               cout << message2;
               cin >> Quiz2;
               
               message = "\n\nError! Please Re-Enter Quiz #2 2core: ";
        } while (Quiz2 < 0 || Quiz2 > 25);
        
        myStudent.quiz_2 = Quiz2;
        
        //determines midterm score
        int Midterm;
        
        string message3 = "Please enter Midterm score: ";
        
        do
        {
               cout << message3;
               cin >> Midterm;
               
               message = "\n\nError! Please Re-Enter Midterm 2core: ";
        } while (Midterm < 0 || Midterm > 50);
        
        myStudent.midterm_score = Midterm;
        
        //determines final score
        int Final;
        
        string message4 = "Please enter Final score: ";
        
        do
        {
               cout << message4;
               cin >> Final;
               
               message = "\n\nError! Please Re-Enter Final score: ";
        } while (Final < 0 || Final > 100);
        
        myStudent.final_score = Final;
        
    } //end getInput()
    
    int computeHighestPoints(int numbers1, int numbers2)
    {
           if (numbers1 >= numbers2)
           return numbers1;
        else
            return numbers2;
    }//end computeHighestPoints
    
    int computeLowestPoints(int numbers1, int numbers2)
    {
        if (numbers1 <=numbers2)
           return numbers1;
        else
            return numbers2;
    }//end computeLowestPoints
    
    int computeClassPoints(int numbers1, int numbers2)
    {
        return (numbers1 + numbers2);
    }//end computeAllPoints
    
    double computeAveragePoints(int allPoints, int totalStudents)
    {
           return (allPoints/totalStudents);
    }//end computeAveragePoints
         
    double computePercent(int total, int max)
    {
           //calculates percentage
           int output;
           output = (maxpts)/total*100;
           return output;
    } //end computePercent
    
    
    int computePointsEarned(int quiz_1, int quiz_2, int midterm_score, int final_score)
    {
           //calculates total points earned
           int output;
           output = quiz_1 + quiz_2 + midterm_score + final_score;
           return output;
    } //end computePointsEarned
    
    char computeGrade(double input)
    {
         if (input >= 90)
            return 'A';
         else if (input >= 80)
              return 'B';
         else if (input >= 70)
              return 'C';
         else if (input >= 60)
              return 'D';
         else return 'F';
    }//end computeGrade
    
    
    void displayRecord(studentRecord staff)
    {
         //display input data
         cout << "\n\nStudent ID: " << staff.studentID;
         cout << "\nFirst Name: " << staff.first_name;
         cout << "\nLast Name: " << staff.last_name;
         cout << "\nQuiz #1 Score: " << staff.quiz_1;
         cout << "\nQuiz#2 Score: " << staff.quiz_2;
         cout << "\nMidterm Score: " << staff.midterm_score;
         cout << "\nFinal Score: " << staff.final_score;
         cout << "\nTotal Points Earned: " << staff.totalpts;
         cout << "\nOverall Percentage: " << staff.percent;
         cout << "\nLetter Grade: " << staff.grade;
    } //end of displayRecord
    
    
    void displayTotals(int totalStudents, double highest, double lowest,
                       double avgPoints)
    {
         cout << "\n--------------------------------------------\n\n";
         cout << "Total Students = " << totalStudents;
         cout << "\nHighest Total Points Earned = " << highest;
         cout << "\nLowest Total Points Earned = " << lowest;
         cout << "\nAverage Total Points Earned = " << avgPoints;
         cout << "\nAverage Letter Grade Earned = ???";
    
    }//end displayTotals

  2. #2
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Well, I'll teach you the art of debugging. Start by commenting out everything inside the main(). Then uncomment the first line, compile and run it. Repeat for the next line until you find the one that crashes the program.
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  3. #3
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    care to narrow down where it's crashing?
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  4. #4
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by alphaoide
    Well, I'll teach you the art of debugging. Start by commenting out everything inside the main(). Then uncomment the first line, compile and run it. Repeat for the next line until you find the one that crashes the program.
    or you could fast forward from the 50's and use a debugger?
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  5. #5
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Well, a beginner is not yet familiar with stepping into, inserting breakpoint, etc, I suppose.
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  6. #6
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by alphaoide
    Well, a beginner is not yet familiar with stepping into, inserting breakpoint, etc, I suppose.
    well then it's a good time to learn, isn't it?

    seriously, that kind of blind change/recompile/test cycle takes forever once you have any decent size of program. Learning good debugging skills early on is invaluable.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    9
    Quote Originally Posted by ChaosEngine
    care to narrow down where it's crashing?
    it crashes right after it displays the student's individual record. =\

  8. #8
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Code:
        do
        {
             getInput(student);
             displayRecord(student);
             student.percent = computePercent(student.totalpts, maxpts);  // student.totalpts is not initialized
    You may want to re-order your function calls
    Code:
        do
        {
             getInput(student);
             displayRecord(student);
             student.totalpts = computePointsEarned(student.quiz_1, student.quiz_2, student.midterm_score, student.final_score);
             student.percent = computePercent(student.totalpts, maxpts);
    Last edited by alphaoide; 05-08-2006 at 08:09 AM.
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    Code:
    int totalStudents = 0;
    
    ...
    
    avgPoints = computeAveragePoints(allPoints, totalStudents);
    totalStudents++; //add 1 to student counter
    
    ...
    
    double computeAveragePoints(int allPoints, int totalStudents)
    {
           return (allPoints/totalStudents);
    }//end computeAveragePoints
    You'll have problems when calling that function the first time since totalStudents is equal to 0 the first time through the loop and you are dividing by 0 which is a no-no. The increment of totalStudents should be before you call the computeAveragePoints function and not after.

    There is also a problem in that you are returning a double from that function but the function itself performs integer division which will not give you what you want. You will need to cast at least one of the arguments to type double prior to the division so that floating-point division will take place.
    Code:
    double computeAveragePoints(int allPoints, int totalStudents)
    {
        return static_cast<double>(allPoints) / totalStudents;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    9
    yay! i was able to get it to work, thanks for the help! =]

    here's what the code looks like:

    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    struct studentRecord //structure definition
    {
           int studentID;
           char first_name[21];
           char last_name[11];
           int quiz_1;
           int quiz_2;
           int midterm_score;
           int final_score;
           int totalpts;
           double percent;
           char grade;
    };
    
    studentRecord student; //structure variable student
    
    //declaring constants
    const int maxpts = 200;
    
    //declare function prototypes
    bool moreStudents();
    void getInput(studentRecord &myStudent);
    int computePointsEarned(int quiz_1, int quiz_2, int midterm_score, int final_score);
    double computePercent(int totalpts, int);
    void displayRecord(studentRecord);
    int computeHighestPoints(int numbers1, int numbers2);
    int computeLowestPoints(int numbers1, int numbers2);
    double computeAveragePoints(int allPoints, int totalStudents);
    double computeAveragePercent (double avgPoints, int);
    char computeGrade(double input);
    void displayTotals(int totalStudents, double highest, double lowest,
                       double avgPoints, char avgGrade);
    
    
    int main()
    {
        //declare variables
        int totalStudents = 0;
        int highest = 0;
        int lowest = 0;
        int allPoints = 0;
        double avgPoints;
        double avgPercent;
        char avgGrade;
        
        //adjusts number output
        cout.setf(ios::fixed);
        cout.setf(ios::showpoint);
        cout.precision(1);
        
        do
        {
             getInput(student);
             student.totalpts = computePointsEarned(student.quiz_1, student.quiz_2, 
                                         student.midterm_score, student.final_score);
             student.percent = computePercent(student.totalpts, maxpts);
             student.grade = computeGrade(student.percent);
             lowest = computeLowestPoints(student.totalpts, highest);
             highest = computeHighestPoints(highest, student.totalpts);
             displayRecord(student);
             totalStudents++; //add 1 to student counter
             allPoints = allPoints + student.totalpts; //accumulates total class points
             avgPoints = computeAveragePoints(allPoints, totalStudents);
             avgPercent = computeAveragePercent(avgPoints, maxpts);
             avgGrade = computeGrade(avgPercent);
            
             
        }while(moreStudents()); //calls the moreStudents() function
    
        
        displayTotals(totalStudents, highest, lowest, avgPoints, avgGrade);
        
        system("PAUSE");
        return 0;
        
    } //end main()
    
    //functions
    
    bool moreStudents()
    
    {
         bool more = false;
         char anotherStudent = 'n';
         
         cout << "\n\nWould You like to enter another student? enter 'y' or 'Y' and press ENTER; \n";
         cout << "if not, enter any other character and press ENTER ";
         cin >> anotherStudent;
         
         if(anotherStudent == 'y' || anotherStudent == 'Y')
                more = true;
         else
                more = false;
                
         
         return more;
    
    } //end anotherStudent
    
    void getInput (studentRecord &myStudent)
    
    {
         int ID;
         
         string message = "\n\nEnter Student ID: ";
         
         do
         {
                cout << message;
                cin >> ID;
                
                message = "\n\nError! Please Re-Enter Student ID (1-99999): ";
         
         } while (ID < 1 || ID > 99999);
         
         myStudent.studentID = ID;
         
         
         //asks user to input first and last name
    
                cout << "Enter First Name: ";
                cin >> myStudent.first_name;
                
                
                cout << "Enter Last Name: ";
                cin >> myStudent.last_name;
    
    
    
        //determines quiz #1 score
        int Quiz1;
        
        string message1 = "Please enter Quiz #1 score: ";
        
        do
        {
               cout << message1;
               cin >> Quiz1;
               
               message1 = "\n\nError! Please Re-Enter Quiz #1 score: ";
        } while (Quiz1 < 0 || Quiz1 > 25);
        
        myStudent.quiz_1 = Quiz1;
        
        //determines quiz #2 score
        int Quiz2;
        
        string message2 = "Please enter Quiz #2 score: ";
        
        do
        {
               cout << message2;
               cin >> Quiz2;
               
               message2 = "\n\nError! Please Re-Enter Quiz #2 2core: ";
        } while (Quiz2 < 0 || Quiz2 > 25);
        
        myStudent.quiz_2 = Quiz2;
        
        //determines midterm score
        int Midterm;
        
        string message3 = "Please enter Midterm score: ";
        
        do
        {
               cout << message3;
               cin >> Midterm;
               
               message3 = "\n\nError! Please Re-Enter Midterm 2core: ";
        } while (Midterm < 0 || Midterm > 50);
        
        myStudent.midterm_score = Midterm;
        
        //determines final score
        int Final;
        
        string message4 = "Please enter Final score: ";
        
        do
        {
               cout << message4;
               cin >> Final;
               
               message4 = "\n\nError! Please Re-Enter Final score: ";
        } while (Final < 0 || Final > 100);
        
        myStudent.final_score = Final;
        
    } //end getInput()
    
    
    int computePointsEarned(int quiz1, int quiz2, int midtermScore, int finalScore)
    {
           //calculates total points earned
           int output;
           output = quiz1 + quiz2 + midtermScore + finalScore;
           return output;
    } //end computePointsEarned
    
    double computePercent(int total, int max)
    {
           //calculates percentage
           int output;
           output = (total)*100/max;
           return output;
    } //end computePercent
    
    char computeGrade(double input)
    {
         if (input >= 90)
            return 'A';
         else if (input >= 80)
              return 'B';
         else if (input >= 70)
              return 'C';
         else if (input >= 60)
              return 'D';
         else return 'F';
    }//end computeGrade
    
    int computeLowestPoints(int numbers1, int numbers2)
    {
        if (numbers1 <=numbers2)
           return numbers1;
        else
            return numbers2;
    }//end computeLowestPoints
    
    int computeHighestPoints(int numbers1, int numbers2)
    {
           if (numbers1 >= numbers2)
           return numbers1;
        else
            return numbers2;
    }//end computeHighestPoints
    
    
    double computeAveragePoints(int all, int total)
    {
           int output;
           output = all/total;
           return output;
    }//end computeAveragePoints
    
    double computeAveragePercent(double avg, int max)
    {
           //calculates percentage
           double output;
           output = (avg)*100/max;
           return output;
    } //end computePercent     
    
    
    void displayRecord(studentRecord staff)
    {
         //display input data
         cout << "\n\nStudent ID: " << staff.studentID;
         cout << "\nFirst Name: " << staff.first_name;
         cout << "\nLast Name: " << staff.last_name;
         cout << "\nQuiz #1 Score: " << staff.quiz_1;
         cout << "\nQuiz#2 Score: " << staff.quiz_2;
         cout << "\nMidterm Score: " << staff.midterm_score;
         cout << "\nFinal Score: " << staff.final_score;
         cout << "\nTotal Points Earned: " << staff.totalpts;
         cout << "\nOverall Percentage: " << staff.percent;
         cout << "\nLetter Grade: " << staff.grade;
    } //end of displayRecord
    
    
    void displayTotals(int totalStudents, double highest, double lowest,
                       double avgPoints, char avgGrade)
    {
         cout << "\n-------------------------------------------------\n\n";
         cout << "Total Students = " << totalStudents;
         cout << "\nHighest Total Points Earned = " << highest;
         cout << "\nLowest Total Points Earned = " << lowest;
         cout << "\nAverage Total Points Earned = " << avgPoints;
         cout << "\nAverage Letter Grade Earned = " << avgGrade;
         cout << "\n-------------------------------------------------\n\n";
         
    
    }//end displayTotals

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 03:35 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21