-2

In particular I am looking at lines 207 to 219:

// ZakAttack.cpp--A program for calculating student grades and
// displaying the overall GPA
// Shaheen Fathima Abdul Jamal Nazar, CISP 360
// Instructor: Professor Fowler
// Date: 04/27/2018

#include <iomanip>
#include <iostream>
#include <limits>
#include <string>
using namespace std;

// Variable Declarations
string stName;
int scores[50];
int *ptr = scores;
int count = 0;

// Function Prototypes
void results();
void gradeAdd();
void mainMenu();
int getUserChoice();
void mainEvent();

int main() {
    // Program Greeting
    cout << " Hi! Welcome to the Zak Attack Program!\n" << endl;
    cout << " This program calculates the grades for a\n" << endl;
    cout << " specific number of quizzes taken by a student.\n" << endl;
    cout << " It also displays the overall grades along with\n" << endl;
    cout << " the letters (A-F) based on the scores attained by the student! "
         << endl;
    cout << endl;

    cout << " Enter the name of the student (First and Last): ";
    getline(cin, stName);
    cout << endl;

    mainEvent();

    return 0;
}

void mainEvent() {
    int userInput;
    bool task = false;

    do {
        mainMenu();
        cout << "\nEnter your choice: ";
        userInput = getUserChoice();

        if (userInput == 1) {
            gradeAdd();
        } else if (userInput == 2) {
            results();
            break;
        } else if (userInput == 3) {
            task = true;
        }
    } while (task == false);
}

void results() {
    // variables to be used
    int tem, sNum, rNum, pNum;
    bool swapNum;
    int scCopy[50];
    int *ptrCopy = scCopy;
    int lowScore[15];
    int *ptrLow = lowScore;
    double gpAvg = 0;
    char rChoice;

    cout << " Name of Student: " << stName << endl;

    // Copying scores[] to scCopy[] array
    for (int i = 0; i < count; i++) {
        *(ptrCopy + i) = *(ptr + i);
    }
    do {
        swapNum = false;
        for (int j = 0; j < count; j++) {
            if (*(ptrCopy + j) > *(ptrCopy + (j + 1))) {
                tem = *(ptrCopy + j);
                *(ptrCopy + j) = *(ptrCopy + (j + 1));
                *(ptrCopy + (j + 1)) = tem;
                swapNum = true;
            }
        }
    } while (swapNum);
    sNum = (count * 0.3);
    rNum = count;
    pNum = sNum;

    for (int i = 0; i < sNum; i++) {
        *(ptrLow + i) = *(ptrCopy + i);
        *(ptrCopy + i) = 0;
        pNum--;
    }

    // Display the grades as letters and overall GPA
    for (int i = 0; i < count; i++) {
        if (*(ptrCopy + i) >= 94) {
            cout << *(ptrCopy + i) << ": A " << endl;
            gpAvg += 4;
        } else if (*(ptrCopy + i) >= 90 && *(ptrCopy + i) < 94) {
            cout << *(ptrCopy + i) << ": A- " << endl;
            gpAvg += 3.7;
        } else if (*(ptrCopy + i) >= 87 && *(ptrCopy + i) < 90) {
            cout << *(ptrCopy + i) << ": B+ " << endl;
            gpAvg += 3.3;
        } else if (*(ptrCopy + i) >= 83 && *(ptrCopy + i) < 87) {
            cout << *(ptrCopy + i) << ": B " << endl;
            gpAvg += 3;
        } else if (*(ptrCopy + i) >= 80 && *(ptrCopy + i) < 83) {
            cout << *(ptrCopy + i) << ": B- " << endl;
            gpAvg += 2.7;
        } else if (*(ptrCopy + i) >= 77 && *(ptrCopy + i) < 80) {
            cout << *(ptrCopy + i) << ": C+ " << endl;
            gpAvg += 2.3;
        } else if (*(ptrCopy + i) >= 73 && *(ptrCopy + i) < 77) {
            cout << *(ptrCopy + i) << ": C " << endl;
            gpAvg += 2;
        } else if (*(ptrCopy + i) >= 70 && *(ptrCopy + i) < 73) {
            cout << *(ptrCopy + i) << ": C- " << endl;
            gpAvg += 1.7;
        } else if (*(ptrCopy + i) >= 67 && *(ptrCopy + i) < 70) {
            cout << *(ptrCopy + i) << ": D+ " << endl;
            gpAvg += 1.3;
        } else if (*(ptrCopy + i) >= 60 && *(ptrCopy + i) < 67) {
            cout << *(ptrCopy + i) << ": D " << endl;
            gpAvg += 1;
        } else if (*(ptrCopy + i) > 1 && *(ptrCopy + i) < 60) {
            cout << *(ptrCopy + i) << ": F " << endl;
        }
    }
    cout << "*******************" << endl;

    // Dropped scores
    for (int i = 0; i < sNum; i++)
        cout << *(ptrLow + i) << " [Dropped Score] " << endl;

    // Calculation of GPA and displaying results
    rNum -= sNum;
    cout << fixed << setprecision(2) << endl;
    gpAvg = (gpAvg / rNum);
    cout << " Grade Point Average (GPA): " << gpAvg << endl;
    cout << endl;

    if (gpAvg == 4) {
        cout << " Grade: A" << endl << endl;
        cout << " Excellent Job! " << endl;
        cout << " Keep up the good progress! " << endl;
    } else if (gpAvg < 4 && gpAvg > 3.67) {
        cout << " Grade: A-" << endl << endl;
        cout << " Good Score! " << endl;
        cout << " Keep Going! " << endl;
    } else if (gpAvg < 3.67 && gpAvg > 3.33) {
        cout << " Grade: B+" << endl << endl;
        cout << " Good Work! " << endl;
        cout << " Study a little more. " << endl;
    } else if (gpAvg < 3.33 && gpAvg > 3) {
        cout << " Grade: B" << endl << endl;
        cout << " Good " << endl;
        cout << " Need to study even more! " << endl;
    } else if (gpAvg < 3 && gpAvg > 2.67) {
        cout << " Grade: B- " << endl << endl;
        cout << " Well done " << endl;
        cout << " but need to work hard, " << endl;
        cout << " since you are close to a C! " << endl;
    } else if (gpAvg < 2.67 && gpAvg > 2.33) {
        cout << " Grade: C+ " << endl << endl;
        cout << " Looks like the grades are slipping " << endl;
        cout << " Need to spend more time studying! " << endl;
    } else if (gpAvg < 2.33 && gpAvg > 2) {
        cout << " Grade: C " << endl << endl;
        cout << " Getting low! " << endl;
        cout << " Need to work even harder! " << endl;
    } else if (gpAvg < 2 && gpAvg > 1.67) {
        cout << " Grade: C- " << endl << endl;
        cout << " Risky! Gotta study even more! " << endl;
    } else if (gpAvg < 1.67 && gpAvg > 1.33) {
        cout << " Grade: D+ " << endl << endl;
        cout << " Going low on your " << endl;
        cout << " chances of passing! " << endl;
        cout << " Work even more harder! " << endl;
    } else if (gpAvg < 1.33 && gpAvg > 1) {
        cout << " Grade: D " << endl;
        cout << " Chances of passing the class " << endl;
        cout << " are getting even lower! " << endl;
        cout << " Concentrate and study or seek help " << endl;
        cout << " from a tutor! " << endl;
    } else if (gpAvg < 1 && gpAvg > 0.67) {
        cout << " Grade: D- " << endl;
        cout << " Nearly no chances of passing! " << endl;
    } else if (gpAvg < 0.67) {
        cout << " Grade: F " << endl;
        cout << " Disappointing and dejecting! " << endl;
        cout << " Try Again or Opt for something else " << endl;
    }

    cout << " Would you like to enter the grades for another student?";
    cin >> rChoice;

    if (rChoice == 'Y') {
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cout << " Enter name of the student (First and Last): ";
        getline(cin, stName);

        cin.clear();
        cin.ignore();
        mainEvent();
    } else if (rChoice == 'N') {
        cout << " Good Bye! " << endl;
    }
}

void gradeAdd() {
    cout << "\nEnter quiz # " << (count + 1) << " score: ";
    cin >> *(ptr + count);
    while (*(ptr + count) > 100 || *(ptr + count) < 0) {
        cout << " Sorry! Invalid Input! Please enter a value from 0-100: "
             << endl;
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cin >> *(ptr + count);
    }
    count++;
}

void mainMenu() {
    cout << "1. Add a grade" << endl;
    cout << "2. Display Results" << endl;
    cout << "3. Quit" << endl;
}

int getUserChoice() {
    int userInput;

    cin >> userInput;

    while (userInput != 1 && userInput != 2 && userInput != 3) {
        cout << " Invalid Choice! Please enter a choice from 1 to 3: " << endl;
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cin >> userInput;
    }
    return userInput;
}

Link removed since its not found..

https://repl.it/@Shaheen_Fathima/EducatedJubilantBusiness

I don't know how to fix the error right after the prompt asking the user to enter another student's name. Like the code compiles and prompts another student's name but the grades when they are displayed they appear as a cumulative grade from the previous student's grades as the new student's grade.

It's part of a homework.

Mohideen bin Mohammed
  • 18,813
  • 10
  • 112
  • 118
Shaheen
  • 1
  • 3
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Apr 27 '18 at 21:29
  • 2
    Here are some good texts on how to debug: https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ https://stackoverflow.com/questions/2069367/how-to-debug-using-gdb and somewhat tangentially https://ericlippert.com/2014/03/21/find-a-simpler-problem/ – Yunnosch Apr 27 '18 at 21:30

1 Answers1

0

Your problem is here:

    getline(cin, stName);

    cin.clear();
    cin.ignore();
    mainEvent();

You ask cin to ignore 1 character when in fact, since getline will eat the newline at the end of the user input, there are no characters currently in the buffer to ignore. Don't do that. You should get rid of both the cin.clear() and cin.ignore lines there entirely!

scohe001
  • 15,110
  • 2
  • 31
  • 51