任何人都可以帮助解释我在这段代码上收到的错误吗? [英] Can anyone help explain the errors I am receiving on this code?

查看:99
本文介绍了任何人都可以帮助解释我在这段代码上收到的错误吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该程序的目标是获取文件并向用户询问名词,动词等,以便填写句子。新结果将保存到第二个文件。我一直收到以下错误:



prog5B.cpp:在函数'int openInfile()'中:

prog5B.cpp:47:错误:'。'之前的预期初始值。令牌

prog5B.cpp:48:错误:'ins'未在此范围内声明

prog5B.cpp:在函数'void openOutfile()':

prog5B.cpp:72:错误:'。'之前的预期初始化程序。令牌



感谢任何有能力的人帮助我!



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.

推荐答案

ins outs 变量是本地的,只能在声明它的函数中访问。

你可以声明 main 中的变量,并将变量作为参数传递给函数 -

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) { ... }


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:

ifstream ins;       // create the stream object
ins.open(fileName) ;    // connects ins to file fileName



OpenOutFile 中的相同问题。



您的代码还有许多其他问题。您在main方法中创建了许多变量,但从未将它们初始化为任何值,因此您的代码将随机失败。我建议更详细地研究你的笔记。


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.


这篇关于任何人都可以帮助解释我在这段代码上收到的错误吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆