Josh
Josh

Reputation: 71

When displaying an array using a loop, nothing works

Here's my code

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];
} 

I have tried everything. I have tried for loops and I've tried while loops. I don't understand why it won't work. It either displays nothing and immediately crashes, or it just keeps "scrolling" down, like it's repeating infinitely but not displaying any text. There was a while loop I tried that went like this

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
}

I don't understand why it isn't working at all; it's incredibly frustrating. This is the same program I'm working on as How do I skip the first line of an array when reading from a file? I feel very guilty asking you guys for so much help, but I'm seriously about to snap here.

EDIT: Here's my entire code.

#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

Upvotes: 0

Views: 143

Answers (2)

Memento Mori
Memento Mori

Reputation: 3402

Your attempts so far would not actually print the entire matrix even if they did not loop continuously. First for the while loop, it's very likely you should be initializing looper at NUMBER_OF_STUDENTS - 1 because if there are NUMBER_OF_STUDENTS rows in the array then the highest element would be NUMBER_OF_STUDENTS - 1. It's possible that you're saying NUMBER_OF_STUDENTS + 1 because of a coding problem elsewhere however. If that's the case I'd try to take care of that issue first for simplicity's sake.

Regarding your for loop

for (row = 0; row < NUMBER_OF_STUDENTS + 1; col++) {
   cout <<  matrix[row][col];
   cout << endl;
}

this will loop forever because your stop condition is dependent on row, but row never changes, only col does.

To print all the elements in your matrix you need two loops. Something like this:

for(int row=0; row < numOfRows; row++)
    for(int col=0; col < numOfCols; col++)
         cout << matrix[row][col];

Upvotes: 0

john
john

Reputation: 88017

Here's the normal way to print a table of values

void printMatrix(string matrix[][NUMBER_OF_SCORES + 1], int NUMBER_OF_STUDENTS)
{
  for (int row = 0; row < NUMBER_OF_STUDENTS + 1; ++row)
  {
    for (int col = 0; col < NUMBER_OF_SCORES; ++col)
    {
      cout << matrix[row][col] << ' ';
    }
    cout << '\n';
  }
}

BUT there are lots of things about your code I don't like. So whether this is right I cannot say. In particular I'm dubious about

1) Why do you have NUMBER_OF_STUDENTS + 1? No obvious need for the + 1, you are probably trying to compensate for a mistake you made elsewhere.

2) Why do you have a matrix of strings? Scores would normally be a number.

I guess the main lesson to learn is to think about exactly what the code you write does. Code isn't a mysterious magic spell, it's a precise series of instructions to the computer. If you had thought about exactly what your code does, followed it through step by step, you would have seen the errors you'd made, and hopefully been able to fix them. You've got to get into that way of thinking.

Upvotes: 1

Related Questions