gpt4 book ai didi

c++ - 存储2人c++信息

转载 作者:行者123 更新时间:2023-11-28 05:54:37 29 4
gpt4 key购买 nike

这是我存储2个大学生信息的源代码。我想从输入中获取每个人的姓名、姓氏、ID 和 5 分,然后在输出中显示它们。我在输出中显示分数时遇到问题。

请帮忙

#include <iostream>

using namespace std;

struct collegian{
char name[10];
char lastname[15];
int id;
int scores;
int roll;
};

int main(){
struct collegian s[2];
int i;
int j;
cout << "1. Enter First & Last Name\n";
cout << "2. Enter Collegian Number\n";
cout << "3. Enter Collegian Scores of 5\n";
cout << "4. App Will Show You What Received\n\n";
cout << "------------------------------------"<< endl;
for(i=0;i<2;++i)
{

s[i].roll=i+1;
cout << endl << "Collegian #" << s[i].roll << endl;
cout << "Name : ";
cin >> s[i].name;
cout << "Last Name : ";
cin >> s[i].lastname;
cout << "Collegian Number : ";
cin >> s[i].id;
for(j=0;j<5;j++)
{
cout << "Score("<<j+1<<") : ";
cin >> s[j].scores;
}
}
for(i=0;i<2;++i)
{
cout << endl << "Information of Collegian #" << i+1;
cout << endl << "Name : " << s[i].name << " ";
cout << "Last Name : " << s[i].lastname << " ";
cout << "Collegian Number : " << s[i].id << " ";
cout << endl << "Scores : ";
for(j=0;j<5;j++)
{
cout << j+1 << s[j].scores << " ";
}
cout << endl;
}
return 0;
}

输出:

1. Enter First & Last Name
2. Enter Collegian Number
3. Enter Collegian Scores of 5
4. App Will Show You What Received

------------------------------------

Collegian #1
Name : XXX
Last Name : XXX
Collegian Number : 917658053
Score(1) : 15
Score(2) : 16
Score(3) : 17
Score(4) : 18
Score(5) : 19

Collegian #2
Name : ZZZ
Last Name : ZZZ
Collegian Number : 917667015
Score(1) : 17
Score(2) : 16
Score(3) : 14
Score(4) : 18
Score(5) : 19

Information of Collegian #1
Name : XXX Last Name : XXX Collegian Number : 917658053
Scores : 117 216 314 418 519 // Why ????

Information of Collegian #2
Name : ZZZ Last Name : ZZZ Collegian Number : 917667015
Scores : 117 216 314 418 519 // Why ????

--------------------------------
Process exited after 53.16 seconds with return value 0
Press any key to continue . . .

最佳答案

您的 scores 变量应该是一个数组,因为您要为每个学生阅读 5 个 scores

另外,你应该这样读:

cin >> s[i].scores[j];//第 i 个学生的第 j 个分数。

您还必须类似地更改输出语句。

关于c++ - 存储2人c++信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34456954/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com