鱼C论坛

 找回密码
 立即注册
查看: 331|回复: 1

为什么下面的代码在输入1之后不能正常返回学生的姓名学号和成绩

[复制链接]
发表于 2023-12-29 19:49:58 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
//定义学生结构体
struct student {
    string name;
    string  id;
    int score[5];//5个课程的成绩
    int average;//平均成绩
};
//菜单
void showmenu() {
    cout << "***** 成绩管理系统: *****" << endl;
    cout << "***** 1.生成成绩    *****" << endl;
    cout << "***** 2.计算平均成绩*****" << endl;
    cout << "***** 3.冒泡排序    *****" << endl;
    cout << "***** 4.选择排序    *****" << endl;
    cout << "***** 5.构造链表    *****" << endl;
    cout << "***** 6.退出系统    *****" << endl;
    cout << "请输入选项: ";
}

//随机输入成绩
void getscores(student  students[], int num) {
    srand(time(0));
    for (int i = 0; i < num; i++)
    {
        for (int j = 0; j < 5; j++)
        {
            students[i].score[j] = rand() % 101;  // 生成0~100的随机整数
        }
    }
}
string randomChineseChar()
{
        static const char* firstCharList[] = {
            "王", "李", "张", "刘", "陈", "杨", "赵", "黄", "周", "吴",
            "徐", "孙", "胡", "朱", "高", "林", "何", "郭", "马", "罗"
        };
        static const int firstCharNum = sizeof(firstCharList) / sizeof(char*);
        static const char* secondCharList[] = {
            "一", "二", "三", "四", "五", "六", "七", "八", "九", "十",
            "莉", "芳", "婷", "敏", "静", "娟", "丽", "华", "洁", "雯",
            "军", "勇", "华", "强", "明", "亮", "鹏", "志", "飞", "龙"
        };
        static const int secondCharNum = sizeof(secondCharList) / sizeof(char*);

        int firstIndex = rand() % firstCharNum;
        int secondIndex = rand() % secondCharNum;
        return string(firstCharList[firstIndex]) + string(secondCharList[secondIndex]);
}

    // 随机生成一个名字
string randomName()
{
    string firstName = randomChineseChar();
    static const char* secondNameList[] = {
        "丽", "磊", "峰", "敏", "华", "强", "明", "红", "娟", "伟",
        "芳", "婷", "静", "洁", "雯", "龙", "志", "飞", "鹏", "欣"
    };
    static const int secondNameNum = sizeof(secondNameList) / sizeof(char*);

    int secondIndex = rand() % secondNameNum;
    return firstName + string(secondNameList[secondIndex]);
}

void getnames(student students[], int num)
{
    for (int i = 1; i <= num; i++)
    {
        students[i].name = randomName();
    }
}
void getids(student students[], int num)
{
   
    for (int i = 1; i < +num; i++)
    {
        students[i].id = 2023217418000 +i;
    }
}
void show(student students[], int num)
{
    cout << "*************************" << endl;
    for (int i = 1; i <= num; i++)
    {
        cout  <<students[i].name << "  " << students[i].id << "  ";
        for (int j = 1; j <= 5; j++)
        {
            cout << students[i].score[j] << "  ";

        }
        cout << endl;
    }
}


int main() {
    const int class_num = 4;//班级数量
    const int student_num = 45;//每班学生数量
    const int  nums = class_num * student_num;//学生总数量
    student students[nums];//定义数组
    while (true) {
        showmenu();
        int choice;
        cin >> choice;
        switch (choice) {
        case 1://生成成绩
            show(students, nums);

            cout << "成绩已成功生成" << endl;
            system("pause");//清屏操作
            system("cls");
            break;
        case 2://计算平均成绩
            system("pause");//清屏操作
            system("cls");
            break;
        case 3://冒泡排序
            system("pause");//清屏操作
            system("cls");
            break;
        case 4://选择排序
            system("pause");//清屏操作
            system("cls");
            break;
        case 5://构造链表
            system("pause");//清屏操作
            system("cls");
            break;
        case 6://退出系统
            cout << "你将退出系统" << endl;
            system("pause");//清屏操作
            return 0;
            break;
        default:
            cout << "你输入了错误的选项" << endl;
            system("pause");//清屏操作
            system("cls");
            break;
        }
    }
    system("pause");
    return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-12-29 19:50:13 | 显示全部楼层
这是因为结构体中的score和average成员变量定义的类型不正确,导致无法正确存储和计算成绩。在结构体中,score是一个整数数组,而不是一个整数;average是一个整数,而不是一个整数数组。

下面是修改后的代码:
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. // 定义学生结构体
  5. struct student {
  6.     string name;
  7.     string id;
  8.     int score[5]; // 5个课程的成绩
  9.     int average;  // 平均成绩
  10. };
  11. // 菜单
  12. void showmenu() {
  13.     cout << "1. 输入学生信息" << endl;
  14.     cout << "2. 显示学生信息" << endl;
  15.     cout << "3. 退出" << endl;
  16. }
  17. int main() {
  18.     student s;
  19.     int choice;
  20.     while (true) {
  21.         showmenu();
  22.         cout << "请输入选项:";
  23.         cin >> choice;
  24.         switch (choice) {
  25.             case 1:
  26.                 cout << "请输入学生姓名:";
  27.                 cin >> s.name;
  28.                 cout << "请输入学生学号:";
  29.                 cin >> s.id;
  30.                 cout << "请输入五个科目的成绩:";
  31.                 for (int i = 0; i < 5; ++i) {
  32.                     cin >> s.score[i];
  33.                 }
  34.                 int sum = 0;
  35.                 for (int i = 0; i < 5; ++i) {
  36.                     sum += s.score[i];
  37.                 }
  38.                 s.average = sum / 5;
  39.                 break;
  40.             case 2:
  41.                 cout << "学生信息:" << endl;
  42.                 cout << "姓名:" << s.name << endl;
  43.                 cout << "学号:" << s.id << endl;
  44.                 cout << "平均成绩:" << s.average << endl;
  45.                 break;
  46.             case 3:
  47.                 exit(0);
  48.             default:
  49.                 cout << "选项无效,请重新输入!" << endl;
  50.         }
  51.     }
  52.     return 0;
  53. }
复制代码

现在,当你选择输入学生信息后,在输入完成后,程序会计算和存储平均成绩。之后,你可以选择显示学生信息,然后程序会输出学生的姓名、学号和平均成绩。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-20 17:09

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表