0

Hello im stuck at SUBTRACTION AND DIVITION AND I CANT FIGURE OUT WHAT CODE to use because when I try to subtract 10 i inputed it then it will loop since the while condition is not meet which it needs to be negative to terminate the loop and i inputed 2 for the second number then loop again then i putted -number which lead to terminate loop and subtract all the number but the result is -12 its always wrong in every number cant figure out why Please help Also with divition, only my addition is working havent started the divition cuz i cant figure out how

#include <iostream> using namespace std; int amt2, total; double subNumbers(); double amt=1; double number=0; int main() { int chc=0; int amt = 0; int amt2 = 1; cout << "Welcome again User!\n"; cout << "______________________________________________________________\n" << endl; cout << "Mathematical Operations(Improved):\n\n"; cout << "\t[1]-Addition" << endl; cout << "\t[2]-Subtraction" << endl; cout << "\t[3]-Multiplication" << endl; cout << "\t[4]-Division\n" << endl; cout << "______________________________________________________________\n" << endl; cout << "Type the number corresponding to your chosen operation: "; cin >> chc; ``` switch (chc) { case 1: ``` system ("cls"); cout << "\n\n\tOperation chosen: Addition"; cout << "\n______________________________________________________________" << endl; cout << "\n\nInput positive numbers to use the operation and input a negative number to end the operation.\n\n"; cout << "Enter your number: ";` cin >> number; while (number >= 0) { // add all positive numbers amt += number; // take input again if the number is positive cout << "Enter another number: "; cin >> number; } // display the sum cout << "\nThe sum of all the numbers is: " << amt << endl; break; ``` case 2: system ("cls"); cout << "\n\n\tOperation chosen: Subtraction"; cout << "\n______________________________________________________________" << endl; cout << "\n\nInput positive numbers to use the operation and input a negative number to end the operation.\n\n"; do{ cout << "Enter your number: "; cin >> number; amt=number-number ; }while (number >= 0);// subtract all positive numbers // display the difference cout << "\nThe difference of all the numbers is: "<<amt; return 0; }} ``` enter code here 
18
  • 1
    What value do you think amt has when you do amt=number-number ;? Commented Mar 18, 2022 at 7:52
  • i though it would go amt=first number i inputted subtract the second number i inputted itsbecause my teacher wanted to do a infinite subtrating calcu that will only stop when puting negative number and will subtract all the inputed number Commented Mar 18, 2022 at 7:59
  • I also eddited the question to give further explanation and fixed my grammar Commented Mar 18, 2022 at 8:01
  • 1
    What do you think the value of number - number is? What do you get if you subtract a number from itself? Commented Mar 18, 2022 at 8:02
  • @SweetEhanag i though it would go amt=first number i inputted subtract the second number i inputted -- You shouldn't guess what things will do. The number is a single variable -- that's all it is, and that's all it will be. So when you do number-number, you are substracting the value in number from the value in number, giving you a result of 0. Commented Mar 18, 2022 at 8:08

1 Answer 1

1

You are subtracting number from number:

amt = number - number; // Which is always 0 

So that's why amt == 0 always.

So just change your loop to this:

while (true) { cout << "Enter your number: "; cin >> number; if (number < 0) break; if (amt == 0) amt = number; else if (number >= 0) amt -= number; } 

What this does is that if amt == 0, then set amt to number. I have done this because as the default value of amt is 0 (due to int amt = 0;), when amt == 0, then we can assume that the user has entered the first number, and thus we can set amt to number. And then we can use -= operator, which basically means:

amt = amt - number; 

But before all this, using if (number < 0) break; we can check if the user has entered a negative number, and if the user has entered a negative number, then the break keyword will break out of the while loop.

Sign up to request clarification or add additional context in comments.

3 Comments

Hello would you mind explain it more elaborately?? sorry im new
I have tried to explain the solution in my edit. Hope it's clear :).
Uhm hello again can you make a division code???

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.