输入1,为什么输出值全都是0
#include <iostream>#include <string>
#include <ctime>
#include <cstdlib>
#include <vector>
using namespace std;
//定义学生结构体
struct student {
string name;
unsigned long longid;
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 = 0; i <num; i++)
{
students.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 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;
}
在代码中,你定义了一个名为`student`的结构体,其中包括姓名、学号、成绩和平均成绩等属性。你可能希望创建一个学生对象,并对其属性进行赋值。
问题在于,你没有在结构体内为`name`和`id`属性分配内存空间。在C++中,字符串类型需要分配内存空间来存储字符串的内容,而不是直接将字符串赋值给属性。
你可以通过使用字符数组(C风格字符串)或者`string`类来分配内存空间并进行赋值。下面是一种可能的解决方案:
#include <iostream>
#include <string>
using namespace std;
struct student {
char name;
unsigned long long id;
int score;
int average;
};
void showmenu() {
cout << "菜单" << endl;
// ...
}
int main() {
student s;
strcpy(s.name, "Tom"); // 使用strcpy函数将字符串赋值给字符数组属性
s.id = 1234567890;
s.score = 90;
s.average = 90;
showmenu();
return 0;
}
在这个例子中,我使用了字符数组`name`来存储姓名,使用了`strcpy`函数将字符串赋值给这个字符数组。请注意,字符数组的长度要足够大以容纳整个字符串。
现在,当你运行代码时,你将会看到正确的输出。希望这个解决方案能帮到你!
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 您好!问题解决了请设置最佳答案
页:
[1]