尝试将新记录添加到我的结构成员数组中。 我最初通过文件I / O填充了数组,但是尝试添加新记录(在保存到文件之前)会使我的字段留空。
//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;
};
如果我先运行显示功能,它会显示5条记录。通过add函数时,totalRead从4开始(因为数组中有5个),然后递增到下一个插槽。
当你再次尝试显示时,它会输出额外的一行,但我只会得到胡言乱语。看起来没有任何存储。
思想?
答案 0 :(得分:0)
你的问题很可能与克里斯遇到的问题相同:
totalRead++; //New item in the arrayStruct
...
cin >> list[totalRead].gameName;
在使用之前增加totalRead。现在,如果totalRead == 0表示“有1个游戏。”,那很好。但是,我怀疑totalRead == 0表示“没有游戏”。看看[chris]的输出here实际上是如何丢失最后一个游戏输入的。
你最有可能看到乱码,因为你从n场比赛(0到n - 1)开始,跳过游戏n,将新游戏解析为阵列n + 1并打印0到n的游戏,即0到n - 1的游戏加一个未初始化的条目在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 <---