这个C ++程序出了什么问题?

时间:2015-04-03 15:58:54

标签: c++ file-handling

我最近使用文件处理在C ++中制作简单的测验游戏。到目前为止,我写了以下代码。

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{

    int choice;
    cout<<"-------------Welcome to C++ quiz game-------------\n";
    cout<<"Enter your choice\n";
    cout<<"1. Easy\n";
    cout<<"2. Medium\n";
    cout<<"3. Hard\n";
    cin>>choice;


    while((choice!=1) && (choice!=2) && (choice!=3))
    {
        cout<<"Enter choice(1 or 2 or 3) only\n";
        cin>>choice;
    }


    if(choice==1)
    {
        ofstream fout("easy.txt");
        string s[15];
        s[0]="Which is the smallest individual unit in a C++ program?\n";
        s[1]="Which of the following is the C++ style comment?";
        s[2]="Who is known as father of C++?\n";
        s[3]="If default arguments are provided to a constructor function then it becomes what?\n";
        s[4]="In C++ arguments by default are passed by what?\n";
        s[5]="By default class members are of which access specifier in C++?\n";
        s[6]="gets() function prototype is available in which header?\n";
        s[7]="The operator ?: is called?\n";
        s[8]="atoi() function prototype is available in which header?\n";
        s[9]="The constans defined using enum keyword are called?\n";
        s[10]="Which of the following keyword forces the next iteration of the loop to take place?\n";
        s[11]="In C++ the statements are enclosed within what?\n";
        s[12]="What happens if you attempt to modify string literal?\n";
        s[13]="y=x=2; in C++ will result in?\n";
        s[14]="Which is the statement terminator in C++?\n";
        for(int i=0;i<15;i++)
            fout<<s[i]<<'\n';
        fout.close();
    }


    else if(choice==2)
    {
        ofstream fout("medium.txt");
        string s[15];
        s[0]="Which keyword is used to retain value of local variables?\n";
        s[1]="Which keyword can be applied to local variabes only?\n";
        s[2]="Which keyword is used to define a new name for existing type?\n";
        s[3]="What provides value for a variable?\n";
        s[4]="C++ is which type of programming language?\n";
        s[5]="Which operator is used to deallocate dynamically allocated memory in C++?\n";
        s[6]="Is it true that = operator has same precedence as conditional operator in C++?\n";
        s[7]="Which operator can't be overloaded in C++?\n";
        s[8]="Which feature is used to achieve compile time polymorphism in C++?\n";
        s[9]="What is wrong with the following statement float s_interest (float principal, int rate=0.25, int time);?\n";
        s[10]="When a member function of class calls another member function then it is?\n";
        s[11]="Which operator must be used when member function is defined outside the class?\n";
        s[12]="Information is made sharable through?\n";
        s[13]="Which keyword is request to the compiler?\n";
        s[14]="Which is used to reduce/eliminate the function call overhead in C++?\n";
        for(int i=0;i<15;i++)
            fout<<s[i]<<'\n';
        fout.close();
    }


    else if(choice==3)
    {
        ofstream fout("hard.txt");
        string s[15];
        s[0]="In which year C++ is invented?\n";
        s[1]="How many types of inheritance is there in C++?\n";
        s[2]="Which keyword is used to create generic function & classes in C++?\n";
        s[3]="Which type of exception is thrown when new operator fails to allocate memory?\n";
        s[4]="When copy constructor is called?\n";
        s[5]="Which of the following regarding constructor function is false?\n";
        s[6]="Which of the following statements regarding constructor is false?\n";
        s[7]="Which is the latest C++ standard recently running?\n";
        s[8]="The binding of a function call at runtime is?\n";
        s[9]="How many keywords are there currently in C++?\n";
        s[10]="The process of giving special meaning to an operator is?\n";
        s[11]="Which operator should be overloaded to perform boundary checks on array?\n";
        s[12]="Which of the following operators cannot be overloaded?\n";
        s[13]="The symbol **\n";
        s[14]="A unary operator when overloaded takes?\n";
        for(int i=0;i<15;i++)
            fout<<s[i]<<'\n';
        fout.close();
    }

    return 0;
}

第二档:

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
string answer;
void validate();
int main()
{
    int choice;
    int points=0;
    string s[15];
    string easy[15]={"c","a","b","a","b","c","d","c","a","b","b","c","c","c","a"};
    cout<<"-------------Welcome to C++ quiz game-------------\n";
    cout<<"Enter your choice\n";
    cout<<"1. Easy\n";
    cout<<"2. Medium\n";
    cout<<"3. Hard\n";
    cin>>choice;


    while((choice!=1) && (choice!=2) && (choice!=3))
    {
        cout<<"Enter choice(1 or 2 or 3) only\n";
        cin>>choice;
    }


    if(choice==1)
    {
        ifstream fin("easy.txt");
        if(!fin)
        {       
            cout<<"Cannot open file\n";
            exit(1);
        }

        cout<<"The first question is\n";
        getline(fin,s[0]);
        cout<<s[0]<<'\n';
        cout<<"Your options are\n";
        cout<<"a. Keyword\tb.Identifier\n";
        cout<<"c. Token\td.None of the above\n";
        cout<<"Select your answer: ";
        cin>>answer;

        while((answer!="a") && (answer!="b") && (answer!="c") && (answer!="d"))
        {
            cout<<"Select answer(a or b or c or d) only\n";
            cin>>answer;
        }

        if(answer==easy[0])
        {
            cout<<"Congratulations your answer is right\n";
            points+=5;
            cout<<"Your points are: "<<points<<'\n';
        }
        else
        {
            cout<<"Sorry Incorrect answer. Have a good luck next time\n";
            cout<<"Quiz has been Finished\n";
            exit(0);
        }

        cout<<"The second question is\n";
        **getline(fin,s[1]);
        cout<<s[1]<<'\n';**
        cout<<"Your options are\n";
        cout<<"a. //\tb./*..*/\n";
        cout<<"c. _\td.None of the above\n";
        cout<<"Select your answer: ";
        cin>>answer;

        validate(); 

        if(answer==easy[1])
        {
            cout<<"Congratulations your answer is right\n";
            points+=5;
            cout<<"Your points are: "<<points<<'\n';
        }
        else
        {
            cout<<"Sorry Incorrect answer. Have a good luck next time\n";
            cout<<"Quiz has been Finished\n";
            exit(0);
        }

    }
}
void validate()
{
    while((answer!="a") && (answer!="b") && (answer!="c") && (answer!="d"))
    {
            cout<<"Select answer(a or b or c or d) only\n";
            cin>>answer;
    }
}

但是当我根据第一个文件中选择的游戏级别运行第二个程序文件时。这是我得到的结果。 this

为什么第二个问题没有打印?我错过了什么?请帮我。我的意思是为什么问题&#34;以下哪个是C ++风格的注释?&#34; 没有打印?我的代码有什么问题。我认为似乎跳过了在第二个文件中显示为Bold字体的语句。

我们非常感谢您的帮助。

谢谢

1 个答案:

答案 0 :(得分:1)

几个问题:

  1. "easy.txt"中的问题集的第二个问题中缺少换行符。

  2. 您正在为输出引入不必要的换行符。 "easy.txt"的第二行是空白的。当您使用getline阅读文本时,您将收到一个空行。

  3. 有两种方法可以解决问题:

    选项1

    更改行

    for(int i=0;i<15;i++)
        fout<<s[i]<<'\n';
    

    for(int i=0;i<15;i++)
        fout<<s[i];
    

    选项2

    从问题中删除换行符。