CSDN-Ada助手 2023-06-09 11:17 采纳率: 1.6%
浏览 21

请教问题:图片中的代码哪里有问题

该问题来自社区帖: https://bbs.csdn.net/topics/615846221.为符合问答规范, 该问题经过ChatGPT优化
请教问题:图片中的代码哪里有问题?
  • 写回答

1条回答 默认 最新

  • yo~Magee 2023-06-09 12:51
    关注

    1、结构体里没有“英语成绩”
    2、main函数第一个for循环中第一个输入cin>>studentS,这个变量名写错了
    3、第二个for前面变量声明的时候空格丢个
    4、最后输出名字的时候变量名又写错了

    #include <iostream>
    #include <string> 
     
    using namespace std;
     
    struct Student {
        string name;
        float c_s;
        float m_s;
        float eh_s;
        float avg_s;
    }; 
     
    int main() {
        Student students[3];
        for (int i=0; i<3; i++) {
            cout<< "input the "<< i+1 <<"student name:";
            cin >> students[i].name;
            cout<< "input Ch score:";
            cin >> students[i].c_s;
            cout<< "input math score:";
            cin >> students[i].m_s;
            cout<< "input eh score:";
            cin >> students[i].eh_s;
            students[i].avg_s = ( students[i].c_s + students[i].m_s + students[i].eh_s )/3.0;
        } 
        
        int max_index = 0;
        
        for (int i=1; i<3; i++) {
            if (students[i].avg_s > students[max_index].avg_s) {
                max_index=i;
            }
        }
        
        cout << "max " << students[max_index].name << endl;
        cout << "avg score " << students[max_index].avg_s << endl;
    }
    
    评论

报告相同问题?

问题事件

  • 创建了问题 6月9日