Thread: Calculator

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    3

    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);
    
    }

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    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.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    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.
    bit∙hub [bit-huhb] n. A source and destination for information.

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    3
    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?

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by Justinjah91 View Post
    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.
    bit∙hub [bit-huhb] n. A source and destination for information.

  6. #6
    Registered User
    Join Date
    Aug 2009
    Posts
    3
    awesome that makes a lot of sense. i think i need to refresh my memory on structures and the like first though

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GUI Calculator - Critique
    By The Brain in forum Windows Programming
    Replies: 1
    Last Post: 02-25-2006, 04:39 AM
  2. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  3. Need help with calculator program
    By Kate in forum C# Programming
    Replies: 1
    Last Post: 01-16-2004, 10:48 AM
  4. Replies: 2
    Last Post: 05-10-2002, 04:16 PM
  5. c++ Reverse Polish Calculator Help
    By knight101 in forum C++ Programming
    Replies: 5
    Last Post: 11-12-2001, 09:31 AM