I am having issue with the search function, when I search my database instead of getting the information saved on the database I just get 1, so how can I fix it so it shows the info of the game which I have saved ?
Also went it comes to rating, it has to be a system from 0-10, and if I make multiply inputs it should divide the total value with the number of inputs.
Hopefully what I am saying makes sense.
Code:
---------
#include
#include
#include
#include
using namespace std;
const int GameName = 50;
// gameType is our custom data type which will be a struct with 4 members
struct gameType
{
string name;
string type;
string price;
string publisher;
string dataBase[GameName];
int gameRating;
};
// function prototypes
int menu(); // display a menu to the user
gameType addGame();
void addRating(gameType game[], int postion);
void outputData(const gameType game[], int currentgame);
void showgameByName(const gameType game[], int currentgame);
int loadFromFile(gameType game[]);
void saveToFile(const gameType game[], int currentgame);
int findGame(const gameType game[], int currentgame, string name);
int main()
{
// declare an array of 50 games
gameType game[50];
int choice;
string name;
string type;
string price;
string publisher;
int gameRating;
// currently though, we have 0 game
int currentgame = 0;
// set up a loop to show and process our menu and the user's choice
do
{
// call and display the menu and get the user's choice
choice = menu();
// process that choice
if (choice == 1)
{
// call the addGames function, which returns a variable
// of type gameType. Store that in the array at the
// the first open position, which is currentgame
game[currentgame] = addGame();
// increment currentgame to note the new # of game
currentgame++;
}
else if (choice == 2)
{
int position; // variable to hold the correct index for
// the game to add an rating to
cout << "Which game do you want to add an rating for? " << endl;
showgameByName(game, currentgame);
cin >> position;
addRating(game, --position);
}
//Seach for game using name, genre, or rating
else if (choice == 3)
{
cout << "Enter one of the following: Name of Video Game, Type of Video Game, or Rating of Video Game: " << endl;
cin >> name, type, gameRating;
cout << findGame(game, currentgame, type) << endl;
}
else if (choice == 4)
{
// call our output function passing to it the array of structs
// which contains ALL data and the # of game, which will
// tell the function how many game to loop through
outputData(game, currentgame);
}
else if (choice == 5)
{
// populate our array from a file of data
currentgame = loadFromFile(game);
}
else if (choice == 6)
{
// dump our array of structs to a file
saveToFile(game, currentgame);
}
} while (choice != 7);
return 0;
}
int menu()
{
int choice;
cout << "1. Add Video Game" << endl
<< "2. Add Video Game Rating" << endl
<< "3. Search for Video Game" << endl
<< "4. Show Saved Video Games " << endl
<< "5. Load Data from File" << endl
<< "6. Save Data to File" << endl
<< "7. Exit Program" << endl;
cin >> choice;
return choice;
}
gameType addGame()
{
// create a temporary structure to hold the input
gameType temp;
cout << "Enter title of the game: ";
// always use a cin.get() BEFORE a getline, AFTER a cin >>
cin.get();
getline(cin, temp.name);
cout << "Enter the Genre: ";
cin >> temp.type;
cout << "Enter Price Paid: ";
cin >> temp.price;
cout << "Publisher: ";
cin >> temp.publisher;
// set up the initial videogame dataBase with 0 prices
temp.gameRating = 0;
// send this game back to main
return temp;
}
void addRating(gameType game[], int position)
{
string price;
// ask what rating to add
cout << "Enter the rating you want to add to the game: ";
cin >> price;
// add the price entered above to the next avaiable index
// within the dataBase member variable (which is an array)
// of the current games
game[position].dataBase[game[position].gameRating] = price;
// increment the number of rating in that person's dataBase
game[position].gameRating++;
}
void outputData(const gameType game[], int currentgame)
{
for (int i = 0; i < currentgame; i++)
{
cout << left << setw(25) << "Name:" << game[i].name << endl;
cout << left << setw(25) << "Genre:" << game[i].type << endl;
cout << left << setw(25) << "Price:" << "$" << game[i].price << endl;
cout << left << setw(25) << "Publisher:" << game[i].publisher << endl;
// inner loop to output each rating in their dataBase
for (int j = 0; j < game[i].gameRating; j++)
cout << setw(25) << "Game Rating:" << " " << left << setw(30) << game[i].dataBase[j] << endl;
cout << endl;
}
}
void showgameByName(const gameType game[], int currentgame)
{
for (int i = 0; i < currentgame; i++)
cout << left << setw(3) << (i + 1) << game[i].name << endl;
}
int loadFromFile(gameType game[])
{
ifstream fin;
fin.open("data.dat");
int num = 0;
getline(fin, game[num].name);
while (!(fin.eof()))
{
fin >> game[num].type;
fin >> game[num].price;
fin >> game[num].publisher;
fin >> game[num].gameRating;
// inner loop to get each rating in their dataBase
for (int j = 0; j < game[num].gameRating; j++)
fin >> game[num].dataBase[j];
num++;
fin.get();
getline(fin, game[num].name);
}
return num;
}
void saveToFile(const gameType game[], int currentgame)
{
ofstream fout;
fout.open("data.dat");
for (int i = 0; i < currentgame; i++)
{
fout << game[i].name << endl
<< game[i].type << endl
<< game[i].price << endl
<< game[i].publisher << endl;
fout << game[i].gameRating << endl;
// inner loop to output each rating in their dataBase
for (int j = 0; j < game[i].gameRating; j++)
fout << game[i].dataBase[j] << endl;
}
}
int findGame(const gameType game[], int currentgame, string name)
{
for (int i = 1; i < currentgame; i++)
{
if (game[i].name == name);
return i;
}
return -1;
}
---------
↧