Click here to Skip to main content
15,887,965 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My program is supposed to display the contents of a user-entered file, which it does. The problem is that it does not display the second part, where it replaces the "blank" with a word entered by user. It does not even prompt the user to enter a new word. If anyone can help, thank you.
Here is my code:
/*        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&, string) ;

int main ()
{
	string line = " " ;
	string word = " " ;
	string diffLine = " " ;
	ifstream ins ;		//ins is an input stream
	ofstream outs ;		// outs is an output stream
	line = openInfile (ins) ;
	istringstream iss (line) ;
	while (getline(ins, line))
	{
		while (iss >> word)
		{
			change (word, diffLine, line) ;
		}
	cout << line << endl ;
	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 = " " ;
	string line = " " ;
	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) ;
	}
return line ;
}

/*	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 line)
{
	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 + ' ' ;
	}
}


What I have tried:

I have asked my teacher for so much help and I did change certain things according to what he said, but still the program does not work.
Posted
Updated 29-Nov-17 23:07pm
Comments
cvogt61457 29-Nov-17 20:33pm    
Time to become familiar with your most useful tool, the DEBUGGER.
It is your friend.
Learn to use it.
With the debugger, you will be able to step through the program and see what is happening during execution.

Are the any compile errors or warnings that you are ignoring?

Well, just looking quickly, you declared 2 variables called 'outs' - one in main and one in openInfile.
openInfile calls openOutfile.
However, the outs variable in main is supposed to be used to write the file but it is never set/initialized/used.

What part is displayed? What part is not displayed?
Member 13479017 30-Nov-17 8:09am    
The in file is displayed, but some of the words are supposed to be changed and copied into the out file. That does not happen and nothing is displayed after the original lines.
Richard MacCutchan 30-Nov-17 4:49am    
Your call to openInfile returns a blank line. And it does not prompt the user for a word, since the key you send to the change function is a blank. Your program structure does not look very logical, and is quite difficult to understand.

1 solution

Think about what you are doing here:
line = openInfile (ins) ;
istringstream iss (line) ;
Your openInfile() functions returns the string " " (a single space) which is passed to the istringstream::istringstream - C++ Reference[^] constructor:
Quote:
(2) initialization constructor
Constructs an istringstream object with a copy of str as content.
So your iss stream contains just the single space string. That is extracted later with the >> operator leaving an empty stream object.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900