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.
#include <iostream>
#include <iomanip>
#include <fstream>
usingnamespace std;
constint NUM_REVIEWERS = 4; //Number of rows in reviews array
constint 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.
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}};
}
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.