+ 3

help me in c++ calculator

so my problem is I have to make a simple program that will take the users desired arithmetic operation to be used and ask the user how many input he/she want to enter, So the first three operation is quite a bit easy but the division make me sick. can anyone help me? this is my codes by the way #include <iostream> using namespace std; int main() { int sum,diff,prod = 1,range,StartNum; char UseOperation; double num = 1,quo =1; cout << "Enter Operation to be used : " << endl; cout << "(a)Addition" << endl << "(b)Subtraction" << endl << "(c)Multiplication" << endl << "(d)Division" << endl; cin >> UseOperation; cout << "Enter how many number you want to input : " << endl; cin >> range; if (UseOperation == 'a'){ for (StartNum = 0;StartNum < range;range --){ cout << "Enter Number : " << endl; cin >> num; num += num; } sum = num; cout << "The sum of the inputed number is " << sum << endl; } else if (UseOperation == 'b'){ for (StartNum = 0;StartNum < range;range --){ cout << "Enter Number : " << endl; cin >> num; num -= num; } diff = num; cout << "The difference of the inputed number is " << diff << endl; } else if (UseOperation == 'c'){ for (StartNum = 0;StartNum < range;range --){ cout << "Enter Number : " << endl; cin >> num; prod *= num; } prod = prod; cout << "The product of the inputed number is " << prod << endl; } else if (UseOperation == 'd'){ for (StartNum = 0;StartNum < range;range --){ cout << "Enter Number : " << endl; cin >> num; num /= num; } quo = num; cout << "The quotient of the inputed number is " << quo << endl; } else{ cout << "Error" << endl; } return 0; system("pause"); }

4th Sep 2019, 4:26 PM
Allan Ramos
Allan Ramos - avatar
7 Antworten
+ 4
write it on playground to make it easier to read
4th Sep 2019, 7:22 PM
ABADA S
ABADA S - avatar
+ 4
In case of addition, subtraction and division you have written statements incorrectly. They should be like: 1) sum += num; 2) diff -= num; 3) quo /= num; Hope it will solve your issue.
4th Sep 2019, 7:27 PM
blACk sh4d0w
blACk sh4d0w - avatar
+ 4
Just in case of division, the quotient will get to zero after three or more inputs. Let's say at first iteration use enetered number 20. Second iteration 2 20 / 2 = 10 Third iteration 2 10 / 2 = 5 Fourth iteration 3 5 / 3 = 1 Fifth iteration 4 1 / 4 = 0 So at last you'll get either 0 or 1. Here notice another thing, at first iteration we are not dividing. We start dividing by 2nd iteration so your code should be like this for division only: cin >> num; if(StartNum == 0) quo = num; else quo /= num; Then print it. It should work now.
5th Sep 2019, 6:08 AM
blACk sh4d0w
blACk sh4d0w - avatar
+ 2
@nAutAxH AhmAd I have tried what you have suggested it word on addition,subtraction and multiplication but not in division.. maybe Im having logical errors on my codes.. thanks by the way
5th Sep 2019, 3:21 AM
Allan Ramos
Allan Ramos - avatar
+ 2
@nAutAxH AhmAd it works like magic. thank you very much for the help. I appreciate it so much :)
5th Sep 2019, 6:28 AM
Allan Ramos
Allan Ramos - avatar
+ 1
@ABADA S I have tried making it in the playground but I always get compile error and I dont know why.
5th Sep 2019, 3:22 AM
Allan Ramos
Allan Ramos - avatar
0
Me too
5th Sep 2019, 11:30 AM
Umar Dayyabu