好的,我已经编程了大约一个星期,我开始使用c ++。我正在编写一个程序,这是一种算术训练器,你输入你想要的方程式数量,你输入随机数发生器的极限,你指定你想要的方程式(/ * - +) ,然后程序使用for循环并通过并在var中生成方程式及其答案,然后针对此var检查用户输入,如果它们匹配另一个正在计数的正确答案的var,则递增。在最后一个等式之后,程序告诉用户他们从多少个等式得到了多少,并且通过将正确答案的数量除以问题量,然后将该值乘以100 u应该获得该用户算术会话的准确度百分比。问题是c ++不断向我返回一个friggin 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%。
任何人都知道为什么会这样,以及如何获得结果。
答案 0 :(得分:4)
rights
和amount
都是int
,因此当您划分时,该值会被覆盖,例如,如果您执行5/2
,则答案为2
而不是2.5
。要解决此问题,您需要将变量之一投射到float
,如下所示:(float(rights)/amount) * 100
。
答案 1 :(得分:1)
当两个int数被分割时,即使临时变量,结果也将是int。所以你可以使任何变量浮动或加倍或投射它。
您只需要转换一种数据类型,因为另一种数据类型将是隐式类型提升。
float x = ((double)rights/amount)*100;
或者如果它不影响代码的任何其他部分,你可以默认使你的数量变量浮动。
你也可以选择静态演员:
float x = (static_cast<double>(rights)/amount)*100;