存储2人c ++的信息

时间:2015-12-24 19:07:08

标签: c++

这是我存储2位大学生信息的源代码。我想从输入中获取每个人的姓名,姓氏,身份和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 . . .

1 个答案:

答案 0 :(得分:1)

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

另外,你应该这样读:

cin >> s[i].scores[j]; // the jth score of the ith student.

您还必须同样更改输出语句。