结构问题

时间:2013-10-14 00:16:17

标签: c++ pointers struct

嘿,我刚刚开始使用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;
}

感谢您提供的任何帮助

1 个答案:

答案 0 :(得分:2)

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”是struct StudentType的一部分,所以你应该写:

students[i].name = convert (students[i].name);