populating 2d array after initialization

Hello all. Long time listener, first time caller here :D

I am writing a 2d array with functions that I am asked to initialaize (with a function) and then populate using a function. I can populate with any means I want. I chose to read from file.

For some reason, I cannot populate the array with the data from my file. When I showArray, the array shows only zeros from when it was initialized.

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

const int NUM_REVIEWERS = 4;   //Number of rows in reviews array
const int NUM_MOVIES = 6;	   //Number of columns in reviews array    	


void initialRatings(int reviews[][NUM_MOVIES]);		  
void printHeader();
void getRatings(int reviews[][NUM_MOVIES]);		             
void displayRatings(int reviews[][NUM_MOVIES]);	

int main()
{
    
    // Variable declarations
    int reviews[NUM_REVIEWERS][NUM_MOVIES];  
    int choice;                              

    initialRatings(reviews);                 
    getRatings(reviews);  


   // do-while loop and switch (menu options) removed to save space.

    return 0;
}
   
// ******************************************************************* //
//                        initialRatings                               //
// This function initalizes the array reviews and sets the values to 0 //
// ******************************************************************* //
void initialRatings(int reviews[][NUM_MOVIES])
{
    
   for (int x = 0; x < NUM_REVIEWERS; x++)
   {
      for (int y = 0; y < NUM_MOVIES; y++)
        reviews[x][y] = 0;
   }

    getRatings(reviews);    //Call function TESTING
}




// ***************************************************************** //
//                         printHeader                               //
// This function prints the header for the Movie Ratings             //
// ***************************************************************** //
void printHeader()
{
    cout << "********************* MOVIE RATINGS ****************" << endl;
    cout << "REVIEWER|  MV#100 MV#101 MV#102 MV#103 MV#104 MV#105" << endl;
    cout << "****************************************************" << endl;
}




// ***************************************************************** //
//                        getRatings                                 //
// This function     //
// ***************************************************************** //
void getRatings(int reviews[][NUM_MOVIES])
{    
	ifstream dataIn;
	dataIn.open("reviews.dat");

    if (!dataIn)
    {
    cout << "Error opening file.\n";
        }   
        else
        {  
            for (int x = 0; x < NUM_REVIEWERS; x++)
            {
                for (int y = 0; y < NUM_MOVIES; y++)
		        {
        dataIn >> reviews[NUM_REVIEWERS][NUM_MOVIES];
                }   
             }
        dataIn.close();
        }
                                              
}




// ***************************************************************** //
//                        displayRatings                             //
// This function              //
// ***************************************************************** //
void displayRatings(int reviews[][NUM_MOVIES])
{
    getRatings(reviews);     // Function call
    printHeader();

    for (int x = 0; x < NUM_REVIEWERS; x++)
    {
        cout << setw(3) << "#" << (x + 1) << "    ";  
        for (int y = 0; y < NUM_MOVIES; y++)
        {
            cout << right << setw(6) << reviews[x][y] << " ";
        }
            cout << endl;
    }

}


I am unsure why my reviews.dat information is not showing in the array. I do know that the file is being read, when I change the name I do get the !dataIn error.

Thank you for any and all assistance.
Ok, after having no luck including a file input from within my function (getRatings), I have decided to just include the numbers to populate the array with an initialization list.

I could ask the user to input the numbers, but I would like to learn this method of populating from file or list.

However, I get this error when I try to use the initalization list:

declaration of 'int reviews[4][6]' shadows a parameter

I am sure I am making a small error, but I cannot figure out what it is or how to properly format my initalization list within a function.

1
2
3
4
5
6
7
8
void getRatings(int reviews[][NUM_MOVIES])
{ 
    
    int reviews[NUM_REVIEWERS][NUM_MOVIES] = {{3,1,5,2,1,5},{4,2,1,4,2,4},
                                              {3,1,2,4,4,1},{5,1,4,2,4,2}}; 

}
Hi, The paramater has the same variable name as a local variable of this method:

1
2
3
4
5
6
7
void getRatings(int reviews[][NUM_MOVIES])
{ 
    
    int reviews[NUM_REVIEWERS][NUM_MOVIES] = {{3,1,5,2,1,5},{4,2,1,4,2,4},
                                              {3,1,2,4,4,1},{5,1,4,2,4,2}}; 

}

Last edited on
1
2
3
4
5
6
7
void getRatings(int reviews[][NUM_MOVIES])
{ 
    
    int reviews[NUM_REVIEWERS][NUM_MOVIES] = {{3,1,5,2,1,5},{4,2,1,4,2,4},
                                              {3,1,2,4,4,1},{5,1,4,2,4,2}}; 

}


The int reviews[NUM_REVIEWERS][NUM_MOVIES] creates another 'reviews ' variable inside the function and the function is not using the array you received, but the one you declared inside it. Removing the "int" should do the trick.
Last edited on
Topic archived. No new replies allowed.