好吧,我已經編程了大約一個星期了,我開始使用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。
整數除法... – Mysticial 2012-03-19 07:07:52
+1一份詳細的問題 – jcoder 2012-03-19 07:22:17