Thread: Calculator

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    1

    Calculator

    Hi, I would like to create a C program which requires the input in this form: number1 operator number2, where number1 and number2 are numbers of type double and operator is one of the characters +,-,*,/.

    There is my attempt to write this code:
    Code:
    #include <stdio.h>
    
    double main()
    {
       char operator;
       double num1, num2;
    
    
       printf("Type what you want to calculate\n");
       scanf("%d%c%d", &num1, &operator, &num2);
    
    
       switch(operator);
       {
    	case '+':
    		printf("num1+num2=%.2f",num1+num2);
    		break;
    	case '-':
    		printf("num1-num2=%.2f",num1-num2);
    		break;
            case '*':
    		printf("num1*num2=%.2f",num1*num2);
    		break;
    	case '/':
    		if(num2 != 0)
    		{
                    	printf("num1/num2=%.2f",num1/num2);
    			break;
    		}
    		else
    		{
    			printf("Error! operator is not correct");
    			break;
    		}
    	default:
    		printf("Error! operator is not correct");
    		break;
       }
       return 0;
    }
    Now I have to solve these problems:
    1. This code above doesn't work and I don't know why.
    2. I don't know how to fix the case when some user enters the input in this form:
      Code:
      1.5896        *5
      or
      Code:
      7    /       5
      I mean how the program knows that
      Code:
      1.5896        *5
      is the same as
      Code:
      1.5896*5
    3. I don't know how to fix the case when the user enters the input in the incorrect form for example
      Code:
      3


    I am a beginner in C, please take note of this while explaining.
    Thanks

  2. #2
    Registered User tidda's Avatar
    Join Date
    Mar 2002
    Posts
    37
    1) lookup the ways in which you can accept data from stdin from the manual.
    2) if you really want to code - given that we know the valid characters you can receive from stdin, i.e. 0 thru 9, +, -, *, / and .(dot)... we know what to do for each character

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    first of 1.5896 is not integer and %d is not suitable

    I would read whoel string into char buffer

    use strtod to convert first argument - and use the pointer parameter to find where the number ends
    Then - use isspace to skip all white spaces till I find operand
    And lastly - use strtod again to extract second argument.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Your code looks fairly good.

    "%lf" is the correct format specifier for reading in a double with scanf. You can see this by looking at the scanf documentation (scanf - C++ Reference). There are also more options which may allow you to do some of the other things that you wish to do.

    Code:
    double main()
    I guess you thought that you should change this to a double because you are working to double instead of ints. This is not correct. It should always be
    Code:
    int main( void )
    until you know better.

  5. #5
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Sorry for double post. Was having connection issues. Feel free to delete, most benevolent moderators.
    Last edited by DeadPlanet; 11-16-2013 at 06:12 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculator in c...
    By nobita in forum C Programming
    Replies: 1
    Last Post: 11-28-2009, 09:22 AM
  2. RPN calculator
    By bassist11 in forum C Programming
    Replies: 14
    Last Post: 03-02-2009, 11:37 PM
  3. calculator
    By adr in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2006, 11:24 AM
  4. Please help with this calculator
    By Asbestos in forum C++ Programming
    Replies: 9
    Last Post: 03-08-2005, 01:00 PM
  5. Calculator Help!
    By jdude in forum C Programming
    Replies: 4
    Last Post: 10-01-2004, 11:48 AM

Tags for this Thread