Project help

Hi, this is my first time posting on this forum. I need help with a project of mine that is supposed to allow the user to input input video games to database. I'm having two different problems at the moment. One problem is that my search function isn't working. Whenever i ask to search for video game, it only returns the first video game that was entered; not the one i asked for. The second problem I'm having is that my load from a file function isn't working. Every time i run it, the program crashes and it returns with a visual studio error. Any help would be great, this is due in a couple of days.

#include<iostream>
#include<iomanip>
#include<string>
#include<fstream>
using namespace std;

const int CATALOG = 50;

struct videoGame
{
double ratings[CATALOG];
double rating;
string genre;
string name;
int currentInv;
};

int gameMenu();
videoGame addGame();
void addRating(videoGame games[], int location, int currentTitles);
void outputScreen(const videoGame games[], int currentTitles);
void showGameByTitle(const videoGame games[], int currentTitles);
int loadFromFile(videoGame games[], int currentTitles);
void saveToFile(const videoGame games[], int currentTitles);
int findGame(const videoGame games[], int currentTitles, string name);

int main()
{
videoGame games[20];
int selection;
string searchGame;

int currentTitles = 0;

do
{
selection = gameMenu();

if (selection == 1)
{
games[currentTitles] = addGame();
currentTitles++;
}
else if (selection == 2)
{
if (currentTitles == 0)
{
cout << "You don't have any games saved." << endl;
}

else if (currentTitles >= 1)
{
int location;

cout << "Which game would you like to add a rating for?" << endl;

showGameByTitle(games, currentTitles);
cin >> location;

addRating(games, --location, currentTitles);

cout << "Which game would you like to add a rating for?" << endl;

showGameByTitle(games, currentTitles);
cin >> location;

addRating(games, --location, currentTitles);
}

}
else if (selection == 3)
{
outputScreen(games, currentTitles);
}

else if (selection == 4)
{
loadFromFile(games, currentTitles);
}

else if (selection == 5)
{
saveToFile(games, currentTitles);
}

else if (selection == 7)
{
if (currentTitles == 0)
{
cout << "You don't have any games saved." << endl;
}

else if (currentTitles >= 1)
{
cout << "What is the name of the game you are looking for?";
cin >> searchGame;
cout << findGame(games, currentTitles, searchGame);
}
}

} while (selection != 6);


return 0;
}

int gameMenu()
{
int selection;
cout << "1. Add a game" << endl
<< "2. Add Video Game Rating" << endl
<< "3. Output all Games" << endl
<< "4. Load Games from file" << endl
<< "5. Save Games to file " << endl
<< "6. Leave Program" << endl
<< "7. Search Video Game by Name" << endl;
cin >> selection;
return selection;
}

videoGame addGame()
{
videoGame temp;

cout << "Enter the name of the game: ";
cin.get();
getline(cin, temp.name);

cout << "Enter the genre of the game: ";
//cin.get();
getline(cin, temp.genre);

cout << "Enter the rating of the game: ";
//cin.get();
cin>> temp.rating;

temp.currentInv = 0;

return temp;
}

void addRating(videoGame games[], int location, int currentTitles)
{
double addition;

cout << "Enter the rating for the game: " << endl;
cin >> addition;

games[location].ratings[games[location].currentInv] = addition;

games[location].currentInv++;
}
void outputScreen(const videoGame games[], int currentTitles)
{
if (currentTitles == 0)
{
cout << "You don't have any games saved." << endl;
}

else if (currentTitles >= 1)
{
for (int i = 0; i < currentTitles; i++)
{
cout << left << setw(25) << games[i].name
<< setw(5) << games[i].genre
<< setw(10) << games[i].rating << endl;

for (int j = 0; j < games[i].currentInv; j++)
cout << setw(6) << " " << left << setw(30) << games[i].ratings[j] << endl;
}
}

}

void showGameByTitle(const videoGame games[], int currentTitles)
{
if (currentTitles == 0)
{
cout << "You don't have any games saved." << endl;
}

else if (currentTitles >= 1)
{
for (int i = 0; i < currentTitles; i++)
cout << left << setw(3) << (i + 1) << games[i].name << endl;
}

}

int loadFromFile(videoGame games[], int currentTitles)
{
if (currentTitles == 0)
{
cout << "You don't have any games saved." << endl;
}

else if(currentTitles >= 1)
{
string filename;
ifstream fin;
cout << "What is the name if the file to open?" << endl;
cin >> filename;
filename = filename + ".txt";
fin.open(filename);
int num = 0;

getline(fin, games[num].name);

while (!(fin.eof()))
{

fin >> games[num].genre;
fin >> games[num].rating;

for (int j = 0; j < games[num].currentInv; j++)
fin >> games[num].name[j];
num++;
fin.get();
getline(fin, games[num].name);
}
return num;
}

}

void saveToFile(const videoGame games[], int currentTitles)
{
if (currentTitles == 0)
{
cout << "You don't have any games saved." << endl;
}

else if (currentTitles >=1)
{
ofstream fout;
string filename;
cout << "What would you like you file name to be?(without the '.txt')" << endl;
cin >> filename;
filename = filename + ".txt";
fout.open(filename);

for (int i = 0; i < currentTitles; i++)
{
fout << games[i].name << endl
<< games[i].genre << endl
<< games[i].rating << endl;
fout << games[i].currentInv << endl;

for (int j = 0; j < games[i].currentInv; i++)
fout << games[i].name[j] << endl;
}
}

}

int findGame(const videoGame games[], int currentTitles, string name)
{
if (currentTitles == 0)
{
cout << "You don't have any games saved." << endl;
}

else if (currentTitles >= 1)
{
for (int i = 0; i < currentTitles; i++)
{
if (games[i].name == name)
cout << "There is/are " << games[i].currentInv << " games with this name." << endl;
cout << games[i].name << left << setw(10)
<< games[i].genre << setw(10) << games[i].rating << endl;
cout << endl;
return i;
cout << endl;
}
return -1;
}

}
For the function loadFromFile(...) you shouldn't pass currentTitles because within this function you determine this value. currentTitles contains an old value which is not longer of interest.

There are two reasons why it might crash within loadFromFile(...):

1. num exceeds the maximum possible number of entries (20).
2. currentInv is not loaded from the file and hence does not contain a valid value. You save it to the file so you need to load it from the file as well.

Since currentInv is the number of entries in ratings make sure that it does not exceed CATALOG.

In findGame(...) you have the problem of missing {} regarding the if. Thus the first cout will be within the branch of the if while everything else (including return i;) will be executed regardless.
yeah looks like you are missing curly braces for the if statement in your findgame function
Ok, putting the curly braces worked, thank you. But when i tried to increase the CATALOG size, the loadFromFile function still fails. How do i implement the second option you suggested? Also, i threw in a statement that will let the use know if they entered an incorrect name in the searchGame function, but it will print as many times as you have games stored, instead of just once. I assumed that its because its in a for loop, but how do i use it outside the for loop? Thank you again for helping.
Sorry i meant findGame, not searchGame. Here is updated code:

//This function is supposed to find a game based on its name
int findGame(const videoGame games[], int currentTitles, string name)
{
if (currentTitles == 0)
{
cout << "You don't have any games saved." << endl;
}

else if (currentTitles >= 1)
{
for (int i = 0; i < currentTitles; i++)
{
if (games[i].name.compare(name) == 0)
{
cout << "There is/are " << i << " games with this name." << endl;
cout << endl;
cout << games[i].name << left << setw(5)
<< games[i].genre << right << setw(10) << games[i].rating << endl;
//return i;
cout << endl;
}

else if (games[i].name.compare(name) != 0)
{
cout << "There are no games with this name." << endl;
cout << "Please be case sensitive when searching." << endl;
}
}
return -1;
cout << endl;
}
}
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
int findGame(const videoGame games[], int currentTitles, string name)
{
if (currentTitles == 0)
{
cout << "You don't have any games saved." << endl;
}

else if (currentTitles >= 1)
{
  int count = 0;
  std::stringstream ss; // Note: #include <sstream>
for (int i = 0; i < currentTitles; i++)
{
  if (games[i].name == name)
  {
    ++count;
    ss  << games[i].name << left << setw(5)
<< games[i].genre << right << setw(10) << games[i].rating << endl;
  }
}
if (count > 0)
{
cout << "There is/are " << count << " games with this name." << endl;
cout << endl;
cout << ss.str();
//return i;
cout << endl;
}

else
{
cout << "There are no games with this name." << endl;
cout << "Please be case sensitive when searching." << endl;
}
return -1;
cout << endl;
}
}
Note: Not tested.
Topic archived. No new replies allowed.