我试图制作一个程序,要求用户输入问题和答案,但程序循环不正确
im trying to make a program that ask the user to enter a question and answer, but the program does not loop right
我正在进行一个项目,要求用户创建一个问答。然后程序将询问用户是否要添加更多问题。由于某种原因,我的程序没有循环。这是我的密码。如果有什么建议,请告诉我。谢谢
#include <iostream>
#include <string>
#include<fstream>
using namespace std;
int main()
{
string exam_Name;
string questions, DMV, answer;
fstream examfile;
string another_question,no,yes;
examfile.open("exam.txt");
// ask the user to create a question
while (another_question != "no");
{
cout << "create a question. " << endl;
getline(cin, questions);
cout << "enter the answer" << endl;
getline(cin, answer);
// program will now ask the user to create another question
cout << "would you like to add another question, yes or no ?" << endl;
getline(cin, another_question);
}
//display question and answer on the document
examfile << questions << endl;
examfile << answer;
return 0;
system("pause");
}
编辑我添加了整个代码。
应删除while
语句之后的;
。也就是说,由于
while (another_question != "no");
是无限循环并且永远不会结束,我们应该重写这一行如下:
while (another_question != "no")
我想显示的所有问题
将examfile <<
放在while{...}
部分,您可以显示所有问题:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
string questions, answer;
fstream examfile;
string another_question;
examfile.open("exam.txt");
// ask the user to create a question
while (another_question != "no");
{
cout << "create a question. " << endl;
getline(cin, questions);
cout << "enter the answer" << endl;
getline(cin, answer);
//display question and answer on the document
examfile << questions << endl;
examfile << answer << endl;
// program will now ask the user to create another question
cout << "would you like to add another question, yes or no ?" << endl;
getline(cin, another_question);
}
return 0;
system("pause");
}
尝试将问题和答案连接到单个字符串中的方式将不起作用,它们将被调用getline((覆盖。
while (another_question != "no");
上面的行被认为是不好的做法,您应该使用更合适的类型作为循环条件,并去掉分号。
下面是一个更好的代码示例,它将产生您想要的结果。
// You want to append any changes to the file, for example
// in the case of re-using the program.
File.open( "exam.txt", std::ios::app );
while( AnotherQuestion ) {
printf( "Please enter a question:n" );
std::getline( std::cin, Buffer );
File << Buffer << std::endl;
printf( "Please enter an answer:n" );
std::getline( std::cin, Buffer );
File << Buffer << std::endl;
printf( "Would you like to add another question? (Yes/No)n" );
std::getline( std::cin, Buffer );
// You want to be able to receive input regardless of case.
std::transform( Buffer.begin( ), Buffer.end( ), Buffer.begin( ), ::tolower );
AnotherQuestion = Buffer.find( "yes" ) != std::string::npos;
}
您可以采取的另一种方法是创建一个包含问题和答案的类,然后将输入数据存储到std::向量中,该向量将在最后写入文件。只是想一想:-(
相关文章:
- C++程序问题:抛出'std::invalid_argument'实例后终止调用
- 测试外壳,插入和快速的程序问题
- Mongodb c ++驱动程序问题
- 输出中的特定递归反向程序问题
- C++ std::endl 的多线程程序 I/O 问题
- 我如何利用核心文件在C/C 中找到应用程序问题
- 套接字 TCP 服务器程序问题
- C++,结构数据库程序问题
- visual c++程序问题
- C++多个程序问题(rand、转换、崩溃)
- 非常基本的C++程序问题 - 二进制表达式的操作数无效
- C++练习车辆程序问题 - 初学者编程
- 关于堆和堆栈应用程序问题
- Boost asio:io服务在销毁步骤中被阻止了更多的处理程序问题
- 使用链接linux编译GLFW应用程序问题
- C++计算器程序.问题
- 基于WIndows - manifest的ETW提供程序问题
- 求1到100之间质数的程序问题
- 文件加载程序问题
- mfcc++托盘应用程序问题