0

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
8
  • If everything you try in this function doesn't work, it might be your program has gone wrong before you called this function. So concentrate on finding out what the earlier problem is. Commented Apr 6, 2013 at 6:18
  • btw, in your for (row = 0; row < NUMBER_OF_STUDENTS + 1; col++), should be row++ instead of col++? and col is not initialized, it's UB? Commented Apr 6, 2013 at 6:19
  • 1
    You should probably post more code, preferably a complete program with the input you are using. Commented Apr 6, 2013 at 6:19
  • 1
    Possibly you should be starting 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 Commented Apr 6, 2013 at 6:20
  • Can you post your full code Commented Apr 6, 2013 at 6:21

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I cannibalized this from an assignment I did a few days ago that was working with int arrays and not string arrays. I tried removing the +1 from all of the code, but for some reason that messed it up, so I kept it. NUMBER_OF_STUDENTS and NUMBER_OF_SCORES are just variable names I've yet to change.
I tried using this code. It displays all of the data right after another, but it's all displayed like it's on the same line. After that however, the 'blank line' loops infinitely. I don't get why it doesn't work, but I'm going to retrace my code and see what the problem is.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.