#include <iostream>
#include <fstream>
#include <string>
#include "LinDicT.h"

using namespace std;


int main()
{
    LinDicT l; 
	ifstream inFile;   // input file stream variable
	string w, m;
	int choice;
	

    inFile.open("meanings.dat");  // open the input file
    if (!inFile)
    {
       cout << "Cannot open the input file" 
            << "The program terminates." << endl;
       return 1;
    }

    while (!inFile.eof())
    {
        inFile >> w;
		getline(inFile, m, '\n'); 

		l.insert(w, m);

		cout << "Enter your choice: " << endl;
		cin >> choice;

		if(choice == 1)
		{
			l.traverse();
		}
		else if(choice == 2)
		{
		
		  List* def;

	          def = l.lookUp(w);
		
			if(def == NULL)
			{
				cerr << "word not found!" << endl;
			} 
			else
			{
				def->printList();
			}
	    }
		else if(choice == 3)
		{
			break;
	
		}
	
	}
    inFile.close();	

    return 0;
}

When compiling it with other classes 'Segmentation Fault' occurs, can anyone spot an error in the above code please?

Thanks..

As with any file I/O, I will need to see how your txt file data is formated.. white spaces and new lines are important factors to consider when trying to troubleshoot file I/O.

As with any file I/O, I will need to see how your txt file data is formated.. white spaces and new lines are important factors to consider when trying to troubleshoot file I/O.

provoke "To deliberately act in a way intended to anger someone."
data "In everyday language is a synonym for information."
BA "Bachelor of Arts"
bluebell "A woodland plant with blue bell-shaped flowers"
wizard "A person who is talented in some specified field."
provoke "To incite or stimulate."
B2B "business to business."
cook "To prepare by boiling, baking, frying, etc."
program "A list of the acts, speeches, pieces."
Bluetooth "A short-range radio technology that allows wireless communication between computers, mobile phones, etc."
sextet "A composition of six instruments."
multimedia "Refers to the use of electronic media to store and experience multimedia content."
wizard "A man in fairy tales who has magical powers."
provoke "(often foll. by into) to cause a person to react in a particular, often angry, way."
BA "British Airways"


I dont think its to do with this part...

I dont think its to do with this part...

Your mixing of >> extraction and use of getline() combined with your complaint of segmentation fault warrants the viewing of your .txt file data format.

Here is your attempt to read in the .txt file:

while (!inFile.eof())    
{        
     inFile >> w;		
     getline(inFile, m, '\n');  		

     l.insert(w, m); 		
    
     cout << "Enter your choice: " << endl;		
     cin >> choice;

In the above code, you read in the first word of the .txt file using the >> extraction operator, then read in the remainder of the line using getline(). With only the first word and first line extracted from the .txt file, you immediately begin prompting the user for menu choices. Therefore, using the above code, you would have prompt the user once for every line in your .txt file in order to read in the entire file content.

My suggestion would be to read in the entire file first, and then prompt to user for menu choice after the entire file has been loaded:

string w[15], m[15];
int i=0;

//Load the file using a safer method than testing for eof()
while(infile >> w[i])
{
     getline(inFile, m[i], '\n'); 
     i++;
}

//Now that the file is loaded, begin prompting for user action
char ans = '\0';

do{

     cout << "Enter your choice: ";
     cin >> choice;

     switch(choice)
     {
          case 1:  l.traverse();
          break;
          case 2:  l.lookUp(w);
          case 3:
          break;

          default:  cout << "Not a menu choice, try again.";
     }

     cout << "Do you wish to make another menu entry?";
     cin >> ans;

}while(ans == 'Y' || ans == 'y');

My recommendations may or may not solve your problem directly. I didn't compile your code or run it through a debugger. Furthermore, I do not have access to LinDicT.h. I'm not positive, but I have a feeling your segementation fault may have something to do with attempting to read in more .txt file that you have available due to testing for eof() which is not recommended.

Edited 7 Years Ago by Clinton Portis: it was the code ninjas...!!! ahhhhh!!!!

This article has been dead for over six months. Start a new discussion instead.