2015-04-03 34 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