Click here to Skip to main content
15,899,935 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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;
}


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.
Posted
Updated 30-Apr-17 13:56pm
v2
Comments
George Swan 30-Apr-17 2:03am    
Try renaming the variable 'b' to 'choice' and have another look at your code. It's important to give your variables meaningful names.

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.
 
Share this answer
 
C++
total=c/b;

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.
 
Share this answer
 
v2
Wow, I am so stupid, I don't know how I didn't notice that before, thanks guys.
 
Share this answer
 
Comments
Patrice T 30-Apr-17 20:08pm    
Thank you.
But you shouldn't use an answer for such a message.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900