Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The goal of this program is to take a file and ask the user for nouns, verbs, etc. in order to fill out a sentence. The new results will be saved to a second file. I keep receiving the following errors:

prog5B.cpp: In function ‘int openInfile()’:
prog5B.cpp:47: error: expected initializer before ‘.’ token
prog5B.cpp:48: error: ‘ins’ was not declared in this scope
prog5B.cpp: In function ‘void openOutfile()’:
prog5B.cpp:72: error: expected initializer before ‘.’ token

Thank you to anyone who can help me!

/* 	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 ;

int openInfile () ;
void openOutfile () ;
void change (string&, string&) ;

int main ()
{
	string line, word, diffLine ;
	ifstream ins ;		//ins is an input stream
	ofstream outs ;		// outs is an output stream
	openInfile () ;
	openOutfile () ;
	change (word, diffLine) ;
	while (getline(ins, line))
	{
		outs << line << endl ;
	}
	ins.close () ;
	outs.close () ;
}

/*	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
*/

int openInfile ()
{
	string fileName ;
	cout << "Enter file of madlips outline." << endl ;
	cin >> fileName ;
	ifstream ins.open(fileName) ;		//connects ins to file inFile
	if (ins.fail (fileName))
	{
		cerr << "Error: Unable to open file : FILENAME" << endl ;
	return -1 ;		//return if failure
	}
	else
	{
		openOutfile () ;
	}
}

/*	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 ()
{
	string copyFile ;
	cout << "Enter name of file for updated data." << endl ;
	cin >> copyFile ;
	ofstream outs.open(copyFile) ;
}

/*	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 ;
		noun = searchN ;
	}
	else if (word == searchA)
	{
		cout << "Enter an adjective." << endl ;
		cin >> adjective ;
		adjective = searchA ;
	}
	else if (word == searchV)
	{
		cout << "Enter a verb." << endl ;
		cin >> verb ;
		verb = searchV ;
	}
	else if (word == searchP)
	{
		cout << "Enter a place." << endl ;
		cin >> place ;
		place = searchP ;
	}
	else if (word == searchD)
	{
		cout << "Enter an adverb." << endl ;
		cin >> adverb ;
		adverb = searchD ;
	}
	else 
	{
		word = word ;
	}
}


What I have tried:

I have adjusted the program many times in regards to the ins and outs.
Posted
Updated 26-Nov-17 22:09pm

The ins and outs variables are local and accessible only in the function in which it is declared.
You could declare the variables in main and pass the variables to the functions as parameters -
ifstream ins;    //ins is an input stream
ofstream outs;         // outs is an output stream
openInfile(ins);
openOutfile(outs);

int openInfile(ifstream& ins) { ... }
int openOutfile(ofstream& outs) { ... }
 
Share this answer
 
v4
C++
ifstream ins.open(fileName) ;       //connects ins to file inFile

You cannot instantiate an object and call one of its methods in a single line like the above. It must be:
C++
ifstream ins;       // create the stream object
ins.open(fileName) ;    // connects ins to file fileName

Same issue in OpenOutFile.

There are a number of other issues with your code. You create a number of variables in your main method but never initialise them to any values so your code will just randomly fail. I would suggest studying your notes in more detail.
 
Share this answer
 
Comments
CPallini 27-Nov-17 4:28am    
5.

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