2016-10-11 6 views
1

だから、私はこれに新しいので、私と一緒に裸をしてください。私は単純なトリビアゲームに取り組んでいます。テストのために、この小さなコードを作成して、正しく機能していることを確認しました。トリビアゲームループとラインの問題を取得

// Trivia game for one player. Player will choose a category and program 
// will ask you questions within set category. Correct answers are 
// worth 5 points and incorrect answers are worth only 2.5 points. 

#include <iostream> 
#include <fstream> 
#include <string> 

using namespace std; 

int main() 
{ 
    float score = 0; 
    string name, question, ans1, ans2, ans3, ans4; 
    char ans, correct; 

    ifstream history; 
    history.open("History.txt"); 

    while (!history.eof()) 
    { 
     getline(history, question); 
     cout << question << '\n'; 
     getline(history, ans1); 
     cout << ans1 << '\n'; 
     getline(history, ans2); 
     cout << ans2 << '\n'; 
     getline(history, ans3); 
     cout << ans3 << '\n'; 
     getline(history, ans4); 
     cout << ans4 << '\n'; 
     history >> correct; 
     cout << "Enter your answer: "; 
     cin >> ans; 

     if (ans = correct) 
     { 
      cout << "You are correct!\n"; 
      cout << "5 Points awarded...\n"; 

      score = score + 5; 
     } 
     else 
     { 
      cout << "Wrong!\n"; 
      cout << "2.5 Points awarded...\n"; 

      score = score + 2.5; 
     } 
    } 
} 

HISTORY.TXTが

What was the first Tudor monarch in England? 
A. Henry V 
B. Edward IV 
C. Henry VII 
D. Edmund 
C 
The Kingdom of Joseon was founded in 1392 in what country? 
A. England 
B. Isreal 
C. China 
D. Korea 
D 
The Spanish Civil War began in what year? 
A. 1936 
B. 1874 
C. 1920 
D. 1948 
A 

だから何が起こっていることは、それが読み込みされ、適切に最初の質問と回答を出力し、その後、次のセットのために、それが唯一のC.に行く....このようになりますBに、そうするように。また、ユーザーからのすべての応答を正しいものとしてカウントしています。私は間違って何をしていますか?

if (ans = correct) 

(それが非ゼロなので)技術的にtrueに評価割り当て、次のとおりです。あなたのif文の条件があるため

答えて

0

それは正しいとすべての答えを数えています。平等のために評価する

if (ans == correct) 

:へ

変更。

+0

ありがとうございました!それは完璧な意味合いがあります。 – Brice

+0

最初の4行を正しく読み込んでいる理由を知りたいのであれば、私は混在した行を読むことになりますか?最初の質問と4つの回答のオプションが完全に出力され、次に次の質問のみがhistory.txtからの正解で3つの回答が出力されます – Brice