C++ Random File Access - Game rating Store Application
Posted by khan1488
Last Updated: November 17, 2012
\\System. h

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cstdlib>

using namespace std;

class gamesystem
{
    private:
    int identify;
    char game[100];
    int rating;
    int category;
    double price;

    public:
    gamesystem (int show,string name, int quality, int section, double amount )
    {
       void SetGame (string name);
       string Getgame ();
       void setRating ( int quality);
       int getRating ();
       void setCategory (int section);
       int getCategory ();
       void setPrice (double amount);
       double getPrice();
       void Setidentify ( int show);
       int Getidentify ();
        }
      void Setidentify ( int show)
      {
         identify= show;

          }
      int Getidentify ()
      {
         return identify;
          }

    void SetGame (string name1)
    {
        const char *name= name1.data();
        int size = name1.size ();
        size = (size<13?size :12);
        strncpy (game, name, size);
        game[size]='\0';
                }


string Getgame()
{
    return game;

    }
void setRating(int quality)
{
    rating = quality;

    }
int getRating ()
{
   return rating;
    }
void setCategory(int section)
{
   category= section;
    }
int getCategory ()
{
    return category;
    }
void setPrice (double amount)
{
   price= amount;
    }
double getPrice ()
{

   return price;
    }


    };


Main.cpp


#include <iostream>
#include "system.h"
#include <string>
#include <iomanip>
#include <fstream>
#include <cstdlib>


using std::ofstream;

using namespace std;
gamesystem gamestop (0,"", 0,0,0.0);
void Menu();
void header ();
void create();
void write ();
void print_screen ();
void Erase();
void showgame(int);
int search1();
void print ();
void add ();
void search();
int main()
{
    cout << "Game Stop..One stop Game shop!" << endl;
    Menu();

        return 0;
}


 void create()
 {
   ofstream outgame( "videogames.dat", ios::out );
     if ( !outgame )
     {
         cerr << "File could not be opened." << endl;
         exit( 1 );

         }
    for ( int i = 0; i < 100; i++ )
    outgame.write( reinterpret_cast< const char * >( &gamestop ),sizeof(gamesystem));
     }

void write ()
{
    int record;
  char game_name [100];
  int cat;
  int ratn;
  double cost;


  fstream outgame( "videogames.dat", ios::in | ios::out | ios::binary );

     if ( !outgame )
     {
         cerr << "File could not be opened." << endl;
         exit( 1 );
     }

    cout<<"Please enter the game number 1 to 100 and the integer 0 to exit\n";
    cin>> record;
    while (record>0 &&record<= 100)
    {
        cout <<"Please enter the game data in the form 'game name', category, rating and price"<<endl;
        cin>>game_name>> setw(15)>>cat>>setw(15)>>ratn>>setw(15)>>cost;
        gamestop.Setidentify (record);
        gamestop.SetGame (game_name);
        gamestop.setRating ( ratn);
        gamestop.setCategory (cat);
        gamestop.setPrice (cost);

        outgame.seekp ((gamestop.Getidentify ()-1)*sizeof (gamesystem));
        outgame.write( reinterpret_cast< const char * >( &gamestop ),sizeof(gamesystem));

        cout<<"Please enter the game number 1 to 100 and the integer 0 to exit\n";
        cin >>record;


        }



    }

    void header ()
  {

cout<<"Please select:\n";
cout<<"1- to create file"<<endl
    <<"2- to Update file "<<endl
    <<"3- to view complete database "<<endl
    <<"4-to print hardcopy "<<endl
    <<"5- to Modify data "<<endl
    <<"6-to delete "<<endl
    <<"7-Search the database "<<endl
    <<"8-Exit"<<endl;

  }

   void Menu()
   {
       header();

   int choice=0;
      cin>>choice;

   while(choice>0 && choice<=7)
   {
    switch (choice)
    {
     case 1:
     create ();
     break;

     case 2:
     write ();
     break;

    case 3:
     print();
     break;

     case 4:
     print_screen();
     break;

     case 5:
     add();
     break;

     case 6:
     Erase ();
     break;

     case 7:
     search ();
     break;
}

header();
cin>>choice;
}
   }




    int search1 ()
    {
     int choice=0;
     cout<<endl<<setw(50)<<"Game locator"<<endl;
     cout<<endl<<" please Select Search Type"<<endl
         <<"1-Game name search"<<endl
         <<"2-Rating search"<<endl
         <<"3-Category search"<<endl
         <<"4-Price Search"<<endl
         <<"5-Exit"<<endl;

    cin>>choice;

    return choice;
    }



    void search ()
    {
    ifstream outgame( "videogames.dat", ios::in );

     if ( !outgame )
     {
        cerr << "File could not be opened." << endl;
        exit( 1 );
     }
      system ("cls");

     int choice=search1 ();

     if(choice>0 && choice<=4)
     {
      switch(choice)
      {
       case 1:
       {
        string entered="";
        cout<<endl<<"Enter the name of game: ";
        cin>>entered;


         outgame.seekg(0);

        outgame.read( reinterpret_cast< char * >( &gamestop ), sizeof( gamesystem ) );
         cout<<endl<<left<<setw(8) << "Record"
         << setw( 10 ) << "Game"
         << setw( 10 ) << "Rating"
         << setw( 10 ) << "Category"
         << setw( 10 ) << "Price"<<endl;

        while ( outgame && !outgame.eof() )
        {

          if ( gamestop.Getgame() == entered )
           {
        cout<< left << setw(8) << gamestop. Getidentify()
         << setw( 10 ) << gamestop.Getgame ()
         << setw( 10 ) << gamestop.getRating ()
         << setw( 10 ) << gamestop.getCategory()
         << setw( 10 ) << gamestop.getPrice()<<endl;
           }
            else if( outgame && outgame.eof() && gamestop.Getgame ()!=entered)
                cout<<"Game could not be Found."<<endl;

         outgame.read( reinterpret_cast< char * >( &gamestop ), sizeof(gamesystem  ) );
        }
     break;
     }




     case 2:
       {
        int entered1=0;
        cout<<endl<<"Enter the Rating by which you want to search: ";
        cin>>entered1;

          outgame.seekg(0);

        outgame.read( reinterpret_cast< char * >( &gamestop ), sizeof( gamesystem ) );
       cout<<endl<<left<<setw(8) << "Record"
         << setw( 10 ) << "Game"
         << setw( 10 ) << "Rating"
         << setw( 10 ) << "Category"
         << setw( 10 ) << "Price"<<endl;


        while ( outgame && !outgame.eof() )
        {

          if ( gamestop.getRating() == entered1 )
           {
            cout<< left << setw(8) << gamestop. Getidentify()
         << setw( 10 ) << gamestop.Getgame ()
         << setw( 10 ) << gamestop.getRating ()
         << setw( 10 ) << gamestop.getCategory()
         << setw( 10 ) << gamestop.getPrice()<<endl;
           }
          else if( outgame && outgame.eof() && gamestop.getRating()!=entered1)
               cout<<" we are sorry entry not Found."<<endl;

        outgame.read( reinterpret_cast< char * >( &gamestop ), sizeof( gamesystem ) );
     }
     break;
     }

     case 3:
       {
        int entered2=0;
        int find_cat;
        cout<<endl<<"Please Enter the category of the games you want to locate: ";

     cout<<endl<<"Categories"<<endl
         <<"1-Shooting"<<endl
         <<"2-Racing"<<endl
         <<"3-Adventure"<<endl
         <<"4-Sports"<<endl;
         cin>>entered2;


        cin>>entered2;

    if (entered2==1)
        {
        outgame.seekg(0);

        outgame.read( reinterpret_cast< char * >( &gamestop ), sizeof( gamesystem ) );
        cout<<endl<<left<<setw(8) << "Record"
         << setw( 10 ) << "Game"
         << setw( 10 ) << "Rating"
         << setw( 10 ) << "Category"
         << setw( 10 ) << "Price"<<endl;



        while ( outgame && !outgame.eof() )
        {

         if ( gamestop.getCategory () == entered2 )
           {
           cout<< left << setw(8) << gamestop. Getidentify()
         << setw( 10 ) << gamestop.Getgame ()
         << setw( 10 ) << gamestop.getRating ()
         << setw( 10 ) << gamestop.getCategory()
         << setw( 10 ) << gamestop.getPrice()<<endl;
           }
         else if( outgame && outgame.eof() && gamestop.getCategory()!=entered2)
              cout<<" We are sorry but there are no games currently in that category."<<endl;

         outgame.read( reinterpret_cast< char * >( &gamestop ), sizeof(gamesystem ) );
        }}
     break;
     }

     case 4:
       {
        double entered3=0.0;
        cout<<endl<<"Please Enter the price of the game you want to find: ";
        cin>>entered3;


        outgame.seekg(0);

        outgame.read( reinterpret_cast< char * >( &gamestop ), sizeof( gamesystem ) );
        cout<<endl<<left<<setw(8) << "Record"
         << setw( 10 ) << "Game"
         << setw( 10 ) << "Rating"
         << setw( 10 ) << "Category"
         << setw( 10 ) << "Price"<<endl;

        while (outgame && !outgame.eof() )
        {

          if ( gamestop.getPrice() == entered3 )
           {
           cout<< left << setw(8) << gamestop. Getidentify()
         << setw( 10 ) << gamestop.Getgame ()
         << setw( 10 ) << gamestop.getRating ()
         << setw( 10 ) << gamestop.getCategory()
         << setw( 10 ) << gamestop.getPrice()<<endl;
           }
          else if( outgame && outgame.eof() && gamestop.getPrice()!=entered3)
                cout<<"There are no games at the price you entered."<<endl;

        outgame.read( reinterpret_cast< char * >( &gamestop ), sizeof( gamesystem ) );
        }
     break;
     }
     }//End Switch

    }//End if
    else
    {
     cout<<"Entry is invalid"<<endl;

    }


    }

    void showgame(int showcase)
     {
      ifstream outgame( "videogames.dat", ios::in );

      if ( !outgame )
      {
        cerr << "File could not be opened." << endl;
        exit( 1 );
      }
     }
     void Erase()
    {
     int record=0;
     char game [100]= "";
     int category=0;
     int rating=0;
     double price=0.0;


     fstream outgame ( "videogames.dat",  ios::out |ios::binary | ios::in);

     if ( !outgame )
     {
        cerr << "File could not be opened." << endl;
        exit( 1 );
     }

     cout<<endl<<"Please enter the record number of the record you wish to delete from the game data base(1-100, 0 to exit) ";
     cin>>record;
     showgame (record);



     while ( record > 0 && record <= 100 )
     {

        gamestop.Setidentify (record);
        gamestop.SetGame (game);
        gamestop.setRating ( rating);
        gamestop.setCategory (category);
        gamestop.setPrice (price);

         outgame.seekp( ( gamestop.Getidentify() - 1 ) *
            sizeof( gamesystem ) );
     cout<<endl<<"Please enter the record number of the record you wish to delete from the game data base(1-100, 0 to exit) ";
     cin>>record;
     }


     }
 void print ()
   {
     ifstream outgame( "videogames.dat", ios::in );

     if ( !outgame )
     {
        cerr << "File could not be opened." << endl;
        exit( 1 );
     }



      outgame.read( reinterpret_cast< char * >( &gamestop ), sizeof( gamesystem ) );
        cout<<endl<<left<<setw(8) << "Record"
         << setw( 10 ) << "Game"
         << setw( 10 ) << "Rating"
         << setw( 10 ) << "Category"
         << setw( 10 ) << "Price"<<endl;

     while (outgame && !outgame.eof() )
     {

               if(gamestop.getPrice()!=0.0)
         {cout<< left << setw(8) << gamestop. Getidentify()
         << setw( 10 ) << gamestop.Getgame ()
         << setw( 10 ) << gamestop.getRating ()
         << setw( 10 ) << gamestop.getCategory()
         << setw( 10 ) << gamestop.getPrice()<<endl;}

        outgame.read( reinterpret_cast< char * >( &gamestop ), sizeof( gamesystem ) );
     }
  }

   void print_screen ()
   {
     ifstream outgame( "videogames.dat", ios::in );

    ofstream file("game.txt", ios::out);
     if ( !outgame )
     {
        cerr << "File could not be opened." << endl;
        exit( 1 );
     }



      outgame.read( reinterpret_cast< char * >( &gamestop ), sizeof( gamesystem ) );
        file<<endl<<left<<setw(8) << "Record"
         << setw( 10 ) << "Game"
         << setw( 10 ) << "Rating"
         << setw( 10 ) << "Category"
         << setw( 10 ) << "Price"<<endl;

     while (outgame && !outgame.eof() )
     {

               if(gamestop.getPrice()!=0.0)
         {file<< left << setw(8) << gamestop. Getidentify()
         << setw( 10 ) << gamestop.Getgame ()
         << setw( 10 ) << gamestop.getRating ()
         << setw( 10 ) << gamestop.getCategory()
         << setw( 10 ) << gamestop.getPrice()<<endl;}

        outgame.read( reinterpret_cast< char * >( &gamestop ), sizeof( gamesystem ) );
     }
  }

void add ()
{
    int record;
  char game_name [100];
  int cat;
  int ratn;
  double cost;


  fstream outgame( "videogames.dat", ios::in | ios::out | ios::binary );

     if ( !outgame )
     {
         cerr << "File could not be opened." << endl;
         exit( 1 );
     }

    cout<<"Please enter the game number 1 to 100 and the integer 0 to exit\n";
    cin>> record;
    while (record>0 &&record<= 100)
    {
        cout <<"Please enter the game data in the form 'game name', category, rating and price"<<endl;
        cin>>game_name>> setw(15)>>cat>>setw(15)>>ratn>>setw(15)>>cost;
        gamestop.Setidentify (record);
        gamestop.SetGame (game_name);
        gamestop.setRating ( ratn);
        gamestop.setCategory (cat);
        gamestop.setPrice (cost);

        outgame.seekp ((gamestop.Getidentify ()-1)*sizeof (gamesystem));
        outgame.write( reinterpret_cast< const char * >( &gamestop ),sizeof(gamesystem));

        cout<<"Please enter the game number 1 to 100 and the integer 0 to exit\n";
        cin >>record;


        }



    }