程序将显示输出的一部分,但缺少第二部分。 [英] Program will display one part of output, but is missing second part.

查看:68
本文介绍了程序将显示输出的一部分,但缺少第二部分。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序应该显示用户输入文件的内容。问题是它没有显示第二部分,它用用户输入的单词替换空白。它甚至不会提示用户输入新单词。如果有人可以提供帮助,谢谢。

这是我的代码:

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.

推荐答案

想想你在这里做了什么:

Think about what you are doing here:
line = openInfile (ins) ;
istringstream iss (line) ;

你的 openInfile()函数返回字符串(单个空格)传递给 istringstream :: istringstream - C ++ Reference [ ^ ]构造函数:

Your openInfile() functions returns the string " " (a single space) which is passed to the istringstream::istringstream - C++ Reference[^] constructor:

引用:

(2)初始化构造函数

构造一个带有str作为内容副本的istringstream对象。

(2) initialization constructor
Constructs an istringstream object with a copy of str as content.

所以你的 iss stream只包含单个空格字符串。稍后将使用>> 运算符提取该对象,并保留空流对象。

So your iss stream contains just the single space string. That is extracted later with the >> operator leaving an empty stream object.


这篇关于程序将显示输出的一部分,但缺少第二部分。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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