I'm trying to write a program which works like a calculator, keeping the running total of an operation, with the number of terms not defined by the user. Here's what I've got so far:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
int add(num1,num2) // these vars are init'd earlier in the program flow
{
cout << "Enter an operation";
while (1)
{
scanf("%d%c%d",&num,&op,&num2);
if (strcmp(&op,'+') == 0)
{
num2 += num1 // num2 becomes the sum of num1 and num2
cout << num2
}
if (strcmp(&op,'–') == 0)
{
// code for subtraction, similar to above
}
}
}
I know that this code is incomplete, in that it doesn't loop back to scanning for another "+" and another number; I'm not sure which condition to run the loop under. Can anyone suggest how this can be done?
EDIT: added conditional to better illustrate point
So just to clarify, you need a program that will add numbers and keep a sum? For example if I entered 1 12 4 5 it would output 22? Also how do you want the program to know when to stop? When the user inputs 0?
Actually, I've got another condition (which I omitted here because it's irrelevant) where the program stops when the user types exit (I elevate it to uppercase, then use strcmp().
And what you're describing is approximately what I want, but I wanted the user to have the option to use a different sign (+,–,*,/) depending on what operation would be used (I'll handle that with strcmp() for the &op variable and a conditional program flow).