C language online question bank: [1050] Score record of structure

There are currently N student data records. Each record includes student number, name , and grades in three subjects . Write a function input to input a student's data record. Write a function print to print a student's data record. Call these two functions in the main function, read N record inputs, and then output them as required. N<100

Input format

The number of students N occupies one line. Each student's student number, name, and three subject scores occupy one line and are separated by spaces.

Output format

Each student's student number, name, and grades in three subjects should be on one line, separated by commas.

Sample input

2
a100 clang 70 80 90
b200 dotcpp 90 85 75

Sample output

a100,clang,70,80,90
b200,dotcpp,90,85,75

The question asks to enter the information of multiple students. My idea is to define the student number , name and subject score.

(Pay attention to the data type, the student ID must be string). Then define an object stu[100] array of the structure. After that comes input and output.


#include<iostream>
/*有n个学生对象,包括学号、姓名、三科成绩的属性/*/
using namespace std;
//方法2
struct Student {
    std::string num; //学号
    std::string name; //姓名
    int su1;         //科一
    int su2;         //科二
    int su3;         //科三
};


struct Student stu[100];   //定义2个学生结构体



//输入学生的数据
int input() {
    int n;
    //cout << "请输入学生个数:";
    std::cin >> n;
    for (int i = 0; i < n; i++) {
        std::cin >> stu[i].num;
       // cout << "NUM:" << stu[i].num<<endl;
      std::cin >> stu[i].name;
        //cout << "NAME:" << stu[i].name<<endl;
        //测试语句
     //cout<<i<<"Name:"<<stu[i].name;
        std::cin >> stu[i].su1;
    
        std::cin >> stu[i].su2;
        std::cin >> stu[i].su3;

    }

    return n;
}

void print(int count) {//录入学生数量
    for (int i = 0; i < count; i++) {
        std::cout << stu[i].num<<",";
        std::cout << stu[i].name<<",";
        std::cout << stu[i].su1<<",";
        std::cout << stu[i].su2<<",";
        std::cout << stu[i].su3<<endl;

    }
}

int main() {
    int count = input();
    print(count);
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_63999224/article/details/132842974