#include <iostream>
#include <conio.h>
#include <iomanip>
#include <fstream>
using namespace std;

const int numStudents = 5, numQuiz = 10, numAssign = 10, numFinal = 1;
const int totalGrades = 21;
void computeStudAver(const int grade[][numQuiz], double stAve[]);
void computeQuizAver(const int grade[][numQuiz], double quizAve[]);
void printall(const int grade[][numQuiz]);

int menu();
int main();

int menu()
{
    char choice;
    cout << "S. Start a new semester."<<endl;
    cout << "O. Output Data."<<endl;
    cout << "E. Exit"<< endl << endl;
    cin >> choice;
    cout << endl << endl;
    return choice = main();
}

int calcgrade(double average, char& grade)
{
    if (average <= 59)
    grade = 'F';
    else if (average <= 69 && average >= 60)
    grade = 'D';
    else if (average <= 79 && average >= 70)
    grade = 'C';
    else if (average <= 89 && average >= 80)
    grade = 'B';
    else if (average >= 90 && average <= 100)
    grade = 'A';
    return 0;
}

//void scales() { // Final = 33%; Quizzes = 33%; Assignments = 33%
//    int quiz = .3 * qgrade = double q
    

void inputgrades() {
    char fanswer;
    int quizN, assignG, final;
    double qgrade[4], agrade[4], fgrade[1],qsum;
    
    cout << "How many quiz grades will you add per student?";
    cin >> quizN;
    for (int i = 1; i <= quizN; i++) {
         cout << "Enter quiz grade: " << endl;
         cin >> ++qgrade[i];
    qsum =+qgrade[i];
    }

    cout << "How many assignment grades will you add per student?";
    cin >> assignG;
    for (int i = 1; i <= assignG; i++) {
         cout << "Enter assignment grade: " << endl;
         cin >> agrade[i];
    }

    cout << "Is there a final?";
    cin >> fanswer;
    if (fanswer == 'y' || fanswer == 'Y') {
         cout << "Enter final grade: " << endl;
         cin >> fgrade[1];
    }

    //scales();
}

void computeStudAver(const int grade[][numQuiz], double stAve[]) {
     int stNum;
     for (int stN = 1; stN <= numStudents; stNum++) {
         double sum = 0;
         for (int quizNum = 1; quizNum <= numQuiz; quizNum++)
             sum = sum + grade[stNum-1][quizNum-1];
         stAve[stNum-1] = sum/numQuiz;
         }
}

void computeQuizAver(const int grade[][numQuiz], double quizAve[]) {
     for (int quizNum = 1; quizNum <= numQuiz; quizNum++) {
         double sum = 0;
         for (int stN = 1; stN <= numStudents; stN++)
             sum = sum + grade[stN-1][quizNum-1];
         quizAve[quizNum-1] = sum/numStudents;
     }
}

void printall(const int grade[][numQuiz], const double stAve[], const double quizAve[]) {
     cout.setf(ios::fixed);
     cout.setf(ios::showpoint);
     cout.precision(1);
    
     ofstream outFile ("GRADEDATA.txt");
    
     outFile.open("C:\\Documents and Settings\\USER\\Desktop\\output1.txt");
     outFile << setw(10) << "Student" << setw(5) << "Average" << setw(15) << "Quizzes\n";
     for (int stN = 1; stN <= numStudents; stN++) {
              cout << setw(10) << stN << setw(5) << stAve[stN-1] << " ";
         for (int quizNum = 1; quizNum <= numQuiz; quizNum++)
             cout << setw(5) << grade[stN-1][quizNum-1];
         cout << endl;
         }

     cout << "Quiz averages = ";
     for (int quizNum = 1; quizNum <= numQuiz; quizNum ++)
         cout << setw(5) << quizAve[quizNum-1];
     cout << endl;
}

int main()
{
    int grade[numStudents][numQuiz];
    double stAve[numStudents];
    double quizAve[numQuiz];
    
    char choice;
    cout << "S. Start a new semester."<<endl;
    cout << "O. Output Data."<<endl;
    cout << "E. Exit"<< endl << endl;
    cin >> choice;
    cout << endl << endl;

        switch(choice)
        {
        case 'S':
        case 's':
            cout << "Welcome. You have started a new semester!\n";
            inputgrades();
            break;

        case 'O':
        case 'o':
            printall(grade, stAve, quizAve);
            break;

        case 'E':
        case 'e':
            cout << "Quitting..." << endl;
            break;

        default:
            cout << "You must pick one to continue!" << endl;
            break;
        }
    
    computeStudAver(grade, stAve);
    computeQuizAver(grade, quizAve);
    printall(grade, stAve, quizAve);
    getche();
    return 0;
}

Recommended Answers

All 3 Replies

please help me debug this cause I cant find the reason why it promp an error when i try to output the data in a text base format!! ?

this program is running actually but it has some errors!

help!

For starters, what is this suppoed to be doing in function menu() return choice = main(); ?? Just return the value choice.

It would help if you gave a better description of the error that occurs, but I'm betting you get a debugging message about your array fgrade[] .
Your function:

void inputgrades() {
    char fanswer;
    int quizN, assignG, final;
    double qgrade[4], agrade[4], fgrade[1],qsum;

    cout << "How many quiz grades will you add per student?";
    cin >> quizN;
    for (int i = 1; i <= quizN; i++) {
        cout << "Enter quiz grade: " << endl;
        cin >> ++qgrade[i];
        qsum =+qgrade[i];
    }

    cout << "How many assignment grades will you add per student?";
    cin >> assignG;
    for (int i = 1; i <= assignG; i++) {
        cout << "Enter assignment grade: " << endl;
        cin >> agrade[i];
    }

    cout << "Is there a final?";
    cin >> fanswer;
    if (fanswer == 'y' || fanswer == 'Y') {
        cout << "Enter final grade: " << endl;
        cin >> fgrade[1];
    }

    //scales();
}

Note that you allocate arrays of size 4, or in the case of fgrade, size 1. You begin accessing them at index 1, up to or equal a value entered by the user. Several problems here.

First, remember that array indexes run from 0 up to size-1, so your size 4 arrays have indexes (or indices, if you prefer) 0 through 3. Your fgrade array, of size 1, has only index 0. (An array of size 1 is really a pretty useless thing, unless you absolutely have to treat storage of one value as an array.) So, when you store to the fgrade array, you are writing to memory past the end of what's allocated to it. You don't see this happening with the other arrays, if the user does not want to enter more than three quiz or assignment grades. Try to enter a fourth one and.....

Which brings us to another potential problem - you do nothing to verify that the user's input is in fact correct based on the size of those arrays. User could enter 6 or 10 for the number of assignments, and your code will just go on and try to store grades somewhere. It's vital that you check user input that will be used as array indexing, and reject any input that will go out of bounds.

Below is a portion of your function, beefed up to be more user resistant:

void inputgrades() {
    char fanswer;
    int quizN, assignG, final;
    double qgrade[4], agrade[4], fgrade[1],qsum = 0; //init qsum!!

    cout << "How many quiz grades will you add per student?";
    cin >> quizN;
    while( quizN <1 || quizN > 4 )
    {
        cout << "That's too many.  You can have 1 to 4 quizes: ";
        cin >> quizN;
    }
    //note the 0 to strictly less than quizN range
    for (int i = 0; i < quizN; i++) 
    {
        cout << "Enter quiz grade " << i+1 << " : " << endl;
        cin >> ++qgrade[i];
        qsum =+qgrade[i];
    }

// rest of function omitted, follow the model above
}

I think you'll find you're making similar mistakes with arrays in other parts of your program. Reread your textbook's section on arrays.

And finally, the function is called inputgrades( ), and from its use in main( ) I'd expect it to get the grades that other parts of the program will act upon. Unfortunately, your grade data is stored in arrays that are local to this function - they cease to exist once the function is completed. You must declare the arrays in main( ) and pass them as parameters to this function.

thank you for the sample

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.