Printing data columns

My function is meant to read data from (x) rows and specifically five(5)columns. Currently, I am only printing the last data entry from the last row (x) times. Not each individual row. Do I need a nested for to distinguish the difference? Either way, I need advice on how to solve this problem. Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  void extractDesignatedColumns(int nbrOfRows, float &totalColumnTwo, float &totalColumnFour)              
{                                             
float columnOne, columnTwo, columnThree, columnFour, columnFive = 0.0;                                             
int counter;

for (int counter = 1; counter <= nbrOfRows; counter++)
 {
  cin >> columnOne >> columnTwo >> columnThree >> columnFour >> columnFive ;

  totalColumnTwo  = columnTwo  + totalColumnTwo ;
  totalColumnFour = columnFour + totalColumnFour;
 }
 
 int rows; 
   for (int rows = 0; rows < nbrOfRows; rows++)
    {   
     cout << setw(10) << columnTwo << setw(10) << columnFour << endl;  
    }

}
What problem? You do tell what you do, but don't exactly say what you should do.

The name of your function implies that the functions should gather some data. Why should you print anything at all within this function?

What is the purpose of lines 4 and 14?

Should the function accumulate to or set to its reference parameters?

Do you still have some implementation restrictions, like in your earlier threads?
Why i should print anything at all is because it is a requirement, is what this function does, and is irrelevant if you can help me or not. It reads data, non-negative integers, from a text file, 5 columns with an undetermined amount of rows.

My expected purposes for lines 4 and 14 are to read each row of data from columns 2 and 4. Either the 2nd one, line 14 is redundant, or I was trying to read from the columns.

I do not understand accumulate to or set to reference parameters.

My problem is as follows. enter number of rows : 5
enter number of columns: 5
(Five columns with 5 rows)
1 1 1 3 1
2 2 2 2 2
3 1 3 3 3
4 2 4 2 4
5 1 5 3 5

(I need to print columns two and four.)

1....3
2....2
1....3
2....2
1....3


Currently I print the last data entry for column 2 and column 4
1...3
1...3
1...3
1...3
1...3

No. No other problems aside from this one.
Why i should print anything at all is because it is a requirement

You did not explain that requirement clearly in your first post.

Line 4 and 14. You have essentially:
1
2
3
4
5
6
7
int foo;
for ( int foo = 0; foo < 7; ++foo )
{
  // the foo that you see here is the foo that was declare on line 2
  // it is different that the foo from line 1
}
// the foo that you see here is the foo that was declare on line 1 


If you move your line 17 to line 9, you will get the printout that you expect.
Your loop on lines 15-18 cannot print all rows, because you did not store them.

The accumulate/assign:
1
2
3
4
5
6
7
8
9
10
11
12
13
void bar( int & gaz )
{
  gaz = gaz + 42;
}

// use:
int X = 7;
bar( X );
// X is now 49
// the bar did add (aka accumulate) to X

// if bar should discard the previous value of X, then
// it should assign/set to gaz and not just add to it 
Last edited on
Topic archived. No new replies allowed.