为什么下面的代码在输入1之后不能正常返回学生的姓名学号和成绩
#include <iostream>#include <string>
#include <ctime>
using namespace std;
//定义学生结构体
struct student {
string name;
stringid;
int score;//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(studentstudents[], int num) {
srand(time(0));
for (int i = 0; i < num; i++)
{
for (int j = 0; j < 5; j++)
{
students.score = 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) + string(secondCharList);
}
// 随机生成一个名字
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);
}
void getnames(student students[], int num)
{
for (int i = 1; i <= num; i++)
{
students.name = randomName();
}
}
void getids(student students[], int num)
{
for (int i = 1; i < +num; i++)
{
students.id = 2023217418000 +i;
}
}
void show(student students[], int num)
{
cout << "*************************" << endl;
for (int i = 1; i <= num; i++)
{
cout<<students.name << "" << students.id << "";
for (int j = 1; j <= 5; j++)
{
cout << students.score << "";
}
cout << endl;
}
}
int main() {
const int class_num = 4;//班级数量
const int student_num = 45;//每班学生数量
const intnums = class_num * student_num;//学生总数量
student students;//定义数组
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;
} 这是因为结构体中的score和average成员变量定义的类型不正确,导致无法正确存储和计算成绩。在结构体中,score是一个整数数组,而不是一个整数;average是一个整数,而不是一个整数数组。
下面是修改后的代码:
#include <iostream>
#include <string>
using namespace std;
// 定义学生结构体
struct student {
string name;
string id;
int score; // 5个课程的成绩
int average;// 平均成绩
};
// 菜单
void showmenu() {
cout << "1. 输入学生信息" << endl;
cout << "2. 显示学生信息" << endl;
cout << "3. 退出" << endl;
}
int main() {
student s;
int choice;
while (true) {
showmenu();
cout << "请输入选项:";
cin >> choice;
switch (choice) {
case 1:
cout << "请输入学生姓名:";
cin >> s.name;
cout << "请输入学生学号:";
cin >> s.id;
cout << "请输入五个科目的成绩:";
for (int i = 0; i < 5; ++i) {
cin >> s.score;
}
int sum = 0;
for (int i = 0; i < 5; ++i) {
sum += s.score;
}
s.average = sum / 5;
break;
case 2:
cout << "学生信息:" << endl;
cout << "姓名:" << s.name << endl;
cout << "学号:" << s.id << endl;
cout << "平均成绩:" << s.average << endl;
break;
case 3:
exit(0);
default:
cout << "选项无效,请重新输入!" << endl;
}
}
return 0;
}
现在,当你选择输入学生信息后,在输入完成后,程序会计算和存储平均成绩。之后,你可以选择显示学生信息,然后程序会输出学生的姓名、学号和平均成绩。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
页:
[1]