You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++ GWA计算器开发:存储多课程名称并关联学分成绩展示问题

解决方案

核心问题分析

你当前代码的核心问题是仅使用单个字符数组c_name[50]存储课程名称,每次输入新课程时会覆盖之前的内容,导致最终只能显示最后一门课的名字。此外还有几处不符合标准C++规范的写法和小bug,以下是修复方案:

关键修改点

  • 存储多门课程名称:将单个字符数组替换为vector<string>,用于动态存储所有课程的名称,避免覆盖问题。
  • 替换非标准可变长度数组:C++标准不支持float unit[q]这类可变长度数组,改用vector<float>存储学分和成绩,更安全且符合规范。
  • 修复结果展示逻辑:在输出部分添加循环,逐行打印每门课程的名称、学分和成绩。
  • 补充必要头文件:添加<iostream>、<string>、<vector>头文件,支持输入输出、字符串和动态数组功能。
  • 修复笔误与不良写法:修正calculateGPA()为calculateGWA();避免递归调用main(),改用循环实现菜单逻辑;移除不必要的goto语句。

修复后的完整代码

#include <iostream>
#include <string>
#include <vector>
#include <stdlib.h>

using namespace std;

void calculateGWA();
void showMainMenu();

int main() {
    showMainMenu();
    return 0;
}

void showMainMenu() {
    int input;
    do {
        system("cls");
        cout << "		 -------------------------------------------------------------------------- " << endl;
        cout << "		|                                GWA Calculator                           |" << endl;
        cout << "		 -------------------------------------------------------------------------- " << endl;
        cout << "		|            MENU:                                                          |" << endl;
        cout << "		|                   1. Calculate GWA (General Weighted Average)             |" << endl;
        cout << "		|                   2. Calculate CGWA (Cumulative Weighted Average)         |" << endl;
        cout << "		|                   3. Exit Application                                     |" << endl;
        cout << "		|                                                                         |" << endl;
        cout << "		 -------------------------------------------------------------------------- " << endl;
        
        cout << "		Enter your choice: ";
        cin >> input;

        switch(input) {
            case 1:
                calculateGWA();
                break;
            case 2:
                // calculateCGWA();
                cout << "\nCGWA功能暂未实现,按任意键返回主菜单..." << endl;
                cin.ignore();
                cin.get();
                break;
            case 3:
                exit(EXIT_SUCCESS);
                break;
            default:
                cout << "输入错误,请重试!\n" << endl;
                cin.ignore();
                cin.get();
                break;
        }
    } while(true);
}

void calculateGWA() {
    int q;
    system("cls");
    cout << "-------------- GWA Calculator -----------------" << endl;
    cout << " How many course(s)?: ";
    cin >> q;

    vector<string> c_name(q);
    vector<float> unit(q);
    vector<float> grade(q);

    cout << endl;
    for(int i = 0; i < q; i++) {
        cout << "Enter the Course Name " << i+1 << ": ";
        cin >> c_name[i];
        cout << "Enter the Unit " << c_name[i] << ": ";
        cin >> unit[i];
        cout << "Enter the Grade " << c_name[i] << ": ";
        cin >> grade[i];
        cout << "-----------------------------------\n" << endl;
    }

    float sum = 0;
    for(int j = 0; j < q; j++) {
        sum += unit[j] * grade[j];
    }

    float totCr = 0;
    for(int k = 0; k < q; k++) {
        totCr += unit[k];
    }
    system("cls");

    // 打印课程信息与GWA结果
    cout << "		 =============================================================== " << endl;
    cout << "		|           COURSE             |      UNIT      |     GRADE     |" << endl;
    cout << "		 =============================================================== " << endl;
    for(int i = 0; i < q; i++) {
        cout << "		| " << c_name[i];
        // 格式化对齐,让输出更美观
        for(int len = c_name[i].size(); len < 25; len++) cout << " ";
        cout << "|      " << unit[i] << "      |     " << grade[i] << "     |" << endl;
    }
    cout << "		 =============================================================== " << endl;
    cout << "\nTotal Points: " << sum << "\nTotal Credits: " << totCr << "\nTotal GWA: " << sum/totCr << "." << endl;

    int inmenu;
    do {
        cout << "\n\n1. Calculate Again" << endl;
        cout << "2. Go Back to Main Menu" << endl;
        cout << "3. Exit This App\n" << endl;
        cout << "Your Input: ";
        cin >> inmenu;

        switch(inmenu) {
            case 1:
                calculateGWA();
                return;
            case 2:
                return;
            case 3:
                exit(EXIT_SUCCESS);
            default:
                cout << "\n输入错误,请重新选择!" << endl;
                break;
        }
    } while(true);
}

说明

  • 用vector<string>存储课程名称,每门课的名字都会被保留,不会被覆盖。
  • 结果展示部分添加了格式化对齐,让输出更整齐易读。
  • 菜单逻辑改用do-while循环替代goto和递归调用,代码更易维护。
  • 修复了原代码中的笔误和未实现功能的提示。

内容的提问来源于stack exchange,提问作者jeck.98

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 16:15:27