minus function for calculator

#include <iostream>
#include <conio>

int Sum(int, int);
int minus(int,int);


void main()
{
int userInput, ansTotal, ansNum=0;
bool ans = true;
char ch;

cout << "Input number: ";

while (ans == true)
{

cin >> userInput;
cin >> ch;
ansTotal = minus(ansNum, userInput);
ansNum = ansTotal;
if (ch=='=')
ans = false;

}

cout << ansTotal;

getch();
}

int Sum(int total, int newValue)
{

total = total + newValue;
return total;

}
int minus(int total, int newValue)
{

total = total - newValue;
return total;

}

i'm going to build a calculator which could enter a multiple value and add them together without the limitation of variables.The Sum function could respond properly like"6+6+7+8+20=47"without any problem.But the minus problem seems got problem ,for example 6-5=-11.How to solve it?
You only call the function minus in main, whatever ch is, and never the function Sum. I guess that isnt how it should be?
i forgotten to post another lines of code which is a menu to let me decide which operator function i want .For instance, if i choose 2.which is minus operator function it suppose to peform the calculation of subtraction.As i above mentioned the 6-5 is equal to -11 how could it be ?
Well, i guess this is what happens:

Ans=0
Input=6
Input="-"
Ans=0-6=-6
Input=5
Ans=-6-5=-11
Last edited on
I think if you declare ch as char ch='+' it will work
i don't get it ,why should i change the ch='+',the main problem for me is about what Scorpio had mentioned .Any better idea ?
This is how it works now:
The ansNum is 0. Then the user put in a number into userInput, e.g. 6, and -/+/= into ch, eg "-". As user you dont see it, but at this point the program starts calculating:
-because ch is "-", it calls the function minus
-this functions returns -6: total was 0; 0-6=-6
After that, the loop start again, and again the user put in a number into userInput, eg 5, and -/+/= into ch, eg "=". The program starts calculating again, and in some way, it calls again the function minus (I need the whole code, incl. menu of operator functions, to understand wy: ch is now "="):
total=total-newValue
total=-6-5=-11

I hope this will help, if not, upload the whole code including menu, so i can take a look at that

Ps. I was wrong, declaring ch as char ch='+' wont solve te problem, since ch would be changed into "-" before it's used
#include <iostream>
#include <conio>

int Sum(int, int);
int minus(int,int);



int main()
{
int userInput, ansTotal, ansNum=0,choice;
bool ans = true;
char ch;
cout << "==================MENU===============\n";
cout << "1: Addtion of numbers\n";
cout << "2: Subtraction of numbers\n";
cout << "Choice: ";
cin >> choice;
cout << "=====================================\n";

switch (choice)

{
case 1:

cout <<"Input the numbers and the '+'&'=' operator to peform addition"<<endl;
while (ans == true)
{

cin >> userInput;
cin >> ch;
ansTotal = Sum(ansNum, userInput);
ansNum = ansTotal;
if (ch=='='){
ans = false;
}
}

cout << ansTotal;
break;

case 2:

cout <<"Input the numbers and the '-'&'=' operator to peform subtraction "<<endl;

while (ans == true)
{
cin >> userInput;
cin >> ch;
ansTotal = minus(ansNum, userInput);
ansNum = ansTotal;
if (ch=='='){
ans = false;
}
}
cout << ansTotal;


}


getch();
}
int Sum(int total, int newValue)
{

total = total + newValue;
return total;

}

int minus(int total, int newValue)
{

total = total - newValue;
return total;

}

if i cannot solve this problem which mean that i cannot the implement the same thing with other operator like division ,multiplication
The problem with with the subtraction is with the first number: you dont want to subtract but to add that:
6 - 5
is actually
+6 - 5
There are many ways to solve that problem. The most easiest is in my opinion to create another bool variable that tells or it is the first number in the substraction or not, and if it is, make the program calls the function Sum in stead of minus.

Here is the new code (i have changed some other things because my compiler gave some errors):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>


using namespace std;

int Sum(int, int);
int Minus(int,int);
bool firstsubtraction=true;                 //Creating the variable


int main()
{
int userInput, ansTotal, ansNum=0,choice;
bool ans = true;
char ch;
cout << "==================MENU===============\n";
cout << "1: Addtion of numbers\n";
cout << "2: Subtraction of numbers\n";
cout << "Choice: ";
cin >> choice;
cout << "=====================================\n";

switch (choice)
{
case 1:
cout <<"Input the numbers and the '+'&'=' operator to peform addition"<<endl;
while (ans == true)
{
cin >> userInput;
cin >> ch;
ansTotal = Sum(ansNum, userInput);
ansNum = ansTotal;
if (ch=='='){
ans = false;
}
}
cout << ansTotal;
break;

case 2:
cout <<"Input the numbers and the '-'&'=' operator to peform subtraction "<<endl;
while (ans == true)
{
cin >> userInput;
cin >> ch;
if (firstsubtraction)                        //Look or it is the first substraction
{
ansTotal = Sum(ansNum, userInput);           //Call the function Sum instead of minus
firstsubtraction=false;                      //Set the variable to false, otherwise the program would keep adding
}
else                                         //From here on the program works just as normal:
                                             //It substract all the numbers
{
ansTotal = Minus(ansNum, userInput);
}
ansNum = ansTotal;
if (ch=='='){
ans = false;
}
}

cout << ansTotal;
}
return 0;
}

int Sum(int total, int newValue)
{

total = total + newValue;
return total;

}

int Minus(int total, int newValue)
{

total = total - newValue;
return total;

}


As you may notice, i have removed #include <conio> and getch();, the program still works fine

Good luck with the rest of the program, the menu and stuff looks nice
Last edited on
Thanks very much it solve the problem like a charm.In programming field ,there is so much interactivity things to construct a body of a program.I never expected to use the "firstsubtraction = false"to declare a variable and seal the first number of the ansNum away from the "-" operator
Topic archived. No new replies allowed.