2015-04-03 61 views
0

我最近使用文件处理在C++中进行简单的问答游戏。到目前为止,我写了以下代码。这个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

为什么第二个问题没有打印?我错过了什么?请帮帮我。我的意思是为什么问题“以下哪项是C++风格评论?”没有打印?我的代码有什么问题。在第二个文件中显示为Bold字体的语句似乎被忽略,我认为。

我们将非常感谢您的帮助。

感谢

+0

基本**技术**问题是您的文本文件是双倍行距。在编辑器中查看它。每个原始字符串的末尾都有一个换行符,并在将其输出到文本文件时添加换行符。 – 2015-04-03 16:09:51

+0

注意:至少对于当前的功能,您不需要这些阵列。 – 2015-04-03 16:11:27

+0

@ Cheersandhth.-Alf:我已经删除了仍然没有获得所需输出的空格。为什么? – Destructor 2015-04-03 16:13:14

回答

1

几个问题:

  1. 你必须在一套去"easy.txt"问题的第二个问题缺少换行符。

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

有两种方法来解决这个问题:

选项1

更改线路

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

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

选项2

从问题中删除换行符。

+0

@R Sahu:优秀。它非常感谢你。 – Destructor 2015-04-03 16:15:55

+0

@meet,不客气。 – 2015-04-03 16:16:57