I built a program that is supposed to read in a data file that looks like this for example:
1 2 3 4 5 6
2 3 4 9 8 7
1 6 5 7 4 2
1 3 4 5 6 7
2 5 7 8 3 1
3 1 6 7 4 5

I want to be able to display these values in the command window just as they are displayed in the data file.

but instead, it gets printed to the command window as something like this:
1
2
3
4
5
6
2
3
4..so on and so forth

i read in the data file properly like this:

number>>row>>col

and my for loop looks like this:

for(i=1;i<5;i++)
    {
         for( j=1;j<6; j++)
         {
              infile>>random[i][j];
              cout<<random[i][j]<<endl;
              cout<<endl;
         }
    }

in the program i am also supposed to find the maximum value and its location in the data file. My program correctly prints out that the maximum value in this case is 9 and that its in row 2 column 4.

is the structure of the for loop incorrect/is the the way that cout is set up incorrect? Or is there nothing that I can do about it?

put the "cout << endl; " at the end of the first for loop. So do this :

for(i=1;i<5;i++)
    {
         for( j=1;j<6; j++)
         {
              infile>>random[i][j];
              cout<<random[i][j]<<endl;
         }
              cout<<endl;
    }

put the "cout << endl; " at the end of the first for loop. So do this :

for(i=1;i<5;i++)
    {
         for( j=1;j<6; j++)
         {
              infile>>random[i][j];
              cout<<random[i][j]<<endl;
         }
              cout<<endl;
    }

Maybe you caught it already but line 6 of the snippet should probably be cout<<random[i][j]<<" "; (no endl) that way you get the spacing and there's not the extra carriage return. Hopefully I have not misunderstood your requirement.

This article has been dead for over six months. Start a new discussion instead.