这是我的代码
void printMatrix(string matrix[][NUMBER_OF_SCORES + 1], int NUMBER_OF_STUDENTS)
{
int row, col;
//this code below is to loop it so it displays all the values
/* for (row = 0; row < NUMBER_OF_STUDENTS + 1; col++)
{
cout << matrix[row][col];
cout << endl;
}
*/
int looper = 0;
// THIS IS THE FORMAT FOR DISPLAYING ONE LINE MAN
//cout << matrix[0][0];
//cout << matrix[0][1];
//this is just some test code to see if it can output certain values right
// cout << matrix[0][0];
// cout << matrix[0][1];
// cout << matrix[0][2];
// cout << matrix[0][3];
}
我已经尝试了一切。我尝试过 for 循环,也尝试过 while 循环。我不明白为什么它不起作用。它要么不显示任何内容并立即崩溃,要么只是继续向下“滚动”,就像它无限重复但不显示任何文本一样。我尝试了一个while循环,就像这样
int looper;
int looper = NUMBER_OF_STUDENTS;
//in this instance, NUMBER_OF_STUDENTS was equal to 28
while (looper > 0)
{
cout << matrix[looper][0];
cout << matrix[looper][1];
looper--;
//i have tried both looper-- and --looper
}
我不明白为什么它根本不起作用;这令人难以置信的沮丧。这与我正在处理的程序与从文件读取时如何跳过数组的第一行? 向你们寻求这么多帮助我感到非常内疚,但我真的要在这里抨击了。
编辑:这是我的整个代码。
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
const int NUMBER_OF_STUDENTS = 28;
const int NUMBER_OF_SCORES = 28;
void getData (ifstream& infile, string matrix[][NUMBER_OF_SCORES + 1 ],
int NUMBER_OF_STUDENTS) ;
void printMatrix(string matrix[][NUMBER_OF_SCORES + 1], int NUMBER_OF_STUDENTS);
int main()
{
string birdarray [NUMBER_OF_STUDENTS][NUMBER_OF_SCORES +1 ] ;
// column 0 will hold the student ID.
// row n has the ID and birdarray for student n.
ifstream inData; //input file stream variable
inData.open("one.txt");
if ( !inData)
{
cout << "invalid file name \n";
return 1;
}
// input the birdarray into two-D array birdarray
getData ( inData , birdarray, NUMBER_OF_STUDENTS );
printMatrix(birdarray, NUMBER_OF_STUDENTS);
// return the row number of a searchItem in a particular column.
// if not found, return -1
}
void getData (ifstream& infile,string chart[][NUMBER_OF_SCORES + 1 ],
int student_count)
{
int row, col;
string dummyLine;
getline(infile, dummyLine);
for ( row = 0; row < student_count; row++)
for (col =0; col < NUMBER_OF_SCORES +1 ; col++)
infile >> chart [row] [col] ;
}
void printMatrix(string matrix[][NUMBER_OF_SCORES + 1], int NUMBER_OF_STUDENTS)
{
int row, col;
//this code below is to loop it so it displays all the values
/* for (row = 0; row < NUMBER_OF_STUDENTS + 1; col++)
{
cout << matrix[row][col];
cout << endl;
}
*/
int looper = 0;
// THIS IS THE FORMAT FOR DISPLAYING ONE LINE MAN
//cout << matrix[0][0];
//cout << matrix[0][1];
//this is just some test code to see if it can output certain values right
// cout << matrix[0][0];
// cout << matrix[0][1];
// cout << matrix[0][2];
// cout << matrix[0][3];
}
//prints a labeled listing of students' scores