Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 我试图制作一个程序,要求用户输入一个问题和答案,但程序没有正确循环_C++_File_Loops_While Loop - Fatal编程技术网

C++ 我试图制作一个程序,要求用户输入一个问题和答案,但程序没有正确循环

C++ 我试图制作一个程序,要求用户输入一个问题和答案,但程序没有正确循环,c++,file,loops,while-loop,C++,File,Loops,While Loop,我正在做一个项目,我要求用户创建一个问题和答案。然后程序将询问用户是否需要添加更多问题。由于某种原因,我的程序没有循环。这是我的密码。如果有任何建议,请告诉我。谢谢 #include <iostream> #include <string> #include<fstream> using namespace std; int main() { string exam_Name; string questions, DMV, answer;

我正在做一个项目,我要求用户创建一个问题和答案。然后程序将询问用户是否需要添加更多问题。由于某种原因,我的程序没有循环。这是我的密码。如果有任何建议,请告诉我。谢谢

#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");
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
字符串名称;
字符串问题,DMV,答案;
fstream-examfile;
再串一个问题,不,是;
examfile.open(“exam.txt”);
//要求用户创建一个问题
而(另一个问题!=“否”);
{

我添加了整个代码


紧跟在
之后的while
语句应该被删除。也就是说,因为

while (another_question != "no");
是无限循环且永不结束,我们应将这一行重写如下:

while (another_question != "no")

我想展示所有的问题


examfile放入试图将问题和答案连接到单个字符串中的方式将不起作用,它们将通过调用getline()被覆盖

上面的这一行被认为是不好的做法,您应该使用更合适的类型作为循环条件,并去掉分号

下面是一个更好的代码示例,它将产生您想要的结果

    // 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;
    }
//例如,您希望将任何更改附加到文件中
//在重复使用程序的情况下。
打开(“exam.txt”,std::ios::app);
while(另一个问题){
printf(“请输入问题:\n”);
std::getline(std::cin,Buffer);

文件什么是“不正确循环”是什么意思?您输入到程序的实际输入是什么,输出是什么?在计算“另一个问题”时,请使用调试器检查其内容。我打赌它不是您认为的内容。您熟悉换行符吗?删除字符串中的分号
,同时
我希望我的程序要求用户输入r问题和答案。此外,这些问题和答案将保存在文本文件中。当我运行我的程序时,它仅显示最近的问题。我希望在用户输入这些问题时显示所有问题。感谢您的建议!
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;
    }