c++部门.看似简单的事情让我抓狂,请给我建议

c++ Division . seemingly simple thing driving me crazy, advice please

本文关键字:c++ 简单的事 部门      更新时间:2023-10-16

好吧,我已经编程大约一周了,我从c++开始。我正在写一个程序,它是一种算术训练器,你输入你想要的方程数量,你输入随机数生成器的极限,你指定你想要什么样的方程(/*-+),然后程序使用for循环,在var中遍历并生成方程及其答案,然后根据该var检查用户输入,如果他们匹配另一个正在计数的var,则正确答案递增。在最后一个方程式之后,程序告诉用户他们从多少方程式中获得了多少正确答案,通过将正确答案的数量除以问题的数量,然后将该值乘以100u,应该可以获得该用户算术会话的准确率百分比。问题是c++不断地给我返回一个该死的0值,我一辈子都无法弄清楚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%。

任何人都知道为什么会这样,以及如何在我之后得到结果。

rightsamount都是int,因此当您对值进行除法时,将被求底,例如,如果您进行5/2,则答案将是2而不是2.5。要解决此问题,您需要将其中一个变量强制转换为float,如下所示:(float(rights)/amount) * 100

当两个int数被除时,即使是临时变量,结果也将是int。所以你可以使任何一个变量浮动或加倍或强制转换。

您只需要转换一种数据类型,因为另一种类型将隐式升级。

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

或者,如果金额变量不影响代码的任何其他部分,则可以默认使其浮动。

此外,您还可以选择静态投射:

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