c++部门.看似简单的事情让我抓狂,请给我建议
c++ Division . seemingly simple thing driving me crazy, advice please
好吧,我已经编程大约一周了,我从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%。
任何人都知道为什么会这样,以及如何在我之后得到结果。
rights
和amount
都是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;
相关文章:
- 在c++中用vector填充一个简单的动态数组
- 为什么简单的算术减法在"if"条件下不起作用?
- 如何使用 samtools C API 构建一个简单的主.cpp文件
- 我应该在简单的策略游戏中为各个派系使用类吗 - C++
- 简单的 HTML 验证器
- 无法找到简单的开关大小写枚举错误
- 而循环:简单的除法程序输出零,不明白为什么
- 请求最简单的 OpenMP 目标 GPU 示例
- 查找定义我的 C/C++ 函数/宏的文件比'grep'更简单的方法
- C++20 个模块在 clang (Windows):在最简单的示例中键入信息错误
- 为什么一个简单的程序不能立即启动
- 我如何阅读简单的控制台输出
- 如何在这个简单的最小示例中实现多态性?
- 从简单的 POST 请求中解析异常
- C++ - 在我尝试制作一个简单的计算器时有一个"uninitialized local variable y used"警告
- std::list 可以用于简单的无锁队列吗?
- 一个非常简单的win32套接字代码,但工作错误
- 有没有简单的方法可以将"02:00"转换为分钟,比如 120?
- 在 cpp 中最简单的图形实现
- reinterpret_cast,只读访问,简单的可复制类型,会出什么问题?