我试图制作一个要求用户输入问题和答案的程序,但是该程序无法正确循环

时间:2018-10-26 21:37:21

标签: c++ file loops while-loop

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

#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");
}

2 个答案:

答案 0 :(得分:2)

编辑。我添加了完整的代码。


;语句之后的

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");
}

答案 1 :(得分:1)

您尝试将问题和答案连接到单个字符串中的方法将不起作用,它们将被调用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 :: vector中,然后将其写入文件中。只是需要考虑的一件事:-)