如何调试这个C ++程序?

时间:2018-04-27 21:13:45

标签: c++ cin

特别是我在看207到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;
}

链接已删除,因为找不到..

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

我不知道如何在提示用户输入其他学生姓名后立即修正错误。就像代码编译并提示另一个学生的名字,但是当他们被显示时的成绩,它们显示为从上一个学生的成绩作为新学生成绩的累积成绩。

这是家庭作业的一部分。

1 个答案:

答案 0 :(得分:0)

你的问题在这里:

    getline(cin, stName);

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

实际上,您要求cin忽略1个字符,因为getline将在用户输入结束时占用换行符,缓冲区中当前没有要忽略的字符。不要那样做。你应该完全摆脱那里的cin.clear()cin.ignore行!