Thread: Calculator

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    2

    Calculator

    Hey I started using this site to learn C yesterday, and I got to the chapter on functions and decided to try and make a little program to do simple calculations.
    This is the what I've written so far;
    Code:
    #include <stdio.h>
    
    int calc ( int x, int y );
    
    int main()
    {
      int x;
      char f;
      int y;
    
      printf( "Please input a number: " );
      scanf( "%d", &x );
      printf("Please choose a function(add, subtract, divide, or multiply):");
      scanf("%c", f );
      printf( "Please input a second number: " );
      scanf( "%d", &y );
      if (f == *);
      {
      printf( "The product of your two numbers is %d\n", mult( x, y ) );
      }
      else if (f == +);
      {
      printf( "The product of your two numbers is %d\n", add( x, y ) );
      }
      else if (f == /);
      {
      printf( "The product of your two numbers is %d\n", divide( x, y ) );
      }
      else (f == -);
      {
      printf( "The product of your two numbers is %d\n", subtract( x, y ) );
    }
      getchar();
    }
    
    int mult (int x, int y)
    {
      return x * y;
    }
    int add (int x, int y)
    {
      return x + y;
    }
    int subtract (int x, int y)
    {
      return x - y;
    }
    int divide (int x, int y)
    {
      return x / y;
    }
    However I can't seem to make it work, I get an error on the lines with the if statements saying; "syntax error before ')' token.
    Can anyone help? Thanks

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    if (f == *);

    '*' - to specify the literal constant

    and the ; at the end should be removed


    scanf("&#37;c", f );
    this line also has problems
    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

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    2
    thanks, that worked, but now i have a new problem lol, subtracting works fine, but if i try adding it just subtracts, and if i enter the * or / keys it automatically skips the option of adding a second number.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    All your char operators, need that same 'char_here', treatment. Just to expand on Vart's good eye, here.

    if(f == '-')

    is not the same thing as

    if(f == -)

    at all.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > scanf("&#37;c", f );
    What is missing here, compared to your other scanf calls?

    Also note that %c does NOT skip white space unlike all the other simple scanf formats.
    So 3+2
    would be different from 3 +2


    Which compiler are you using?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    should be an else-if and without the terminating semicolon else (f == -);

  7. #7
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    For clarity of code, "Self documenting" code I would replace:

    1. variable f to be operator

    2. You can use if statements here, as you have a short conditional list, but a switch/case statement would be preferred.

    3. As mentioned all your "operators" should be wrapped within single quotes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need help modifying my calculator!!
    By Matus in forum C Programming
    Replies: 5
    Last Post: 03-25-2008, 12:03 PM
  2. GUI Calculator - Critique
    By The Brain in forum Windows Programming
    Replies: 1
    Last Post: 02-25-2006, 04:39 AM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. Need help with calculator program
    By Kate in forum C# Programming
    Replies: 1
    Last Post: 01-16-2004, 10:48 AM
  5. Replies: 2
    Last Post: 05-10-2002, 04:16 PM