2D array

The university election will start in few days … they decided to create a system that calculates the total number of votes and decide who won the election instead of counting the votes manually. There are three different groups (Hardee’s, KFC, and Krispy cream) who’ll participate in the election. The university will provide 10 boxes to carry the votes (5 boxes for the boys & 5 for the girls). You’ve been asked to create this system. Your system will read the number of votes for the participated groups from every single box (from a file) and then it should display the following information:
- The name of the winning group and the runner up.
- The vote difference between the winner and the runner up too.
- The number of the voting box who granted the highest number of votes for the first group.
- The number of the voting box who granted the highest number of votes for the second group.
- The number of the voting box who granted the highest number of votes for the third group.

i dont know why the output is not showing when am cout it it comes in one colume only starting from the 3rd row
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <fstream>

using namespace std;
 int main() {

 const int boxes = 10;
 const int groups = 3;
 int votes [boxes][groups];

 ifstream in;
 in.open("votes.txt");

 for (int r=0;r<boxes;r++){
    for (int c=0;c<groups;c++)
        in>> votes[r][c];
        cout << votes [r][c]<<endl;





return 0;
 }

 
Do you realize that without braces {} only one line is included within a loop body. I recommend you start using braces for every control statement, even those where they're not technically required.

yes i realized that andeven thought the output is wrong same mistake
Show your current code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <fstream>

using namespace std;
 int main() {

 const int boxes = 10;
 const int groups = 3;
 int votes [boxes][groups];

 ifstream in;
 in.open("votes.txt");

 for (int r=0;r<boxes;r++){
    for (int c=0;c<groups;c++)
        {in>> votes[r][c];
        cout << votes [r][c]<<endl;}}
return 0;
 }
Please show your current output and post the contents of your votes.txt file.

Also are you sure your file is opening correctly? You should be checking the stream state to insure it properly opened.
Last edited on
this is the input file votes

162 130 25
84 55 195
125 134 12
123 140 194
33 114 166
117 64 169
54 50 158
87 154 99
115 14 116
160 136 6

and the output is

123
140
194
33
114
166
117
64
169
54
50
158
87
154
99
115
14
116
160
136
6
The program seems to be working correctly for me. Perhaps your console isn't tall enough to hold all the values without scrolling.

You may want to try to print each row on one line at a time:

1
2
3
4
5
6
7
    for (int r=0; r<boxes; r++) {
        for (int c=0; c<groups; c++)
        {   in>> votes[r][c];
            cout << votes [r][c] << " ";
        }
        cout << endl;
    }


yes it works thanku
Topic archived. No new replies allowed.