为什么除法运算符不能在C ++计算器中工作? [英] Why does division operator not work in C++ calculator?

查看:116
本文介绍了为什么除法运算符不能在C ++计算器中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <iostream>

using namespace std;

int main(){
float total=0;
float a=20;
float b=0;
float c=0;
float d=0;
while(a==20){
    cout << "1 Multiplication 2 Division 3 Addition 4 Subtraction: \n";
    cin >> b;
    cout << "Enter two numbers, one at a time: \n";
    cin >> c;
    cout << "\n";
    cin >> d;
    cout << "\n";
        if (b==1){
            total=c*b;
        }
        if (b==2){
            total=c/b;
        }
        if (b==3){
            total=c+b;
        }
        if (b==4){
            total=c-b;
        }
    cout << total;
    cout << "\n";
    cout << "\n";
    }
    return 0;
}





几天前我用c ++编写了这个计算器,但是我对一个我没有的问题感到困惑解释。计算器中的所有其他运算符都工作,除了除法,当我输入10除以2时,我得到5的输出,如预期的那样,但当我输入20除以4或5时,我得到10.我也得到5当我把10分为5分。任何答案?



我尝试过的事情:



我查看了所有代码大约7次,这一切似乎都是正确的。我不知道还有什么办法可以尝试解决它。​​



I wrote this calculator in c++ a few days ago, but I am baffled by a problem that I have no explanation for. All other operators in the calculator work, except the division, when I put in 10 divided by 2, I get an output of 5, as expected, but when I enter 20 divided by 4 or 5, I get 10. I also get 5 when I put in 10 divided by 5. Any answers?

What I have tried:

I've looked over all the code about 7 times, and it all seems to be correct. I don't know what else to do to try and solve it.

推荐答案

首先,尝试给你的变量更好的名字。



您可以使用b作为操作类型,然后使用c和d作为操作数。然而代码使用c和b。
First, try to give your variables better names.

You use b for the type of operation, then c and d for the operands. Yet the code uses c and b.


total=c/b;



重读你的代码仔细, b 包含操作的代码,而不是除数。顺便说一下,你有比分部更多的错误。



当你不明白你的代码在做什么或为什么它做它做的事情时,答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

调试器在这里显示你的代码在做什么你的任务是与它应该做的事情进行比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就接近了一个错误。


Reread your code carefully, b contain the code of the operation, not the divisor. By the way, you have many more errors than the one in division.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


哇,我是如此愚蠢,我不知道我以前怎么没注意到,谢谢你们。
Wow, I am so stupid, I don't know how I didn't notice that before, thanks guys.


这篇关于为什么除法运算符不能在C ++计算器中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆