Grading Program pt.II (classes) help...

This is a discussion on Grading Program pt.II (classes) help... within the C++ Programming forums, part of the General Programming Boards category; Hi, so, this is a modification to my original program in thread "Grading Program Error." I revised it by using ...

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

    Grading Program pt.II (classes) help...

    Hi, so, this is a modification to my original program in thread "Grading Program Error." I revised it by using classes instead of strucutres and also adding a file i/o. I was able to compile it and run it, but it doesn't seem to be working properly, and I can't figure out what's wrong =\ Here's the initial problems when the program is run:

    1) Doesn't append the data to file "students.txt"
    2) Crashes after inputting all student data

    Again, any help would be great, thank you! =]


    Code:
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    class Student //abstract data type implemented as a class
    {
          public: //prototypes for public member functions
          
                 Student(); //constructor function
           
                 
                 void setID(int);
                 void setFirstName(string);
                 void setLastName(string);
                 void setQuiz1(int);
                 void setQuiz2(int);
                 void setMidterm(int);
                 void setFinal(int);
                 
                 int getTotalpts();
                 double getpercent();
                 char getgrade();
           
           
                 
           private: //private data members accessible to member functions
                  
                  int studentID;
                  string first_name;
                  string last_name;
                  int quiz_1;
                  int quiz_2;
                  int midterm_score;
                  int final_score;
                  int totalpts;
                  double percent;
                  char grade;
                  
    };
    
    Student::Student() //default constructor
    {
                       
                   //do nothing
                                   
    }
    
    
    //member functions
    
    void Student::setID(int id)
    {
         studentID = id;
    } //end setID function
    
    
    void Student::setFirstName(string first)
    {
         first_name = first;
    } //end setFirstName function
    
    
    void Student::setLastName(string last)
    {
         last_name = last;
    } //end setLastName function
    
    
    void Student::setQuiz1(int quiz1)
    {
         quiz_1 = quiz1;
    } //end setQuiz1 function
    
    
    void Student::setQuiz2(int quiz2)
    {
         quiz_2 = quiz2;
    } //end setQuiz2 function
    
    
    void Student::setMidterm(int midterm)
    {
         midterm_score = midterm;
    } //end setMidterm function
    
    
    void Student::setFinal(int final)
    {
         final_score = final;
    } //end setFinal function
    
    
    int Student::getTotalpts()
    {
        //calculate totalpts
        totalpts = quiz_1 + quiz_2 + midterm_score + final_score;
        return totalpts;
    } //end gettotalpts()
    
    
    double Student::getpercent()
    {
           
           //calculate percent
           percent = (totalpts)*100/200;
           return percent;
    } //end getpercent()
    
    
    char Student::getgrade()
    {
         double input;
         
         //calculate grade
         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 getgrade()
    
    
    int main()
    {
        void getInput(int &ID, string &firstName, string &lastName, int &firstQuiz, 
                      int &secondQuiz, int &midterm, int &final);
        bool moreStudents();
        void displayRecord(int ID, string firstName, string lastName, int firstQuiz,
                       int secondQuiz, int midterm, int final, int total,
                       double percentage, char finalgrade);
        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);
                       
        
        //declare variables
        
        //input
        int studentID;
        string first_name;
        string last_name;
        int quiz_1;
        int quiz_2;
        int midterm_score;
        int final_score;
        
        //calculation results
        int total_pts;
        double percent;
        char grade;
        
        //totals
        int totalStudents = 0;
        int highest = 0;
        int lowest = 0;
        int allPoints = 0;
        double avgPoints;
        int maxpts = 200;
        double avgPercent;
        char avgGrade;
    
         Student person;
         
         do
         {
                 getInput(studentID, first_name, last_name, quiz_1, quiz_2, midterm_score, final_score);
                 
                 //set input values
                 person.setID(studentID);
                 person.setFirstName(first_name);
                 person.setLastName(last_name);
                 person.setQuiz1(quiz_1);
                 person.setQuiz2(quiz_2);
                 person.setMidterm(midterm_score);
                 person.setFinal(final_score);
                 
                 //perform calculations
                 total_pts = person.getTotalpts();
                 percent = person.getpercent();
                 grade = person.getgrade();
                 totalStudents++; //adds 1 to totalStudents counter
                 allPoints = allPoints + person.getTotalpts(); //accumulates total class
                 avgPoints = computeAveragePoints(allPoints, totalStudents);
                 avgPercent = computeAveragePercent(avgPoints, maxpts);
                 avgGrade = computeGrade(avgPercent);
                 
         ofstream iFile("students.txt", ios::app);
         if (!iFile.eof())
         {     
                 cout << "Error: Couldn't Append To End Of File" << endl;
         }else{
                 iFile << "\n\nStudent ID: " << studentID
                       << "\nFirst Name: " << first_name
                       << "\nLast Name: " << last_name
                       << "\nQuiz #1 Score: " << quiz_1
                       << "\nQuiz#2 Score: " << quiz_2
                       << "\nMidterm Score: " << midterm_score
                       << "\nFinal Score: " << final_score
                       << "\nTotal Points Earned: " << total_pts
                       << "\nOverall Percentage: " << percent
                       << "\nLetter Grade: " << grade;
         }
         cin.get();
         iFile.close();
                 
         }while(moreStudents()); //calls the moreStudents() function
         
         
         displayTotals(totalStudents, highest, lowest, avgPoints, avgGrade);
         
         return 0;
        
    } //end main()         
                 
    
    void getInput(int &ID, string &firstName, string &lastName, int &firstQuiz, 
                      int &secondQuiz, int &midterm, int &final)
    {
         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);        
         
         
         //asks user to input first and last name
    
                cout << "Enter First Name: ";
                cin >> firstName;
                
                
                cout << "Enter Last Name: ";
                cin >> lastName;
                
    //determines quiz #1 score
        string message1 = "Please enter Quiz #1 score: ";
        
        do
        {
               cout << message1;
               cin >> firstQuiz;
               
               message1 = "\n\nError! Please Re-Enter Quiz #1 score: ";
        } while (firstQuiz < 0 || firstQuiz > 25);
        
        
        string message2 = "Please enter Quiz #2 score: ";
        
        do
        {
               cout << message2;
               cin >> secondQuiz;
               
               message2 = "\n\nError! Please Re-Enter Quiz #2 2core: ";
        } while (secondQuiz < 0 || secondQuiz > 25);
        
        
        string message3 = "Please enter Midterm score: ";
        
        do
        {
               cout << message3;
               cin >> midterm;
               
               message3 = "\n\nError! Please Re-Enter Midterm 2core: ";
        } while (midterm < 0 || midterm > 50);
        
        
        string message4 = "Please enter Final score: ";
        
        do
        {
               cout << message4;
               cin >> final;
               
               message4 = "\n\nError! Please Re-Enter Final score: ";
        } while (final < 0 || final > 100);
        
        
        return;
    
    } //getInput()
    
    
    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
    
    
    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
    
    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 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

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    34
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    class Student //abstract data type implemented as a class
    {
          public: //prototypes for public member functions
          
                 Student(); //constructor function
           
                 
                 void setID(int);
                 void setFirstName(string);
                 void setLastName(string);
                 void setQuiz1(int);
                 void setQuiz2(int);
                 void setMidterm(int);
                 void setFinal(int);
                 
                 int getTotalpts();
                 double getpercent();
                 char getgrade();
           
           
                 
           private: //private data members accessible to member functions
                  
                  int studentID;
                  string first_name;
                  string last_name;
                  int quiz_1;
                  int quiz_2;
                  int midterm_score;
                  int final_score;
                  int totalpts;
                  double percent;
                  char grade;
                  
    };
    
    Student::Student() //default constructor
    {
                       
                   //do nothing
                                   
    }
    
    
    //member functions
    
    void Student::setID(int id)
    {
         studentID = id;
    } //end setID function
    
    
    void Student::setFirstName(string first)
    {
         first_name = first;
    } //end setFirstName function
    
    
    void Student::setLastName(string last)
    {
         last_name = last;
    } //end setLastName function
    
    
    void Student::setQuiz1(int quiz1)
    {
         quiz_1 = quiz1;
    } //end setQuiz1 function
    
    
    void Student::setQuiz2(int quiz2)
    {
         quiz_2 = quiz2;
    } //end setQuiz2 function
    
    
    void Student::setMidterm(int midterm)
    {
         midterm_score = midterm;
    } //end setMidterm function
    
    
    void Student::setFinal(int final)
    {
         final_score = final;
    } //end setFinal function
    
    
    int Student::getTotalpts()
    {
        //calculate totalpts
        totalpts = quiz_1 + quiz_2 + midterm_score + final_score;
        return totalpts;
    } //end gettotalpts()
    
    
    double Student::getpercent()
    {
           
           //calculate percent
           percent = (totalpts)*100/200;
           return percent;
    } //end getpercent()
    
    
    char Student::getgrade()
    {
         double input;
         
         //calculate grade
         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 getgrade()
    
    
    int main()
    {
        void getInput(int &ID, string &firstName, string &lastName, int &firstQuiz, 
                      int &secondQuiz, int &midterm, int &final);
        bool moreStudents();
        void displayRecord(int ID, string firstName, string lastName, int firstQuiz,
                       int secondQuiz, int midterm, int final, int total,
                       double percentage, char finalgrade);
        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);
                       
        
        //declare variables
        
        //input
        int studentID;
        string first_name;
        string last_name;
        int quiz_1;
        int quiz_2;
        int midterm_score;
        int final_score;
        
        //calculation results
        int total_pts;
        double percent;
        char grade;
        
        //totals
        int totalStudents = 0;
        int highest = 0;
        int lowest = 0;
        int allPoints = 0;
        double avgPoints;
        int maxpts = 200;
        double avgPercent;
        char avgGrade;
    
         Student person;
         
         do
         {
                 getInput(studentID, first_name, last_name, quiz_1, quiz_2, midterm_score, final_score);
                 
                 //set input values
                 person.setID(studentID);
                 person.setFirstName(first_name);
                 person.setLastName(last_name);
                 person.setQuiz1(quiz_1);
                 person.setQuiz2(quiz_2);
                 person.setMidterm(midterm_score);
                 person.setFinal(final_score);
                 
                 //perform calculations
                 total_pts = person.getTotalpts();
                 percent = person.getpercent();
                 grade = person.getgrade();
                 totalStudents++; //adds 1 to totalStudents counter
                 allPoints = allPoints + person.getTotalpts(); //accumulates total class
                 avgPoints = computeAveragePoints(allPoints, totalStudents);
                 avgPercent = computeAveragePercent(avgPoints, maxpts);
                 avgGrade = computeGrade(avgPercent);
                 
         ofstream iFile("students.txt", ios::app);
         if (!iFile)
         {     
                 cout << "Error: Couldn't Append To End Of File" << endl;
         }else{
                 iFile << "\n\nStudent ID: " << studentID
                       << "\nFirst Name: " << first_name
                       << "\nLast Name: " << last_name
                       << "\nQuiz #1 Score: " << quiz_1
                       << "\nQuiz#2 Score: " << quiz_2
                       << "\nMidterm Score: " << midterm_score
                       << "\nFinal Score: " << final_score
                       << "\nTotal Points Earned: " << total_pts
                       << "\nOverall Percentage: " << percent
                       << "\nLetter Grade: " << grade;
         }
         cin.get();
         iFile.close();
                 
         }while(moreStudents()); //calls the moreStudents() function
         
         
         displayTotals(totalStudents, highest, lowest, avgPoints, avgGrade);
         
         return 0;
        
    } //end main()         
                 
    
    void getInput(int &ID, string &firstName, string &lastName, int &firstQuiz, 
                      int &secondQuiz, int &midterm, int &final)
    {
         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);        
         
         
         //asks user to input first and last name
    
                cout << "Enter First Name: ";
                cin >> firstName;
                
                
                cout << "Enter Last Name: ";
                cin >> lastName;
                
    //determines quiz #1 score
        string message1 = "Please enter Quiz #1 score: ";
        
        do
        {
               cout << message1;
               cin >> firstQuiz;
               
               message1 = "\n\nError! Please Re-Enter Quiz #1 score: ";
        } while (firstQuiz < 0 || firstQuiz > 25);
        
        
        string message2 = "Please enter Quiz #2 score: ";
        
        do
        {
               cout << message2;
               cin >> secondQuiz;
               
               message2 = "\n\nError! Please Re-Enter Quiz #2 2core: ";
        } while (secondQuiz < 0 || secondQuiz > 25);
        
        
        string message3 = "Please enter Midterm score: ";
        
        do
        {
               cout << message3;
               cin >> midterm;
               
               message3 = "\n\nError! Please Re-Enter Midterm 2core: ";
        } while (midterm < 0 || midterm > 50);
        
        
        string message4 = "Please enter Final score: ";
        
        do
        {
               cout << message4;
               cin >> final;
               
               message4 = "\n\nError! Please Re-Enter Final score: ";
        } while (final < 0 || final > 100);
        
        
        return;
    
    } //getInput()
    
    
    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
    
    
    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
    
    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 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
    All i changed was the way it checked to see if the file was open from
    if(!file.eof()) to if(!file) alright cool.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    9
    oh, k, i tried it and appends now...thanks!

    now if i can get it to stop crashing =\

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    9
    n/m, i got it to work! thanks again!

    it's really long, but here it is:



    Code:
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    class Student //abstract data type implemented as a class
    {
          public: //prototypes for public member functions
          
                 Student(); //constructor function
           
                 
                 void setID(int);
                 void setFirstName(string);
                 void setLastName(string);
                 void setQuiz1(int);
                 void setQuiz2(int);
                 void setMidterm(int);
                 void setFinal(int);
                 
                 int getTotalpts();
                 double getpercent();
                 char getgrade();
           
           
                 
           private: //private data members accessible to member functions
                  
                  int studentID;
                  string first_name;
                  string last_name;
                  int quiz_1;
                  int quiz_2;
                  int midterm_score;
                  int final_score;
                  int totalpts;
                  double percent;
                  char grade;
                  
    };
    
    Student::Student() //default constructor
    {
                       
                   //do nothing
                                   
    }
    
    
    //member functions
    
    void Student::setID(int id)
    {
         studentID = id;
    } //end setID function
    
    
    void Student::setFirstName(string first)
    {
         first_name = first;
    } //end setFirstName function
    
    
    void Student::setLastName(string last)
    {
         last_name = last;
    } //end setLastName function
    
    
    void Student::setQuiz1(int quiz1)
    {
         quiz_1 = quiz1;
    } //end setQuiz1 function
    
    
    void Student::setQuiz2(int quiz2)
    {
         quiz_2 = quiz2;
    } //end setQuiz2 function
    
    
    void Student::setMidterm(int midterm)
    {
         midterm_score = midterm;
    } //end setMidterm function
    
    
    void Student::setFinal(int final)
    {
         final_score = final;
    } //end setFinal function
    
    
    int Student::getTotalpts()
    {
        //calculate totalpts
        totalpts = quiz_1 + quiz_2 + midterm_score + final_score;
        return totalpts;
    } //end gettotalpts()
    
    
    double Student::getpercent()
    {
           
           //calculate percent
           percent = (totalpts)*100/200;
           return percent;
    } //end getpercent()
    
    
    char Student::getgrade()
    {
         
         //calculate grade
         if (percent >= 90)
            return 'A';
         else if (percent >= 80)
              return 'B';
         else if (percent >= 70)
              return 'C';
         else if (percent >= 60)
              return 'D';
         else return 'F';
    } //end getgrade()
    
    
    int main()
    {
        void getInput(int &ID, string &firstName, string &lastName, int &firstQuiz, 
                      int &secondQuiz, int &midterm, int &final);
        bool moreStudents();
        void displayRecord(int ID, string firstName, string lastName, int firstQuiz,
                       int secondQuiz, int midterm, int final, int total,
                       double percentage, char finalgrade);
        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);
                       
        
        //declare variables
        
        //input
        int studentID;
        string first_name;
        string last_name;
        int quiz_1;
        int quiz_2;
        int midterm_score;
        int final_score;
        
        //calculation results
        int total_pts;
        double percent;
        char grade;
        
        //totals
        int totalStudents = 0;
        int highest = 0;
        int lowest = 0;
        int allPoints = 0;
        double avgPoints;
        int maxpts = 200;
        double avgPercent;
        char avgGrade;
    
         Student person;
         
         do
         {
                 getInput(studentID, first_name, last_name, quiz_1, quiz_2, midterm_score, final_score);
                 
                 //set input values
                 person.setID(studentID);
                 person.setFirstName(first_name);
                 person.setLastName(last_name);
                 person.setQuiz1(quiz_1);
                 person.setQuiz2(quiz_2);
                 person.setMidterm(midterm_score);
                 person.setFinal(final_score);
                 
                 //perform calculations
                 total_pts = person.getTotalpts();
                 percent = person.getpercent();
                 grade = person.getgrade();
                 lowest = computeLowestPoints(person.getTotalpts(), highest);
                 highest = computeHighestPoints(highest, person.getTotalpts());
                 totalStudents++; //adds 1 to totalStudents counter
                 allPoints = allPoints + person.getTotalpts(); //accumulates total class
                 avgPoints = computeAveragePoints(allPoints, totalStudents);
                 avgPercent = computeAveragePercent(avgPoints, maxpts);
                 avgGrade = computeGrade(avgPercent);
                 
         ofstream iFile("students.txt", ios::app);
         if (!iFile)
         {     
                 cout << "Error: Couldn't Append To End Of File" << endl;
         }else{
                 iFile << "\n\nStudent ID: " << studentID
                       << "\nFirst Name: " << first_name
                       << "\nLast Name: " << last_name
                       << "\nQuiz #1 Score: " << quiz_1
                       << "\nQuiz#2 Score: " << quiz_2
                       << "\nMidterm Score: " << midterm_score
                       << "\nFinal Score: " << final_score
                       << "\nTotal Points Earned: " << total_pts
                       << "\nOverall Percentage: " << percent << "%"
                       << "\nLetter Grade: " << grade;
         }
         cin.get();
         iFile.close();
                 
         }while(moreStudents()); //calls the moreStudents() function
         
         ifstream iFile("students.txt");
         char ch;
         while(!iFile.eof())
         {
                            iFile.get(ch);
                            cout << ch;
         }
         iFile.close();                   
         
         displayTotals(totalStudents, highest, lowest, avgPoints, avgGrade);
         
         system("PAUSE");
         return 0;
        
    } //end main()         
    
    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(int &ID, string &firstName, string &lastName, int &firstQuiz, 
                      int &secondQuiz, int &midterm, int &final)
    {
         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);        
         
         
         //asks user to input first and last name
    
                cout << "Enter First Name: ";
                cin >> firstName;
                
                
                cout << "Enter Last Name: ";
                cin >> lastName;
                
    //determines quiz #1 score
        string message1 = "Please enter Quiz #1 score: ";
        
        do
        {
               cout << message1;
               cin >> firstQuiz;
               
               message1 = "\n\nError! Please Re-Enter Quiz #1 score: ";
        } while (firstQuiz < 0 || firstQuiz > 25);
        
        
        string message2 = "Please enter Quiz #2 score: ";
        
        do
        {
               cout << message2;
               cin >> secondQuiz;
               
               message2 = "\n\nError! Please Re-Enter Quiz #2 2core: ";
        } while (secondQuiz < 0 || secondQuiz > 25);
        
        
        string message3 = "Please enter Midterm score: ";
        
        do
        {
               cout << message3;
               cin >> midterm;
               
               message3 = "\n\nError! Please Re-Enter Midterm 2core: ";
        } while (midterm < 0 || midterm > 50);
        
        
        string message4 = "Please enter Final score: ";
        
        do
        {
               cout << message4;
               cin >> final;
               
               message4 = "\n\nError! Please Re-Enter Final score: ";
        } while (final < 0 || final > 100);
        
        
        return;
    
    } //getInput()
    
    
    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
    
    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 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

  5. #5
    Registered User stuart_cpp's Avatar
    Join Date
    Apr 2006
    Location
    UK
    Posts
    31
    If it keeps on crashing, put ....
    Code:
    char c;
    cin >> c;
    At the end of the code, but before } obviously

  6. #6
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Quote Originally Posted by stuart_cpp
    If it keeps on crashing, put ....
    Code:
    char c;
    cin >> c;
    At the end of the code, but before } obviously
    Not specifically helpful - that's more in line with keeping the
    console open. If you want help with stopping it from crashing
    you'll need to post details of when/where its crashing
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    I haven't tried your code, but since you've just added file I/O, i suspect it's this which is causing you problems. firstly:
    Code:
         while(!iFile.eof())
         {
                            iFile.get(ch);
                            cout << ch;
         }
    You've probably been taught to "read until EOF", but this idiom is wrong in C++ - because streams don't "read ahead" - Consequently the EOF flag is not set until you actually attempt to read from the stream

    a better way is:
    Code:
         while(iFile.get(ch))
         {
                            cout << ch;
         }
    in other words - the expression which reads from the file should be part of the loop condition - this way, when you hit EOF, the error will be evaluated in your while condition straight away.


    Secondly, this strikes me as just a bit odd:
    Code:
         ofstream iFile("students.txt", ios::app);
         if (!iFile.eof())
         {     
                 cout << "Error: Couldn't Append To End Of File" << endl;
    EOF on an ostream doesn't really make much sense if you think about it - EOF is something that happens when you are reading from a file - so that condition is just bizarre (i'm amazed that my compiler didn't complain when i tried something similar in a toy program) - the line above will always be outputted, because ( !iFile.eof() ) will always return true.

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    34
    I would probably crass because of wrong inputting data like inputting a string instead of one word or a number instead of a string its all cased sensitive. Their are better ways to getting inputted data from user than cin>>input;

    you Could try something like.

    Code:
    //Headers Required//
    #include <string>
    //Code//
    do{
    string str;
    getline(cin,str);
    }while(str.empty());

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C program using structs to calculate grades
    By TampaTrinDM88 in forum C Programming
    Replies: 4
    Last Post: 07-06-2009, 01:33 PM
  2. C++ Program Classes Question
    By racerday182 in forum C++ Programming
    Replies: 6
    Last Post: 12-08-2008, 03:03 AM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 04:58 PM
  4. Grading Program
    By hockeydrummer88 in forum C++ Programming
    Replies: 6
    Last Post: 03-09-2005, 12:05 PM
  5. Replies: 2
    Last Post: 05-10-2002, 05:16 PM

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