Question

The following program is in c++ ; I am trying to read in games from a...

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;
   }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The code seems to be fine. The function which you have given are from definition file, it would be nice if you would have posted the header file contents also. Like rentItemArray and gameArray should be array of pointers.. So they should be declared with double pointer notation, and when you are filling them like gameArray[numGame] = newGame, till that point, the gameArray must have been initialized using new keyword notation.. these array seem to be a proably point of error, because if they have not been initialized and you try to put data into them, you are likely to get segmentation fault.

Still, if it doesnt resolves the issue, you can put some cout statements after each line and chck to see till which point the execution goes, that way you can get to know the line number, which is causing the issue. then we can further debug that piece of code.. hope it helps.

Add a comment
Know the answer?
Add Answer to:
The following program is in c++ ; I am trying to read in games from a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • c++ program that prints joke and its punchline

    nothing happens when I replace the skeleton code. the program runs but it does not give me the desired results. All the question marks must be removed and filled in with the appropriate code.// Function prototypesvoid displayAllLines(ifstream &infile);void displayLastLine(ifstream &infile);int main(){// Arrays for file nameschar fileName1[SIZE];char fileName2[SIZE];// File stream objectsifstream jokeFile;ifstream punchlineFile;// Explain the program to the user.cout << "This program will print a joke "<< "and its punch line.\n\n";// Get the joke file name.cout << "Enter the name of...

  • Hey, so i am trying to have my program read a text file using a structure...

    Hey, so i am trying to have my program read a text file using a structure but i also want to be able to modify the results(I kinda have this part but it could be better). I cant seem to get it to read the file(all the values come up as 0 and i'm not sure why because in my other program where it wrote to the txt file the values are on the txt file) i copied and pasted...

  • Write a simple telephone directory program in C++ that looks up phone numbers in a file...

    Write a simple telephone directory program in C++ that looks up phone numbers in a file containing a list of names and phone numbers. The user should be prompted to enter a first name and last name, and the program then outputs the corresponding number, or indicates that the name isn't in the directory. After each lookup, the program should ask the user whether they want to look up another number, and then either repeat the process or exit the...

  • //This program is your final exam. //Please fill in the functions at the bottom of the...

    //This program is your final exam. //Please fill in the functions at the bottom of the file. (evenCount and insertItem) //DO NOT CHANGE ANYTHING ELSE. //main has all the code needed to test your functions. Once your functions are written, please build and make sure it works fine //Note that in this case, the list is not sorted and does not need to be. Your goal is to insert the number in the given position. #include <iostream> #include <fstream> using...

  • //This program is your final exam. //Please fill in the functions at the bottom of the...

    //This program is your final exam. //Please fill in the functions at the bottom of the file. (evenCount and insertItem) //DO NOT CHANGE ANYTHING ELSE. //main has all the code needed to test your functions. Once your functions are written, please build and make sure it works fine //Note that in this case, the list is not sorted and does not need to be. Your goal is to insert the number in the given position. #include <iostream> #include <fstream> using...

  • I need to make a few changes to this C++ program,first of all it should read...

    I need to make a few changes to this C++ program,first of all it should read the file from the computer without asking the user for the name of it.The name of the file is MichaelJordan.dat, second of all it should print ,3 highest frequencies are: 3 words that occure the most.everything else is good. #include <iostream> #include <map> #include <string> #include <cctype> #include <fstream> #include <iomanip> using namespace std; void addWord(map<std::string,int> &words,string s); void readFile(string infile,map<std::string,int> &words); void display(map<std::string,int>...

  • I need help to fix this program it is written in c++ and needs to run...

    I need help to fix this program it is written in c++ and needs to run in visual studio. Program uses character strings from a file containing on the first line and answer key and every other line after is the students ID number followed by a space then their test answers.Program currently reads only test answer key and student ID and their answers of second line.  Sample data can be found below. TFTTFTFFFTTTFFTTFFFT HUB00123 TFTFFFTFTFTFTTTFTTTT SMU12456 FFTFTTFFTTTTTTFFTTFT #include #include #include...

  • #include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool...

    #include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool openFile(ifstream &); void readData(ifstream &, int [], int &); void printData(const int [], int); void sum(const int[], int); void removeItem(int[], int &, int); int main() { ifstream inFile; int list[CAP], size = 0; if (!openFile(inFile)) { cout << "Program terminating!! File not found!" << endl; return -1; } //read the data from the file readData(inFile, list, size); inFile.close(); cout << "Data in file:" <<...

  • c++ In the following segment of the code, what will be the output if opening of...

    c++ In the following segment of the code, what will be the output if opening of the file is not successful? ifstream infile; try{ infile.open("test.txt"); if(!infile) throw -1; else cout << "successful" << endl; } catch(const char* msg) { cout << msg << " unsuccessful" << endl; } catch(int i) { cout << i < was returned, unsuccessful" <<endl; #8 WA w

  • I have 2 issues with the C++ code below. The biggest concern is that the wrong...

    I have 2 issues with the C++ code below. The biggest concern is that the wrong file name input does not give out the "file cannot be opened!!" message that it is coded to do. What am I missing for this to happen correctly? The second is that the range outputs are right except the last one is bigger than the rest so it will not output 175-200, it outputs 175-199. What can be done about it? #include <iostream> #include...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT