2
我在做一个使用数组,函数和随机数的项目。当我运行这个程序时,它给出了一个指向第44行的错误。看着代码,我无法弄清楚问题所在。
错误说:int [i]不能转换为44行int。数组,函数和随机
任何想法是什么错?
顺便说一下我可以用数字格式吗?
// Debugging: grades.cpp
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
const int NUM_GRADES = 10;
const int NUM_SUDENTS = 3;
int findHighest(int *);
int findLowest(int *);
void printDatabase(const int [][NUM_GRADES], const char [][ 20 ]);
int main()
{
int student1[ NUM_GRADES ] = { 0 };
int student2[ NUM_GRADES ] = { 76, 89, 81, 42, 66, 93, 104,
91, 71, 85 };
int student3[ NUM_GRADES ] = { 65, 69, 91, 89, 82, 93, 72,
76, 79, 99 };
char names[ NUM_SUDENTS ][ 20 ] = { "Bob", "John", "Joe" };
int database[ NUM_SUDENTS ][ NUM_GRADES ];
int i = 0;
srand(time(0));
// initialize student1
for (i = 0; i < NUM_GRADES; i++)
student1[ NUM_GRADES ] = rand() % 50 + 50;
// initialize database
for (i = 1; i < NUM_GRADES; i++) {
database[ 0 ][ i ] = student1[ i ];
database[ 1 ][ i ] = student2[ i ];
database[ 2 ][ i ] = student3[ i ];
} // end for
printDatabase(database, names);
for (i = 0; i < NUM_SUDENTS; i++) {
cout << names[ i ] << "'s highest grade is: "
<< findHighest(database[i]) << endl // This is line 44 in my program
<< names[ i ] << "'s lowest grade is: "
<< findLowest(database[ i ]) << endl;
} // end for
} // end main
// determine largest grade
int findHighest(int a[])
{
int highest = a[ 0 ];
for (int i = 1; i <= NUM_GRADES; i++)
if (a[ i ] > highest)
highest = a[ i ];
return highest;
} // end function findHighest
// determine lowest grade
int findLowest(int a[])
{
int lowest = a[ 0 ];
for (int i = 1; i < NUM_GRADES; i++)
if (a[ i ] < lowest)
lowest = a[ i ];
return lowest;
} // end lowestGrade
// output data
void printDatabase(int a[][ NUM_GRADES ], char names[][ 20 ])
{
cout << "Here is the grade database\n\n"
<< setw(10) << "Name";
for (int n = 1; n <= NUM_GRADES; n++)
cout << setw(4) << n;
cout << endl;
for (int i = 0; i < NUM_SUDENTS; i++) {
cout << setw(10) << names[ i ];
for (int j = 0; j < NUM_GRADES; j++)
cout << setw(4) << a[ i, j ];
cout << endl;
} // end for
cout << endl;
} // end printDatabase
哇,有趣。我将代码粘贴到我的文本编辑器中,它告诉我第44行是空行。 – chris 2014-10-30 01:32:15
当你知道错误来自何处时,为什么这么多代码? – 0x499602D2 2014-10-30 01:34:30
顺便说一句,在为'srand'添加适当的include之后,没有编译器错误,只是一些有用的警告和链接器错误,因为您从未定义过您使用的'printDatabase'重载:http://coliru.stacked- crooked.com/a/8d810ca773d5f502 – chris 2014-10-30 01:34:51