我正在學習類中的指針和數組。我的問題在於dupScore功能。 我正在嘗試查找具有相同分數的學生人數。 scoreArrays元素編號標識學生並保持得分。使用數組作爲數組元素時出錯
我想創建一個新的數組來使用學生分數作爲數組的索引。雖然我在scoreArrays下得到了一個紅色的曲折。錯誤是「表達式必須是整數或枚舉」
for(int count = 0; count < maxStudents; count++)
comparisonArray[scoreArray[count]]++;
這是我的整個代碼。
#include <iostream>
using namespace std;
const int maxStudents = 30;
void readScores(double[]);
void gradeCounter(double[],int&,int&,int&,int&,int&);
int dupScore(double[]);
void readoutFunc(double[], int, int, int, int, int, int);
int main()
{
int As = 0, Bs = 0, Cs = 0, Ds = 0, Fs = 0; // Distribution of scores
int sameScore = 0;
double scoreArray[maxStudents];
readScores(scoreArray);//Read in Scores
gradeCounter(scoreArray, As, Bs, Cs, Ds, Fs);//Count letter grades
sameScore = dupScore(scoreArray);//Detect duplicate scores
system ("PAUSE");
return 0;
}
void readScores(double scoreArray[])
{
double *scorePTR;
scorePTR = scoreArray;
for(int count = 0; count < maxStudents; count++)
{
cout<<"Please enter score for student "<<count+1<<" or -999 to end.\n";
cin>>*(scorePTR+count);
if(*(scorePTR+count) == -999)
break;
}
}
void gradeCounter(double scoreArray[],int &As,int &Bs,int &Cs,int &Ds,int &Fs)
{
double *scorePTR2;
scorePTR2 = scoreArray;
for(int count = 0; count < maxStudents; count++)
{
if(scoreArray[count] >= 90)
As+=1;
else if(*(scorePTR2+count) >= 80 && *(scorePTR2+count) < 90)
Bs+=1;
else if(*(scorePTR2+count) >= 70 && *(scorePTR2+count) < 80)
Cs+=1;
else if(*(scorePTR2+count) >= 60 && *(scorePTR2+count) < 70)
Ds+=1;
else if(*(scorePTR2+count) >= 0 && *(scorePTR2+count) < 60)
Fs+=1;
}
}
int dupScore(double scoreArray[])
{
const int maxGrade = 101;
double comparisonArray[maxGrade];
int sameScores = 0;
for(int count = 0; count < maxStudents; count++)
comparisonArray[scoreArray[count]]++;
for(int count2 = 0; count2 < maxGrade; count2++)
{
if(comparisonArray[count2] > 0)
sameScores+=1;
}
return sameScores;
}
void readoutFunc(double scoreArray[], int As, int Bs, int Cs, int Ds, int Fs, int sameScore)
{
int numofStudents = 0;
for(int count = 0; scoreArray[count] >= 0; count++)
{
numofStudents += 1;
}
cout<<"\n\nReport";
cout<<"\n---------";
cout<<"\nNumber of students: "<<numofStudents;
cout<<"\nNumber of As: "<<As;
cout<<"\nNumber of Bs: "<<Bs;
cout<<"\nNumber of Cs: "<<Cs;
cout<<"\nNumber of Ds: "<<Ds;
cout<<"\nNumber of Fs: "<<Fs;
cout<<"\n\n"<<sameScore<<" students have the same score."
}
我想切換到INT會在這種情況下工作: – sircrisp 2012-02-10 02:14:26
雖然當我改變它,並試圖編譯一切,我在compareArray [(int)scoreArray [count]] ++得到了訪問衝突; – sircrisp 2012-02-10 14:42:07
你有一個訪問當你編譯它?或者當你運行它時出現違規? – mmodahl 2012-02-10 18:37:50