Question

Trying to add a new record into my array of structure members. I have the array populated initially through a file I/O, but trying to add in a new record (before saving to a file) leaves my fields blank.

//CONSTANTS
const int MAX_GAMEDATA = 100;

//STURCTURES
struct GameType
{
string gameName;
char gameRating;
int gameSales;
};

//PROTOTYPES
void ReadDataFromFile(GameType list[], int& totalRead);
void DisplayUserMenu();
void GetMenuInput(char& input);
void DisplayAllGames(GameType list[], int totalRead);
void AddGameData(GameType list[], int& totalRead);

int main()
{
int totalRecords = 0;
GameType gameList[20];

//Populate arrays with data
cout << "Loading...";
ReadDataFromFile(gameList, totalRecords);
system("cls");

//Display menu
DisplayUserMenu();

//If the user enters an invalid option, prompt them again
char input = 'A'; //Default value (initialize)

while(input != 'X')
{
    GetMenuInput(input);
    switch(input)
    {
        case('A'):
            DisplayAllGames(gameList, totalRecords);
            cout << endl;
            break;
        case('B'):
            AddGameData(gameList, totalRecords);
            cout << endl;
            break;
        case('C'):
        case('D'):
        case('E'):
        case('X'):
            break;
    };
};


system("pause");
return 0;
};

void ReadDataFromFile(GameType list[], int& totalRead)
{
fstream fileIN;
totalRead = 0;

fileIN.open("StarterGameData.txt");
if(fileIN.is_open())
{
    while(!fileIN.eof() && totalRead < MAX_GAMEDATA)
    {
        fileIN >> list[totalRead].gameName;
        fileIN >> list[totalRead].gameRating;
        fileIN >> list[totalRead].gameSales;
        totalRead++;
    }
}
else
{
    cout << "ERROR READING FILE.... \n\n";
}
fileIN.close();

};

void DisplayUserMenu()
{
cout << "A. Display Game Information" << endl;
cout << "B. Add New Game Data" << endl;
cout << "C. Display Highest and Lowest Game By Sales" << endl;
cout << "D. Display Game Data by Rating" << endl;
cout << "E. Save Game Data to Disk" << endl;
cout << "X. Exit Program" << endl;
cout << endl;
};

void GetMenuInput(char& input)
{

cout << "Please select your option: ";
cin >> input;
cout << endl;

input = toupper(input);
switch(input)
{
case('A'):
case('B'):
case('C'):
case('D'):
case('E'):
case('X'):
    break;
default:
    cout << "Please enter a valid option!" << endl << endl;
    break;
};
};

void DisplayAllGames(GameType list[], int totalRead)
{
cout << left << setw(15) << "Game Name" << setw(8) <<
    "Rating" << setw(10) << "Sales" << endl <<
    "---------------------------------" << endl;
for(int i = 0; i < totalRead; i++)
{
    cout << left << setw(15) << list[i].gameName << setw(8)
        << list[i].gameRating << setw(10)
        << list[i].gameSales << endl;
};
};

void AddGameData(GameType list[], int& totalRead)
{
totalRead++; //New item in the arrayStruct

cout << "Add new game data:" << endl;
cout << "Title Name: ";
cin >> list[totalRead].gameName;
cout << "\nGame Rating: ";
cin >> list[totalRead].gameRating;
cout << "\nGame Sales: ";
cin >> list[totalRead].gameSales;
cout << "Record added." << endl;

};

If I run the display function first, it displays the 5 records. While going through the add function totalRead starts out at 4 (cause there's 5 in the array) and then increments to the next slot.

When you try the display again it outputs the extra line, but I only get gibberish. Looks like nothing got stored.

Thoughts?

Was it helpful?

Solution

Your problem is most likely the same that chris ran into:

totalRead++; //New item in the arrayStruct
...
cin >> list[totalRead].gameName;

You increment totalRead prior to using it. Now, if totalRead == 0 means "There is 1 game.", that's fine. However, I suspect totalRead == 0 means "There are no games." See how [chris]'s output here is actually missing the last game input.

You most likely see gibberish, because you start with n games (0 to n - 1), skip game n, parse new game into array n + 1 and print games 0 to n, i.e. games 0 to n - 1 plus one uninitialized entry at n.

at initialization       after totalRead++        after AddGameData
[0]   Game_0            [0]   Game_0             [0]   Game_0      \
...                     ...                      ...               |- only these
[n]   ?!?!?!  <---      [n]   ?!?!?!             [n]   ?!?!?!      /  are printed
[n+1] ?!?!?!            [n+1] ?!?!?!  <---       [n+1] Game_n <---
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top