结构的问题
Problems with Structs
嘿,我几天前刚开始在C++中研究结构,我的代码中几乎没有错误。我试着和我的老师一起解决它们,但他给我的只是隐晦的答案,根本没有帮助。你们给我的任何帮助将不胜感激。
以下是错误:
G:Program01.cpp||In function 'void getData(StudentType*)':|
G:Program01.cpp|99|error: 'name' was not declared in this scope|
G:Program01.cpp||In function 'void convert(std::string&)':|
G:Program01.cpp|163|error: expected ';' before ')' token|
G:Program01.cpp||In function 'void assignGrades(StudentType*)':|
G:Program01.cpp|192|error: request for member 'average' in 'students', which is of pointer type 'StudentType*' (maybe you meant to use '->' ?)|
G:Program01.cpp|192|error: request for member 'grade' in 'students', which is of pointer type 'StudentType*' (maybe you meant to use '->' ?)|
G:Program01.cpp|193|error: request for member 'average' in 'students', which is of pointer type 'StudentType*' (maybe you meant to use '->' ?)|
G:Program01.cpp|193|error: request for member 'grade' in 'students', which is of pointer type 'StudentType*' (maybe you meant to use '->' ?)|
G:Program01.cpp|194|error: request for member 'average' in 'students', which is of pointer type 'StudentType*' (maybe you meant to use '->' ?)|
G:Program01.cpp|194|error: request for member 'grade' in 'students', which is of pointer type 'StudentType*' (maybe you meant to use '->' ?)|
G:Program01.cpp|195|error: request for member 'average' in 'students', which is of pointer type 'StudentType*' (maybe you meant to use '->' ?)|
G:Program01.cpp|195|error: request for member 'grade' in 'students', which is of pointer type 'StudentType*' (maybe you meant to use '->' ?)|
G:Program01.cpp|196|error: request for member 'grade' in 'students', which is of pointer type 'StudentType*' (maybe you meant to use '->' ?)|
G:Program01.cpp||In function 'void printGradeSheet(StudentType*)':|
G:Program01.cpp|221|error: expected primary-expression before ')' token|
G:Program01.cpp|222|error: no match for 'operator<<' in 'std::cout << std::setfill<const char*>(((const char*)" "))'|
G:Program01.cpp|234|error: expected primary-expression before 'students'|
G:Program01.cpp|235|error: expected primary-expression before ']' token|
G:Program01.cpp|255|error: expected unqualified-id before '{' token|
代码如下:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <string>
#include <climits>
using namespace std;
// constant declarations:
const int NUM_STUDENTS = 20;
const int NUM_SCORES = 5;
// struct/class declarations:
struct StudentType {
string name;
int scores[NUM_SCORES];
float average;
char grade;
};
// function prototypes:
void getData (StudentType students[]);
void assignGrades (StudentType students[]);
void printGradeSheet (StudentType students[]);
void getFile (ifstream& fin);
void convert (string& name);
void printTopStudents (StudentType students[], float highscore);
float findHighAverage (StudentType students[]);
int main() {
StudentType students [NUM_STUDENTS];
getData (students);
assignGrades (students);
printGradeSheet (students);
return 0;
}
void getData (StudentType students[]) {
ifstream fin;
getFile (fin);
for (int i = 0; i < NUM_STUDENTS; ++i) {
int total = 0;
getline (fin, students[i].name);
students[i].name = convert (name);
for (int j= 0; j < NUM_SCORES; ++j) {
fin >> students[i].scores[j];
total += students[i].scores[j];
}
students[i].average = static_cast<float>(total) / NUM_SCORES;
fin.ignore (INT_MAX, 'n');
}
void getFile (ifstream& fin) {
string filename;
cout<< "Enter filename: ";
cin >> filename;
fin.open (filename.c_str());
while (!fin) {
cout << "Could not open" << filename << ", try again." << endl;
cout << "Enter filename: ";
cin >> filename;
fin.open (filename.c_str());
}
}
void convert (string& name) {
string::size_type n;
string b;
string s;
n = name.find (".");
if (n!=string::npos) {
// found middle initial
s = name.substr(n+2, name.size()) + ", " + name.substr(0, n+1));
name = s;
}
else {
// no middle initial
n = name.find(" ");
b = name.substr(n + 1, name.size()) + ", " + name.substr(0, n);
name = b;
}
}
void assignGrades (StudentType students[]) {
if (students.average >= 90) students.grade = 'A';
else if (students.average >= 80) students.grade = 'B';
else if (students.average >= 70) students.grade = 'C';
else if (students.average >= 60) students.grade = 'D';
else students.grade = 'F';
}
void printGradeSheet (StudentType students[]) {
float highscore;
cout << setw(25) << left << "Names" << setw(10) << right << "Grades";
cout << setw(2) << right << "Avg." << setw(2) << right << "Grade" << endl;
cout << setfill(-) << setw(40) << '-';
cout << setfill(" ");
for (int k = 0; k < NUM_STUDENTS; ++k) {
cout << left << setw(25) << students[k].name;
cout << " ";
for (int g = 1; g < NUM_SCORES; ++g) {
cout << right << students[k].scores[g] << " ";
}
cout << showpoint << setprecision(2) << fixed;
cout << students[k].average << " " << students[k].grade;
cout << endl;
}
highscore = findHighAverage (StudentType students[]);
printTopStudents (students[], highscore);
}
float findHighAverage (StudentType students[]); {
float highscore;
float score;
highscore = students[0].average;
for (s = 1; s < NUM_STUDENTS; ++s) {
score = students[s].average;
if (highscore < score)
highscore = score;
}
return highscore;
}
void printTopStudents (StudentTypes students[], float highscore); {
cout << "Student with the highest grade: " << endl;
cout << "---------------------------------" << endl;
for (int i = 0; i < NUM_STUDENTS; ++i) {
if (students[i].average == highscore)
cout << students[i].name << setw(5) << right << highscore << endl;
}
cout << "---------------------------------" << endl;
}
感谢您提供的任何帮助
在 assignGrades
函数中,访问学生时缺少数组索引:
// students is an array not a single struct
if (students.average >= 90) students.grade = 'A';
else if (students.average >= 80) students.grade = 'B';
else if (students.average >= 70) students.grade = 'C';
else if (students.average >= 60) students.grade = 'D';
else students.grade = 'F';
如果要为数组中的所有学生分配成绩,则必须使用 for:
for(int i = 0; i < NUM_STUDENTS; ++i)
{
if (students[i].average >= 90) students[i].grade = 'A';
else if (students[i].average >= 80) students[i].grade = 'B';
else if (students[i].average >= 70) students[i].grade = 'C';
else if (students[i].average >= 60) students[i].grade = 'D';
else students[i].grade = 'F';
}
同样在功能getData
:
students[i].name = convert (name);
"name"是结构体StudentType的一部分,所以你应该写:
students[i].name = convert (students[i].name);
相关文章:
- 如何重构类层次结构以避免菱形问题
- 包含矢量指针的结构的内存释放问题
- 在类中返回结构时出现问题
- 我对数据结构、双向链表有一些问题
- C++模板类中结构的编译器问题
- 我的超类中的模板问题与结构定义
- 修改C++结构的特征成员时出现问题
- C++ 结构内部的unordered_map会导致内存泄漏问题吗?
- 指向包含对齐 C 结构C++类的 C 指针的对齐问题
- (ODR 使用问题)在不同文件中priority_queue名称相同的结构
- 在C++中循环访问自定义结构列表的小问题
- 在C++中对结构向量进行排序时出现问题
- 我想直接在结构中插入,但没有一种方法可以正确避免填充问题
- 我的堆栈和库存清单程序的结构有什么问题?
- 将字符数组转换为结构时出现问题. 结构的字符数组变量溢出
- 我应该如何在C++中使用结构体解决输入失败的问题?
- DNS 查询格式标头结构中的小字节序问题
- C++,类结构问题
- 使用初始化列表填充C++中的多维结构数组时出现问题
- 为<vtkDataArray> VTK 非结构化网格声明 vtkSmartPointer 类型的变量时出现问题