【问题标题】:C++ Structure issues passing variable [closed]C ++结构问题传递变量[关闭]
【发布时间】:2021-06-08 18:00:48
【问题描述】:

这项作业要求我创建一个程序,其中包含学生信息、名字姓氏和考试成绩。然后我必须分配正确的字母等级。然后我必须输出成绩最高的学生的姓名。除了最高等级的部分外,一切正常。我知道我的“max”变量没有从 findmax 函数更新是一个问题。我只是不知道为什么。

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

struct studentType {
    string studentFName;
    string studentLName;
    int testscore;
    char grade;
};
void getdata(studentType student[]);
void assignment(studentType student[]);
int findmax(studentType student[]);
void print(studentType student[], int max);

int main() {
    int max = 0;
    studentType student[3];
    getdata(student);
    assignment(student);
    findmax(student);
    print(student, max);
}

void getdata(studentType student[]) {
    for (int i = 0; i < 3; i++) {
        cout << "Enter Student First Name: " << endl;
        cin >> student[i].studentFName;
        cout << "Enter Student Last Name: " << endl;
        cin >> student[i].studentLName;
        cout << "Enter Student test score: " << endl;
        cin >> student[i].testscore;
        while (student[i].testscore > 100 || student[i].testscore < 0) {
            cout << "Please input a valid score in the range 0-100" << endl;
            cin >> student[i].testscore;
        }
    }
}
void assignment(studentType student[]) {
    for (int i = 0; i < 3; i++) {
        if (student[i].testscore >= 90 && student[i].testscore <= 100)
            student[i].grade = 'A';
        else if (student[i].testscore >= 80 && student[i].testscore < 90)
            student[i].grade = 'B';
        else if (student[i].testscore >= 70 && student[i].testscore < 80)
            student[i].grade = 'C';
        else if (student[i].testscore >= 60 && student[i].testscore < 70)
            student[i].grade = 'D';
        else if (student[i].testscore < 60)
            student[i].grade = 'F';
    }
    for (int j = 0; j < 3; j++)
        cout << student[j].studentLName << ", "<< setw(10) << student[j].studentFName << ", " << setw(10) << student[j].testscore << endl;
}

int findmax(studentType student[]) {
    int max = 0;
    for (int i = 0; i < 3; i++) {
        if (student[i].testscore > student[i - 1].testscore)
            max = student[i].testscore;
    }
    
    return max;
}


void print(studentType student[], int max) {
    for (int i = 0; i < 3; i++) {
        if (max == student[i].testscore)
            cout << student[i].studentFName << " " << student[i].studentLName << " had the highest score with " << student[i].testscore;

        }
    }

【问题讨论】:

  • 一旦 findmax() 被修复,这段代码将尽可能地工作,我不想成为那个告诉你他们会以不同方式做的人。如果你只是想通过你的课程,获得学分并把 C++ 抛在脑后,那么一切都很好。只是您似乎在使用二十年前的 C++ ......例如,当 std::vector 在大多数情况下是本世纪的选择时,固定长度的数组。
  • 啊,好吧。我们还没有在我们的课程中达到向量,所以这就是我用这种技术接近它的原因。不过感谢您的提示。

标签: c++ arrays object structure


【解决方案1】:

您的findmax() 函数错误。您正在将学生的分数相互比较,而不是与 max 变量进行比较。试试这个:

int findmax(studentType student[]) {
    int max = 0;
    for (int i = 0; i < 3; i++) {
        if (student[i].testscore > max)
            max = student[i].testscore;
    }    
    return max;
}

或者:

int findmax(studentType student[]) {
    int max = student[0].testscore;
    for (int i = 1; i < 3; i++) {
        if (student[i].testscore > max)
            max = student[i].testscore;
    }    
    return max;
}

无论哪种方式,您的 main() 都会忽略返回值。你需要这样做:

int main() {
    studentType student[3];
    getdata(student);
    assignment(student);
    int max = findmax(student);
    print(student, max);
}

就个人而言,我会更改findmax() 以返回索引而不是分数,然后完全摆脱print() 中的循环,例如:

struct studentType {
    string studentFName;
    string studentLName;
    int testscore;
    char grade;
};

static const int MaxStudents = 3;

void getdata(studentType students[]);
void assignment(studentType students[]);
int findmax(studentType students[]);
void printMax(studentType &student);

int main() {
    studentType students[MaxStudents];
    getdata(students);
    assignment(students);
    int index = findmax(students);
    printMax(students[index]);
}

void getdata(studentType students[]) {
    for (int i = 0; i < MaxStudents; ++i) {
        cout << "Enter Student First Name: " << endl;
        cin >> students[i].studentFName;
        cout << "Enter Student Last Name: " << endl;
        cin >> students[i].studentLName;
        cout << "Enter Student test score: " << endl;
        cin >> students[i].testscore;
        while (students[i].testscore > 100 || student[i].testscore < 0) {
            cout << "Please input a valid score in the range 0-100" << endl;
            cin >> students[i].testscore;
        }
    }
}

void assignment(studentType students[]) {
    for (int i = 0; i < MaxStudents; ++i) {
        if (students[i].testscore >= 90)
            students[i].grade = 'A';
        else if (student[i].testscore >= 80)
            students[i].grade = 'B';
        else if (student[i].testscore >= 70)
            students[i].grade = 'C';
        else if (student[i].testscore >= 60)
            students[i].grade = 'D';
        else
            students[i].grade = 'F';
    }

    for (int j = 0; j < MaxStudents; ++j)
        cout << students[j].studentLName << ", " << setw(10) << students[j].studentFName << ", " << setw(10) << students[j].testscore << endl;
}

int findmax(studentType students[]) {
    int index = 0;
    int max = students[index].testscore;

    for (int i = 1; i < MaxStudents; i++) {
        if (students[i].testscore > max) {
            index = i;
            max = students[index].testscore;
        }
    }
    
    return index;
}

void printMax(studentType &student) {
    cout << student.studentFName << " " << student.studentLName << " had the highest score with " << student.testscore;
}

【讨论】:

  • 啊,非常感谢。我没有意识到主要功能中的错误。非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-08
相关资源
最近更新 更多