2014-01-05 21 views
0

我正在做一個任務,我應該編寫一個程序來測試用戶的數學技能。這裏是我現在的代碼:初學者C++程序中的分數組件

using namespace std; 
void addition() 
{ 
    int Value, Value2, Answer; 
    bool gotAnswer; 

    for (int i=1; i<=10; i++) 
    { 
     Value = 1 + (rand()%10); 
     Value2 = 1 + (rand()%10); 

     gotAnswer = false; 
     cout << Value << " + " << Value2 << " = "; 

     for (int a=1; (a<=3 && gotAnswer==false); a++) 
     { 
      cin >> Answer; 
      if (Answer==(Value+Value2)) 
      { 
       cout << "CORRECT" << endl << endl; 
       gotAnswer = true; 
      } 
      else 
      { 
       cout << "WRONG Try again." << endl; 
       if (a==3) 
       { 
        cout << "You have missed 3 times. The answer is " << Value+Value2 << "." << endl << endl; 
       } 
      } 
     } 
    }  
} 

void substraction() 
{ 
    int Value, Value2, Answer; 
    bool gotAnswer; 

    for (int i=1; i<=10; i++) 
    { 
     Value = 1 + (rand()%10); 
     Value2 = 1 + (rand()%10); 

     gotAnswer = false; 
     cout << Value << " - " << Value2 << " = "; 

     for (int a=1; (a<=3 && gotAnswer==false); a++) 
     { 
      cin >> Answer; 
      if (Answer==(Value-Value2)) 
      { 
       cout << "CORRECT" << endl << endl; 
       gotAnswer = true; 
      } 
      else 
      { 
       cout << "WRONG Try again." << endl; 
       if (a==3) 
       { 
        cout << "You have missed 3 times. The answer is " << Value-Value2 << "." << endl << endl; 
       } 
      } 
     } 
    } 
} 

void Multiplication() 
{ 
    int Value, Value2, Answer; 
    bool gotAnswer; 

    for (int i=1; i<=10; i++) 
    { 
     Value = 1 + (rand()%10); 
     Value2 = 1 + (rand()%10); 

     gotAnswer = false; 
     cout << Value << " x " << Value2 << " = "; 

     for (int a=1; (a<=3 && gotAnswer==false); a++) 
     { 
      cin >> Answer; 

      if (Answer==(Value*Value2)) 
      { 
       cout << "CORRECT" << endl << endl; 
       gotAnswer = true; 
      } 
      else 
      { 
       cout << "WRONG Try again." << endl; 
       if (a==3) 
       { 
        cout << "You have missed 3 times. The answer is " << Value*Value2 << "." << endl << endl; 
       } 
      } 
     } 
    } 
} 

int main() 
{ 
    int number; 

    cout << "Enter the number for the problem type desired:"<<endl; 
    cout << " 1. Addition"<<endl; 
    cout << " 2. Subtraction"<<endl; 
    cout << " 3. Multiplication"<<endl; 

    cin >> number; 

    if (number == 1) 
    { 
     addition(); 
    } 
    else if(number == 2) 
    { 
     substraction(); 
    } 
    else if (number ==3) 
    { 
     Multiplication(); 
    } 
} 

該程序運行正常。但是,應該有一個評分組成部分,用戶在第一次嘗試中得到10分,在第二次嘗試中得到5分,在第三次嘗試/錯誤時得到0分。我不知道如何在10個問題的末尾融合得分組件和顯示。請提示一下?

所有在此先感謝。

回答

2

您應該在每個函數中保留一個得分變量,根據需要添加到得分變量中,然後返回得分變量。

所以這些功能不再是空洞的,它們將是整數。然後您可以在最後得到分數並打印出來。

我不會爲你寫任何代碼,因爲它是作業:P

+0

謝謝你!我嘗試添加評分組件,但沒有返回。在我更改爲int之後,它有用。謝謝! – Miko

+0

我會保持本地分數爲主,並讓函數返回值爲增量的int值。所以你可以改變對addition(),subtraction()和multiplication()的調用來得分+ =加法()等。沒有理由將當前分數傳遞給函數。該功能獨立於以前的結果評估用戶的表現。 – jmucchiello