1. Engineering
  2. Computer Science
  3. the following program is in c i am trying...

Question: the following program is in c i am trying...

Question details

The following program is in c++ ; I am trying to read in games from a file and when doing so I am only reading in parts of the file. It is giving me a segmentation fault error I am going to post my code below if anyone is able to help me debug the program I would greatly appreciate it. The program is too large to be submitted as a single question on here :(

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

void Business::addGamesFromFile()
{
   ifstream infile;
   string file;
   char line[100];
  
   Game* newGame;
   Text* gName;
   Text* gDescription;
   float gCost;
   int gPlayers;
   int gOccupancy;
   float gDuration;
  
   cout << "Enter the file name: ";
   cin >> file;
   infile.open(file.c_str());
  
   if(infile.good())
   {
       cout << "Your file has been opened." << endl;
       infile.getline(line, 100);
       cout << line << endl;
       while(!infile.eof())
       {
           gName = new Text(line);
           infile.getline(line, 100);
           cout << line;
           gDescription = new Text(line);
           infile >> gCost;
           cout << gCost << endl;
           infile >> gPlayers;
           cout << gPlayers << endl;
           infile >> gOccupancy;
           cout << gOccupancy << endl;
           infile >> gDuration;
           cout << gDuration << endl;
          
           newGame = new Game(gName, gDescription, gCost, gPlayers, gOccupancy, gDuration);
          
          
           gameArray[numGames] = newGame;
           numGames++;
          
           infile.getline(line, 100);
       }
   }
   else
   {
       cout << "\nNo file" << endl;
   }
}

----------------------------------------------------------------------------------

void Business::addItemsFromFile()
{
   ifstream infile;
   string file;
   char line[100];
  
   RentItem* newItem;
   Text* iName;
   Text* iDescription;
   float iCost;
   int iStock;
   float iDuration;
  
   cout << "Enter file name: ";
   cin >> file;
   infile.open(file.c_str());
  
   if(infile.is_open())
   {
       infile.getline(line, 100);
       while(!infile.eof())
       {
           iName = new Text(line);
           infile.getline(line, 100);
           iDescription = new Text(line);
           infile >> iCost;
           infile >> iStock;
           infile >> iDuration;
          
           newItem = new RentItem(iName, iDescription, iCost, iStock, iDuration);
          
           rentItemArray[numRentItems] = newItem;
           numRentItems++;
          
           infile.getline(line, 100);
       }
   }
   else
   {
       cout << "\nNo file" << endl;
   }
}

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution