C ++结构问题传递变量
C++ Structure issues passing variable
这项作业要求我创建一个程序来接收学生信息、名字姓氏和考试成绩。然后我必须分配正确的字母等级。然后我必须输出成绩最高的学生的名字。除了最高等级的部分外,一切正常。我知道这是我的“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()
功能有误。您正在相互比较学生分数,而不是 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()
都会忽略 returned 值。你需要这样做:
int main() {
studentType student[3];
getdata(student);
assignment(student);
int max = findmax(student);
print(student, max);
}
就个人而言,我会将 findmax()
更改为 return 索引而不是分数,然后完全摆脱 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;
}
这项作业要求我创建一个程序来接收学生信息、名字姓氏和考试成绩。然后我必须分配正确的字母等级。然后我必须输出成绩最高的学生的名字。除了最高等级的部分外,一切正常。我知道这是我的“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()
功能有误。您正在相互比较学生分数,而不是 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()
都会忽略 returned 值。你需要这样做:
int main() {
studentType student[3];
getdata(student);
assignment(student);
int max = findmax(student);
print(student, max);
}
就个人而言,我会将 findmax()
更改为 return 索引而不是分数,然后完全摆脱 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;
}