C ++ Division. seemingly simple thing driving me crazy advice please

Ok, I programmed for about a week, I started with C ++. I am writing a program that is a kind of arithmetic trainer, you enter the number of equations you want, you enter your limit for the random number generator, you specify which equations you want (/ * - +), then the program uses the for loop and goes through and generates equations and their answers in var, and then the user input is checked for this var, and if they match another var, which counts the correct answers, it increases. After the last equation, the program tells the user how many of them came out of the number of equations, and dividing the number of correct answers by the number of questions, and then multiplying this value by 100 u, we get the percentage of accuracy for this arithmetic user session. The problem is that C ++ keeps returning the value friggin 0 to me, and for my life I can’t figure out why C ++ does this in the world.

full program:

#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; } } 

The hard part:

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

how the program returns 0%.

Does anyone know why this is so and how to get the result im after.

+4
source share
2 answers

rights and amount both equal to int , so when you divide the value, it overlaps, for example, if you do 5/2 , the answer will be 2 instead of 2.5 . To solve this problem, you need to pass one of the variables to float as follows: (float(rights)/amount) * 100 .

+4
source

when two int numbers are divisible, the result will also be int, even if a temporary variable. so you can make any variable float or double or throw it.

You need to convert only one data type, because the other will be used implicitly.

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

or you can make your default volume of the float variable if it does not affect any other part of your code.

You also have the option of a static act:

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

Source: https://habr.com/ru/post/1402179/


All Articles