C++新手:输入流错误 求大佬康康

P5740 【深基7.例9】最厉害的学生

像素旋转 @ 2021-01-31 14:04:13

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
const int N = 1000;
class Student {
public:
    char name[9];
    int chinese;
    int math;
    int english;

    Student() {
        name[0] = '0';
        chinese = 0;
        math = 0;
        english = 0;
        sum = 0;
    };

    int summary() {
        return sum=chinese + math + english;
    }

private:
    int sum;
};
int main(void)
{
    int n;
    cin >> n;
    cin.get();
    Student** stu;
    stu = new Student*[n];
    int* sum = new int[n];
    int max = 0;
    int ans = 0;
    for (auto i = 0; i < n; i++){
        cin >>stu[i]->name>>stu[i]->chinese >> stu[i]->math >> stu[i]->english;
        sum[i]=stu[i]->summary();
        if (max < sum[i]) {
            max = sum[i];
            ans = i;
        }
    }
    cout << stu[ans]->name << " " << stu[ans]->chinese
        << " " << stu[ans]->math << " " << stu[ans]->english << endl;

    return 0;
}

by 像素旋转 @ 2021-01-31 14:05:23

输入完学生数据第一行就出错了


by Textbook_blasphemy @ 2021-01-31 14:09:36

感觉有点复杂化了,用结构体不香吗/jk


by Durancer @ 2021-01-31 14:19:50

我居然看不懂,我连新手都不如


by 冰糖鸽子 @ 2021-01-31 14:22:55

我居然看不懂,我连新手都不如


by 像素旋转 @ 2021-01-31 14:24:38

@陶(戴)佳伟 我是想拿来练练基础语法的


by yuchenren @ 2021-01-31 14:25:27

我居然看不懂,我连新手都不如

话说OI基本不用 pubilc


by Textbook_blasphemy @ 2021-01-31 14:28:27

@像素旋转 结构体也很基础吧 \fad


by 像素旋转 @ 2021-01-31 14:29:43

@任宇宸 OI是什么意思啊(无知求问)


by 像素旋转 @ 2021-01-31 14:30:39

@陶(戴)佳伟 刚学完这些拿来练手(滑稽)


by ExplodingKonjac @ 2021-01-31 14:30:50

你的stu指针虽然初始化了,但是每一个元素的值都是无效指针……

需要在cin前对于每个stu[i]初始化

for (auto i = 0; i < n; i++){
        stu[i] = new Student;
        cin >>stu[i]->name>>stu[i]->chinese >> stu[i]->math >> stu[i]->english;

| 下一页