# Calculator

• 08-18-2009
Justinjah91
Calculator
First off, I want to say that I am brand new to programming (I started yesterday). I thought that for my first actual project I would make a calculating program with the four basic functions of addition, subraction, multiplication, and division. I made one that works, but my problem is that I couldnt find a way to make the program so that you actually have to input the +, -, *, and /. Is there any way to do this? Here's what I have right now.

Code:

```#include <stdio.h> int main () {     int v;     int w=1;     int x;     int y;     int z;         do {         printf("Add-1, Subtract-2, Multiply-3, Divide-4: ");         scanf("%d", &z);         if(z==1) {         printf("Type the two numbers you would like to add: ");         }         else if(z==2) {         printf("Type the two numbers you would like to subtract: ");         }         else if(z==3) {         printf("Type the two numbers you would like to multiply: ");         }         else if(z==4) {         printf("type the two numbers you would like to divide: ");         }         else         printf("Reset?");         if(z) {         scanf("%d", &x);         scanf("%d", &y);         if(z==1) {         v = x + y;         }         else if(z==2) {         v = x - y;         }         else if(z==3) {         v = x * y;         }         else if(z==4) {         v = x / y;         }         printf("The result is %d\n", v);         getchar ();         }         }         while (w); }```
• 08-18-2009
KBriggs
Inputting the functions gets really complicated fast - it is fairly simple if you are only dong a single operation, but for longer expressions you are going to find that it is a pain. Check out strtok - C++ Reference - you can use this to split up an input string into numbers and operators and go from there.
• 08-18-2009
bithub
Quote:

but my problem is that I couldnt find a way to make the program so that you actually have to input the +, -, *, and /.
What do you mean by this? Do you want the user to just enter in something like "1+4", and your program would output "5" as the answer? Or you do want to do something like, "Enter the operator to perform on the 2 numbers", and your program would use that operator on the 2 numbers entered.
• 08-18-2009
Justinjah91
well the way i have it set up now, you have to pick a function first, and you just input the two numbers seperated by a space and it gives you the answer. this works fine, but i want it to ask what mathmatical statement you want evaluated, so that you have to specify 3+4, or 7*6 without specifying what function you are using beforehand. does that make sense?
• 08-18-2009
bithub
Quote:

Originally Posted by Justinjah91
well the way i have it set up now, you have to pick a function first, and you just input the two numbers seperated by a space and it gives you the answer. this works fine, but i want it to ask what mathmatical statement you want evaluated, so that you have to specify 3+4, or 7*6 without specifying what function you are using beforehand. does that make sense?

It makes perfect sense, but it is not an easy problem. Read this thread for some insight.
• 08-18-2009
Justinjah91
awesome that makes a lot of sense. i think i need to refresh my memory on structures and the like first though