2012-03-19 65 views
4

好吧,我已經編程了大約一個星期了,我開始使用C++。我正在編寫一個程序,它是一種算術訓練器,輸入您想要的方程式數量,輸入您對隨機數生成器的限制,指定想要的方程式(/ * - +),然後該程序使用for循環並通過並在var中生成等式和它們的答案,然後根據此var檢查用戶輸入,並且如果它們匹配另一個正在計數正確答案的var,則遞增。在最後一個方程之後,程序會告訴用戶他們有多少方案可以從多少個方程中獲得正確答案,並且通過將正確答案的數量除以問題的數量,然後將該值乘以100得到該用戶算術會話的準確性百分比。問題是C++一直在向我返回一個friggin 0值,我不能爲我的生活找出爲什麼在世界上C++正在這樣做。C++分部。看起來很簡單的事情讓我瘋狂,建議請

整個程序:

#include <iostream> 
#include <string> 
#include <ctime> 
#include <cstdlib> 
using namespace std; 
void menu(void); 
class session{ 
public: 
session(){ 
      create_session(); 
     } 
     void create_session(void){ 
     amount = 0; 
     range_limit = 0; 
     rights = 0; 
     answer = 0; 
     input = 0; 
     type = ""; 
     while(amount == 0){ 
      cout << "\nHow many equations do you want?: "; cin >> amount; 
      if(amount < 1){ 
       cout << "\nAmount is too low!"; 
       amount = 0; 
      } 
     } 
     while(range_limit == 0){ 
      cout << "Enter the number range limit: "; cin >> range_limit; 
      if(range_limit < 1){ 
       cout << "\nRange limit too low!"; 
       range_limit = 0; 
      } 
     } 
     while(type == ""){ 
      cout << "What equation type do you want?: "; cin >> type; 
      int strlen = type.size(); 
      if(strlen < 1){ 
       cout << "Invalid type input!"; 
       type = ""; 
      } 
     } 
     if(type == "+"){ 
      for(int i=0;i<amount;i++){ 
       int a = random(); 
       int b = random(); 
       answer = a + b; 
       cout << "\n" << a << " + " << b << " = "; cin >> input; 
       if(answer == input){ 
        rights++; 
       } 
      } 
     } 
cout << "\nYou got " << rights << " answers right out of " << amount <<   " equations." << endl; 
     cout << "Accuracy percentage: " << getAccuracy() << "%" << endl; 
     int post_menu=0; 
     while(post_menu == 0){ 
      cout << "Enter 1 to create another session or 2 to return to the menu: "; 
      cin >> post_menu; 
      if(post_menu == 1){ 
       create_session(); 
      }else if(post_menu == 2){ 
       menu(); 
      }else{ 
       cout << "Invalid input: "; 
       post_menu = 0; 
      } 
     } 
    } 
    float getAccuracy(){ 
      float x = (rights/amount)*100; 
      return x; 
    } 
    int random(){ 
     int x = 1+(rand()%range_limit); 
     return x; 
    } 
    void set_amount(int a){ 
     amount = a; 
    } 
    void set_range_limit(int r){ 
     range_limit = r; 
    } 
    void set_rights(int R){ 
     rights = R; 
    } 
    void set_answer(int a){ 
     answer = a; 
    } 
    void set_input(int i){ 
     input = i; 
    } 
    void set_type(string t){ 
     type = t; 
    } 
private: 
    int amount; 
    int accuracy; 
    int range_limit; 
    int rights; 
    int answer; 
    int input; 
    string type; 

}; 
int main(){ 
cout << "=== WELCOME TO ARITH! === \n=========================\n"; 
menu(); 
return 0; 
} 

void menu(void){ 
//Set the seed for random number gen. 
srand(time(0)); 

//Set var for getting menu input, then get the menu input.. 
int menu_input; 
cout << "\n[1]Create a Session. [2]Exit Arith. \nWhat would you like to do?: "; 
cin >> menu_input; 

//Now we check what the user wants and act accordingly.. 
if(menu_input > 2){ 
    cout << "error"; 
    menu_input=0; 
}else if(menu_input == 1){ 
    session start; 
}else if(menu_input == 2){ 
    cout << "\nExiting Arith!"; 
}else{ 
    cout << "error"; 
    menu_input=0; 
} 
} 

陰陽部分:

float getAccuracy(){ 
      float x = (rights/amount)*100; 
      return x; 

一些如何將程序返回0%。

任何人都知道這是爲什麼,以及如何得到結果後im。

+8

整數除法... – Mysticial 2012-03-19 07:07:52

+1

+1一份詳細的問題 – jcoder 2012-03-19 07:22:17

回答

4

rightsamount都是int,所以當你把值地板,例如,如果你做5/2答案是2而不是2.5。要解決此問題,您需要將其中一個變量轉換爲float,如下所示:(float(rights)/amount) * 100

+3

整數除法不給你結果的地板;相反,它趨於零。例如,「(-1)/ 2 == 0」,但是「floor(( - 1.0)/2.0)== -1.0」。 – 2012-03-19 07:13:30

+0

THANKYOU如此多人。 – user1260830 2012-03-19 07:20:29

+0

負數的除法結果依賴於編譯器。標準要求(C++ 2003的第5.6.4節)不超過'(a/b)* b + a%b == a',其餘部分可以是正數或負數,因此除法結果可以舍入爲零或可能會被淹沒。 – n0rd 2012-03-19 07:28:47

1

當兩個整數分開時,即使臨時變量,結果也是int。所以你可以使任何變量float或double或者施放它。

您只需要轉換一種數據類型,因爲另一種數據類型將被隱式提升。

float x = ((double)rights/amount)*100; 

或者你可以讓你的金額變量默認浮動,如果它不影響你的代碼的任何其他部分。

你也可以選擇靜態澆鑄:

float x = (static_cast<double>(rights)/amount)*100;