鱼C论坛

 找回密码
 立即注册
查看: 394|回复: 2

[已解决]输入1,为什么输出值全都是0

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

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

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

x
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <vector>

using namespace std;
//定义学生结构体
struct student {
    string name;
    unsigned long long  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 = 0; i <num; i++)
    {
        students[i].id = 2023217000ULL +1;
    }
}
void show(student students[], int num)
{
    cout << "*************************" << endl;
    for(int i=0;i<num;i++)
    {
        cout << students->id<<"  ";
        
    }
}


int main() {
    srand(time(nullptr));
    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;
}
最佳答案
2023-12-29 21:12:26
在代码中,你定义了一个名为`student`的结构体,其中包括姓名、学号、成绩和平均成绩等属性。你可能希望创建一个学生对象,并对其属性进行赋值。

问题在于,你没有在结构体内为`name`和`id`属性分配内存空间。在C++中,字符串类型需要分配内存空间来存储字符串的内容,而不是直接将字符串赋值给属性。

你可以通过使用字符数组(C风格字符串)或者`string`类来分配内存空间并进行赋值。下面是一种可能的解决方案:
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. struct student {
  5.     char name[50];
  6.     unsigned long long id;
  7.     int score;
  8.     int average;
  9. };
  10. void showmenu() {
  11.     cout << "菜单" << endl;
  12.     // ...
  13. }
  14. int main() {
  15.     student s;
  16.     strcpy(s.name, "Tom"); // 使用strcpy函数将字符串赋值给字符数组属性
  17.     s.id = 1234567890;
  18.     s.score = 90;
  19.     s.average = 90;
  20.     showmenu();
  21.     return 0;
  22. }
复制代码

在这个例子中,我使用了字符数组`name[50]`来存储姓名,使用了`strcpy`函数将字符串赋值给这个字符数组。请注意,字符数组的长度要足够大以容纳整个字符串。

现在,当你运行代码时,你将会看到正确的输出。希望这个解决方案能帮到你!

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

使用道具 举报

发表于 2023-12-29 21:12:26 | 显示全部楼层    本楼为最佳答案   
在代码中,你定义了一个名为`student`的结构体,其中包括姓名、学号、成绩和平均成绩等属性。你可能希望创建一个学生对象,并对其属性进行赋值。

问题在于,你没有在结构体内为`name`和`id`属性分配内存空间。在C++中,字符串类型需要分配内存空间来存储字符串的内容,而不是直接将字符串赋值给属性。

你可以通过使用字符数组(C风格字符串)或者`string`类来分配内存空间并进行赋值。下面是一种可能的解决方案:
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. struct student {
  5.     char name[50];
  6.     unsigned long long id;
  7.     int score;
  8.     int average;
  9. };
  10. void showmenu() {
  11.     cout << "菜单" << endl;
  12.     // ...
  13. }
  14. int main() {
  15.     student s;
  16.     strcpy(s.name, "Tom"); // 使用strcpy函数将字符串赋值给字符数组属性
  17.     s.id = 1234567890;
  18.     s.score = 90;
  19.     s.average = 90;
  20.     showmenu();
  21.     return 0;
  22. }
复制代码

在这个例子中,我使用了字符数组`name[50]`来存储姓名,使用了`strcpy`函数将字符串赋值给这个字符数组。请注意,字符数组的长度要足够大以容纳整个字符串。

现在,当你运行代码时,你将会看到正确的输出。希望这个解决方案能帮到你!

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

使用道具 举报

发表于 2023-12-29 21:16:27 | 显示全部楼层
您好!问题解决了请设置最佳答案
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-20 20:26

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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