# Trying to make a calculator

 Jonet Harry · July 12, 2015 Hello. I've watched numerous youtube videos about how to make a calculator in c++ but none of them show how to make one the way that I want to. They usually go:"Please enter a number"cin >> blah blah"Please enter another number".This is not the type of calculator that I want to make. I just want a simple one, with no letters. This is what I came up with.#include using namespace std;int value1;int value2;char op;int sum;int numbersEntered;int main(){ cout << "Jonet's Calculator\n\n"; for (int numbersEntered = 0; numbersEntered < 10; numbersEntered++) { cin >> value1; cin >> op; cin >> value2; if (op = '+') sum = value1 + value2; cout << sum << endl;  } }Now, this calculator isn't too bad, but my problem is that I want to take the sum, and add a number to it, so that it goes on continuously until 10 numbers have been added, then it calculates the sum total. However, I have not been able to figure this out. This is as far as I've gotten:cin >> value1; cin >> op; cin >> value2; if (op = '+') sum = value1 + value2; cout << sum; cin >> op; cin >> value1; cout << sum + value1; cin >> op; cin >> value1; cout << sum+value1;Obviously, if you'd run this, you'd hit a dead end quickly. It works fine the first 3 times, but after that, well, I just don't know where to go from here. I know I need a way to store the new sum each time, but not sure how. I thought about using sum+=value1 but when I try to do that, it doesn't even run, I get an error about something not being defined or whatever. So basically, I want this.1+12+24+48+816+925+227, etc...and not1+123+478+412.And also, maybe a way to make it so the user could just type + over and over, so it's like 1+2+3+4+5 and whenever they hit enter, it finally calculates the total. Sorry for all the long questions, but I literally have been up for HOURS just staring at the screen, and have come up with no solution for what seems like such a simple problem, and I am just really tired, so I give up.

## Replies

 RIAS GREMORY · July 13, 2015 do you want to put it in a while loop? Kuroodo Ditory · July 14, 2015 One thing you can do is use a string, and then check the characters in the string.int x = all characters before the +int z = all characters after the plus.But that will only work with 2 numbers.So what you can do is have the user enter their calculations. Then after,have a loop (a while loop) and have code in there that will look for the first 2 numbers (using the + symbols in the string as guidance on where the numbers are). After it finds 2 numbers, it grabs them from the string and places them into integer variables. You delete those characters from the string, and then add the sum of those two variables into an array. Each array element will be a sum of 2 values. So after that, keep looping and grab the next 1 or 2 numbers.  After the string is empty, add up all of the sums from the array to get a final sum.So example (not using the entire and proper code)12+2+5+6+100+8.First iteration of the loop (the first run/loop) will grab 12 and 2 from the StringStore it in 2 variables. int x = 12int z = 2myArray = x + z(the variable i being the amount of times the loop looped/iterated. So first time around i will be equal to 0. Then do i++ at the end of the loop so it iterates by 1).Then the rest would look like thisint totalSum = 0;for(int p = 0, p < myArray.size(); p++){totalSum += myArray[p];}But yeah then from there you can get more complex and check for mixed math stuffs (12+5*6+4-10/3).It's a bit late so I tried my best to explain without code hehe. I hope I helped somewhat or gave you new ideas. James Juan · July 15, 2015 int sum=0, total=0;int value1;,valuue2;char opt;for(int a=0 ; a<10; a++){cin>>value1;cin>>opt;cin>>value2;sum=value1+value2;total+=sum;} Kuroodo Ditory · July 15, 2015 That would work James, but you would have to press enter every time they enter a value.But if OP doesn't mind have to type a something, press enter, type a something, press enter, then it would work. Jon Z. · July 16, 2015 Hi, I think it's easier using scanf, so I wrote it in C instead of C++.It should also work in C++ as long as you include the C stdio.h library.``/** calc.c* by Jon Z.* jon.zz@outlook.com*/#include main(){    int a, b, ans;    char op;    scanf("%d%c%d", &a, &op, &b);    if(op == '+'){        ans = a + b;    }    else if(op == '-'){        ans = a - b;    }    else if(op == '*'){        ans = a * b;    }    else if(op == '/'){        ans = a / b;    }    printf("%d %c %d = %d\n", a, op, b, ans);}`` Manas Mulay · July 16, 2015 Try this..#include #include #include using namespace std;int main(int argc, char *argv[]){    int num;    int sum = 0;    printf("Enter numbers to add. Program ends if you enter 0.\n\n");    do    {        printf("Enter a number: ");        scanf("%i", &num);        sum = sum + num;    }while (num != 0);    printf("The sum of all the numbers is %i.\n\n", sum);    getch();    return 0;} Jon Z. · July 16, 2015 here I fixed it a bit more.``/** calc.c* by Jon Z.* jon.zz@outlook.com*/#include main(){    int a, b;    double ans;    char op;    printf("Enter calculation to make (Ex. 3+7).\tEnter [Ctrl+C] to exit.\n");    while(1){        printf("\n>");        scanf("%d%c%d", &a, &op, &b);        if(op == '+'){            ans = a + b;        }        else if(op == '-'){            ans = a - b;        }        else if(op == '*'){            ans = a * b;        }        else if(op == '/'){            ans = 1.0 * a / b;        }        printf("%d %c %d = %.2f\n", a, op, b, ans);    }}``Output:
• 1

## C++

131,221 followers