【发布时间】:2016-02-07 19:15:27
【问题描述】:
我正在编写一个程序来转换用户提供的文本。我已经在测试程序中单独尝试了这种方法,并且效果很好;但是,当我尝试将其实现到更大的程序中时,用户无法为程序提供要存储的输入。相关代码如下:
int main()
{
string str = "NULL";
int mappings = 0;
readMappings(mappings);
receiveInput(str);
displayInput(mappings, str);
return 0;
}
void readMappings(int &count)
{
ifstream readMappings; // Creates the function "readMappings"
string filename; // Generates the filename variable to search for mapping document
cout << "Enter the filename of the mapping document: ";
cin >> filename; // User enters filename
readMappings.open(filename); // "readMappings" function opens the given mappings document
while (!readMappings.is_open())
{
cout << "Unsble to open file. Please enter a valid filename: "; // If the user enters an invaled filename, the program will ask again
cin >> filename;
readMappings.open(filename);
}
if (readMappings.good()) // Mapping document is successfully opened
{
readMappings >> count; // Reads first line
}
readMappings.close(); // If everything fails in this function, the document will close
}
void receiveInput(string &input)
{
char correctness;
do {
cout << "\nPlease enter the text you would like to be converted to NATO:\n";
getline(cin, input);
cout << "You are about to convert: \"" << input << "\".\nIs this correct? (Y/N)" << endl;
cin >> correctness;
} while (correctness == 'N' || correctness =='n');
}
我认为可能是程序在等待用户的另一个输入,所以我添加了一个变量,我认为它已经填充了,但它并没有解决我的解决方案。在我看来,问题出在 receiveInput 函数中,但我可能是错的。提前感谢您的任何帮助。
另外,我正在使用具有正确引用变量的函数原型。
【问题讨论】: