CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Issue with search function, and rating system

  1. #1
    Join Date
    Mar 2017
    Posts
    14

    Issue with search function, and rating system

    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<iostream>
    #include<iomanip>
    #include<string>
    #include<fstream>
    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;
    }
    Last edited by TheJollyRoger; May 2nd, 2017 at 01:05 PM.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,932

    Re: Issue with search function, and rating system

    Quote Originally Posted by TheJollyRoger View Post
    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 ?
    ...
    Did you try to debug your code?
    Victor Nijegorodov

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,429

    Re: Issue with search function, and rating system

    Code:
    cout << "Enter one of the following: Name of Video Game, Type of Video Game, or Rating of Video Game: " << endl;
    cin >> name, type, gameRating;
    This does not do what you are expecting! It sets name to be the input chars up to a white-space char and then evaluates the value of type and then evaluates the value of gameRating. If you are expecting the user to enter either one of the 3 types of input as suggested by the cout statement, then you need a single cin statement into a string variable and then you have to determine whether the user entered either a name, a type or a rating in the code!

    Also
    Code:
    for (int i = 1; i < currentgame; i++)
    array indexes start at 0, not 1!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++20 Compiler: Microsoft VS2019 (16.8.6)

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,932

    Re: Issue with search function, and rating system

    Quote Originally Posted by TheJollyRoger View Post
    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:
    		choice = menu();
    
    		// process that choice
    		if (choice == 1)
    		{
    			// call the addGames function, which returns a variable
    			...
    		}
    
    		else if (choice == 2)
    		{
    			...
    		}
    		...
    I'd use here the switch construction rather than this if () ...else if () ...
    Victor Nijegorodov

  5. #5
    Join Date
    Mar 2017
    Posts
    14

    Re: Issue with search function, and rating system

    I am new to all this, what do you mean when you say debugging? What I consider a bug is when the code isn't running, but this code works. It opens and runs on terminal.

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,429

    Re: Issue with search function, and rating system

    Debugging is using the debugger that comes with Visual Studio to trace through the code and see the values of the various variables. Within the debugger you can single step through the code or set breakpoints so that the code executes up to the specified point and then stops so that you can examine variable values etc and then restart. Using the debugger is a skill that every programmer needs to acquire.

    To use the debugger, look at the top toolbar in VS IDE and you'll see a top menu item called Debug. Click that and it will give you a drop-down menu offering the debugging options. See https://msdn.microsoft.com/en-us/library/mt243867.aspx for an intro on using the debugger. This relates to c#, but the way it's used applies to c++ as well. Note that you need to have compiled as a debug build, not as a release build.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++20 Compiler: Microsoft VS2019 (16.8.6)

  7. #7
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,607

    Re: Issue with search function, and rating system

    Quote Originally Posted by TheJollyRoger View Post
    What I consider a bug is when the code isn't running, but this code works. It opens and runs on terminal.
    Well, merely running a code typically is not enough. Good code does exactly what you expect it to do. And in case the running code does crazy things, how could you consider it is not a bug? Any flaw in the program logic is a bug, and debugging is finding the flaws.
    Best regards,
    Igor

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)