compiling program / segmentation fault

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 803 Views - Last Post: 30 November 2017 - 04:52 PM Rate Topic: -----

#1 stalktiger  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 30-November 17

compiling program / segmentation fault

Posted 30 November 2017 - 06:33 AM

I have attached my current programming attempt. Please forgive my ignorance but I am missing the problem.

The program is compiling.
When I enter the in file name and out file name, it says "segmentation fault (core dumped)"
I googled what that meant and it said that my program is gone.

Before the program apparently died, the only problem was that it printed out the original lines from the in file, but did not change the "blank" 's and print out the changed file.


If anyone can help - I really appreciate it !




/* Program: prog5B.cpp
By: Mackenzie Ritter
Last Modified: Nov 23, 2017
Purpose: To produce a filled out madlibs with users help.
Notes:
*/
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>

using namespace std ;

string openInfile (ifstream&) ;
void openOutfile (ofstream&) ;
void change (string&, string&) ;

int main ()
{
string line = " " ;
string word = " " ;
string diffLine = " " ;
ifstream ins ; //ins is an input stream
ofstream outs ; // outs is an output stream
openInfile (ins) ;
istringstream iss (line) ;
while (getline(ins, line))
{
diffLine = " " ;
cout << line << endl ;
while (iss >> word)
{
change (word, diffLine) ;
}
cout << diffLine << endl ;
}
ins.close () ;
outs.close () ;
cin.ignore () ;
return 0 ;
}

/* Function: openInfile
Last Modified: Nov 23, 2017
Purpose: Opens the input file after getting file name from user.
In Parameters: None
Out Parameters: string fileName
Return Value: None
*/

string openInfile (ifstream& ins)
{
string fileName = " " ;
ofstream outs ; // outs is an output stream
cout << "Enter file of madlibs outline." << endl ;
cin >> fileName ;
ins.open(fileName.c_str()) ; //connects ins to file inFile
if (ins.fail())
{
cerr << "Error: Unable to open file : " << fileName << endl ;
}
else
{
openOutfile (outs) ;
}
}

/* Function: openOutfile
Last Modified: Nov 23, 2017
Purpose: Opens the output file after getting file name from user.
In Parameters: None
Out Parameters: string copyFile
Return Value: None
*/

void openOutfile (ofstream& outs)
{
string copyFile = " " ;
cout << "Enter name of file for updated data." << endl ;
cin >> copyFile ;
outs.open(copyFile.c_str()) ;
}

/* Function: change
Last Modified: Nov 23, 2017
Purpose: Replaces blanks with words from user.
In Parameters: string word, diffLine
Out Parameters: string fileName
Return Value: None
*/

void change (string&word, string&diffLine)
{
string searchN = "blank-N" ;
string searchA = "blank-A" ;
string searchV = "blank-V" ;
string searchP = "blank-P" ;
string searchD = "blank-D" ;
string noun, adjective, verb, place, adverb ;
if (word == searchN)
{
cout << "Enter a noun." << endl ;
cin >> noun ;
diffLine = diffLine + noun + " " ;
}
else if (word == searchA)
{
cout << "Enter an adjective." << endl ;
cin >> adjective ;
diffLine = diffLine + noun + " " ;
}
else if (word == searchV)
{
cout << "Enter a verb." << endl ;
cin >> verb ;
diffLine = diffLine + noun + " " ;
}
else if (word == searchP)
{
cout << "Enter a place." << endl ;
cin >> place ;
diffLine = diffLine + noun + " " ;
}
else if (word == searchD)
{
cout << "Enter an adverb." << endl ;
cin >> adverb ;
diffLine = diffLine + noun + " " ;
}
else
{
diffLine = diffLine + word + ' ' ;
}
}

Is This A Good Question/Topic? 0
  • +

Replies To: compiling program / segmentation fault

#2 jimblumberg  Icon User is online

  • member icon

Reputation: 5360
  • View blog
  • Posts: 16,735
  • Joined: 25-December 09

Re: compiling program / segmentation fault

Posted 30 November 2017 - 07:01 AM

Have you run your program with your debugger? Your debugger should be able to tell you exactly where the problem is detected and allow you to view the variables at the time of the crash.

Also in future please use code tags when posting code!

:code:

Jim
Was This Post Helpful? 0
  • +
  • -

#3 stalktiger  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 30-November 17

Re: compiling program / segmentation fault

Posted 30 November 2017 - 07:09 AM

Sorry about the missing Tags -- The debugger said that the code is gone...??

Debugger said the program is gone...




 /* 	Program:	prog5B.cpp
	By:		Mackenzie Ritter
	Last Modified:	Nov 23, 2017
	Purpose:	To produce a filled out madlibs with users help.
	Notes:
*/
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>

using namespace std ;

string openInfile (ifstream&) ;
void openOutfile (ofstream&) ;
void change (string&, string&) ;

int main ()
{
	string line = " " ;
	string word = " " ;
	string diffLine = " " ;
	ifstream ins ;		//ins is an input stream
	ofstream outs ;		// outs is an output stream
	openInfile (ins) ;
	istringstream iss (line) ;
	while (getline(ins, line))
	{
		diffLine = " " ;
		cout << line << endl ;
		while (iss >> word)
		{
			change (word, diffLine) ;
		}
	cout << diffLine << endl ;
	}
	ins.close () ;
	outs.close () ;
	cin.ignore () ;
	return 0 ;
}

/*	Function:	openInfile
	Last Modified:	Nov 23, 2017
	Purpose:	Opens the input file after getting file name from user.
	In Parameters:	None
	Out Parameters:	string fileName
	Return Value:	None
*/

string openInfile (ifstream& ins)
{
	string fileName = " " ;
	ofstream outs ;		// outs is an output stream
	cout << "Enter file of madlibs outline." << endl ;
	cin >> fileName ;
	ins.open(fileName.c_str()) ;		//connects ins to file inFile
	if (ins.fail())
	{
		cerr << "Error: Unable to open file : " << fileName << endl ;
	}
	else
	{
		openOutfile (outs) ;
	}
}

/*	Function:	openOutfile
	Last Modified:	Nov 23, 2017
	Purpose:	Opens the output file after getting file name from user.
	In Parameters:	None
	Out Parameters:	string copyFile
	Return Value:	None
*/

void openOutfile (ofstream& outs)
{
	string copyFile = " " ;
	cout << "Enter name of file for updated data." << endl ;
	cin >> copyFile ;
	outs.open(copyFile.c_str()) ;
}

/*	Function:		change
	Last Modified:	Nov 23, 2017
	Purpose:		Replaces blanks with words from user.
	In Parameters:	string word, diffLine
	Out Parameters:	string fileName
	Return Value:	None
*/

void change (string&word, string&diffLine)
{
	string searchN = "blank-N" ;
	string searchA = "blank-A" ;
	string searchV = "blank-V" ;
	string searchP = "blank-P" ;
	string searchD = "blank-D" ;
	string noun, adjective, verb, place, adverb ;
	if (word == searchN)
	{
		cout << "Enter a noun." << endl ;
		cin >> noun ;
		diffLine = diffLine + noun + " " ;
	}
	else if (word == searchA)
	{
		cout << "Enter an adjective." << endl ;
		cin >> adjective ;
		diffLine = diffLine + noun + " " ;
	}
	else if (word == searchV)
	{
		cout << "Enter a verb." << endl ;
		cin >> verb ;
		diffLine = diffLine + noun + " " ;
	}
	else if (word == searchP)
	{
		cout << "Enter a place." << endl ;
		cin >> place ;
		diffLine = diffLine + noun + " " ;
	}
	else if (word == searchD)
	{
		cout << "Enter an adverb." << endl ;
		cin >> adverb ;
		diffLine = diffLine + noun + " " ;
	}
	else 
	{
		diffLine = diffLine + word + ' ' ;
	}
} 

Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon

Reputation: 5360
  • View blog
  • Posts: 16,735
  • Joined: 25-December 09

Re: compiling program / segmentation fault

Posted 30 November 2017 - 07:15 AM

What compiler are you using?

How are you trying to compile this program?

Do you get any compile warnings or errors when you try to compile the program?


I've never seen a debugger say that "The debugger said that the code is gone...??", please post the complete message exactly as it appears in your development environment.


Jim
Was This Post Helpful? 0
  • +
  • -

#5 stalktiger  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 30-November 17

Re: compiling program / segmentation fault

Posted 30 November 2017 - 07:56 AM

Using Putty -- and g++ to compile the program --

When i enter the in file name and out file name, it says "segmentation fault (core dumped)"

Before the program apparently died, the only problem was that it printed out the original lines from the in file, but did not change the "blank" 's and print out the changed file.

Dale
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is online

  • member icon

Reputation: 5360
  • View blog
  • Posts: 16,735
  • Joined: 25-December 09

Re: compiling program / segmentation fault

Posted 30 November 2017 - 08:01 AM

How exactly are you trying to run this program, give the command line that you used to run the program with the debugger.



Jim
Was This Post Helpful? 0
  • +
  • -

#7 stalktiger  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 30-November 17

Re: compiling program / segmentation fault

Posted 30 November 2017 - 08:13 AM

I copied the entire program into the debugger and hit "run" --


To run the program - it takes input from the "in" file and is supposed to copy it to the "out" file but changes the blanks and asks the user to input their own words -- it is supposed to function like a Mad-Lib

but when it runs it is only outputting what is in the "in" file and doesn't change the words and "output" anything else

I think that is what you are asking -- sorry I am a rookie at this...
Was This Post Helpful? 0
  • +
  • -

#8 Thomas1965  Icon User is offline

  • D.I.C Regular

Reputation: 71
  • View blog
  • Posts: 303
  • Joined: 09-September 16

Re: compiling program / segmentation fault

Posted 30 November 2017 - 10:09 AM

Your openInfile function doesn't have a return statement. Not sure if this could cause a segmentation fault but you should correct it, probably declaring openInfile as void.
Was This Post Helpful? 0
  • +
  • -

#9 stalktiger  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 30-November 17

Re: compiling program / segmentation fault

Posted 30 November 2017 - 10:31 AM

Thanks Thomas - I have tried changing that back and forth and it didn't correct the problem. I will fix that though...and hope !!
Was This Post Helpful? 0
  • +
  • -

#10 Thomas1965  Icon User is offline

  • D.I.C Regular

Reputation: 71
  • View blog
  • Posts: 303
  • Joined: 09-September 16

Re: compiling program / segmentation fault

Posted 30 November 2017 - 10:38 AM

What's the content of your input file ?
Was This Post Helpful? 0
  • +
  • -

#11 stalktiger  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 30-November 17

Re: compiling program / segmentation fault

Posted 30 November 2017 - 10:58 AM

It is like a Mad-Lib...it includes sentences that contain blanks. For example "blank-N" for nouns...which should then prompt the user to enter a noun to change it in the out file.
Was This Post Helpful? 0
  • +
  • -

#12 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5955
  • View blog
  • Posts: 20,409
  • Joined: 05-May 12

Re: compiling program / segmentation fault

Posted 30 November 2017 - 11:10 AM

What is a "Mad-Lib"?
Was This Post Helpful? 0
  • +
  • -

#13 stalktiger  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 30-November 17

Re: compiling program / segmentation fault

Posted 30 November 2017 - 12:13 PM

Mad-Libs were an old game -- There were sentences with blanks -- and you would ask someone to give you a verb/noun/adjective to put in the blank without knowing the context of the sentence. Then you would place their words in and read the sentence. Good fun...
Was This Post Helpful? 0
  • +
  • -

#14 Thomas1965  Icon User is offline

  • D.I.C Regular

Reputation: 71
  • View blog
  • Posts: 303
  • Joined: 09-September 16

Re: compiling program / segmentation fault

Posted 30 November 2017 - 12:16 PM

So I guess one line of input could look like this:
Ouch! he said blank-D as he jumped into his convertible blank-N and drove off with his blank-A wife.
Example from https://en.wikipedia.org/wiki/Mad_Libs
Was This Post Helpful? 0
  • +
  • -

#15 stalktiger  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 30-November 17

Re: compiling program / segmentation fault

Posted 30 November 2017 - 12:30 PM

Yes - the D would be an adverb, the N a noun, and the A an adjective
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2