Thread: Program to emulate calculator

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

    Program to emulate calculator

    Hi all,

    i was told to do up a program to emulate a calculator (+-*/ functions) but i am unable to get the program to show my answer.
    attached is the program i typed. can someone help me out?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    double Addition(double num1, double num2){
        return num1+num2;
    }
    double Subtraction(double num1, double num2){
        return num1-num2;
    }
    double Multiplication(double num1, double num2){
        return num1*num2;
    }
    double Division(double num1, double num2){
        return num1/num2;
    }
    main (int argc, char *argv[]){
        char sign;
        double first_num, second_num;
        printf("Type 2 numbers and an operation\n");
        scanf("%lf %c %lf", &first_num, &sign, &second_num);
    
        if (&sign == "+")
            printf("%f\n", Addition(first_num, second_num));
     else if (&sign == "-")
            printf("%f\n", Subtraction(first_num, second_num));
     else if (&sign == "*")
            printf("%f\n", Multiplication(first_num, second_num));
     else if (&sign == "/")
            printf("%f\n", Division(first_num, second_num));
    }

  2. #2
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    When you do (&sign == "+") you're actually performing an address comparison. You're comparing the address of the variable sign (which you get by prepending the &) with the address of the global string literal "+".
    What you really want is a character comparison. So you need to take the variable itself and compare agains the *character* +, which you do by using single quotes.
    Also two other things to note are that you are missing the curly braces on your if and else if statements and that the format specifier in your printf statements is missing the l (ell) that makes it print out a double instead of a float.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by QuantumPete View Post
    that the format specifier in your printf statements is missing the l (ell) that makes it print out a double instead of a float.
    Whilst the rest of your post is indeed correct, this part is not.
    1. All floating point values are promoted to double when passed to var-args functions (and functions with unspecified arguements - old-style code). Thus %f is used for float and double - since they are all double anyways.
    2. %lf is used in some compilers (c99) to indicate "long double", which would lead to erroneous output. In other compilers, %lf _may_ mean double, but in others it will be "unknown" [and thus in the clearly undefined behaviour arena].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    2
    it finally works!
    haha thanks to the both of u pete and mats!

  5. #5
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by matsp View Post
    Whilst the rest of your post is indeed correct, this part is not.
    1. All floating point values are promoted to double when passed to var-args functions (and functions with unspecified arguements - old-style code). Thus %f is used for float and double - since they are all double anyways.
    2. %lf is used in some compilers (c99) to indicate "long double", which would lead to erroneous output. In other compilers, %lf _may_ mean double, but in others it will be "unknown" [and thus in the clearly undefined behaviour arena].

    --
    Mats
    You live and learn. I was sure thaf %f is float and %lf is double...
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by QuantumPete View Post
    You live and learn. I was sure thaf %f is float and %lf is double...
    For scanf, it is. For printf, it isn't.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by QuantumPete View Post
    Also two other things to note are that you are missing the curly braces on your if and else if statements...
    Oh, but it's not required. It's optional.
    More like it's recommended. But then again, perhaps not, because it takes space.
    There's no easy answer to that question, but it certainly isn't required.

    Oh and you're using implicit main. That's bad.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by Elysia View Post
    Oh, but it's not required. It's optional.
    True, but it's better to have them and not need them, then to need them and not have them

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  9. #9
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by tabstop View Post
    For scanf, it is. For printf, it isn't.
    [sarcasm]Well of course, that makes sense, two functions from the same library using the same format specifier meaning different things. [/sarcasm] There are days I hate C

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    same format specifier meaning different things
    Not much sense to do different format specifiers for float and double in printf while both are passed as double

    scanf is other story...
    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

  11. #11
    Registered User
    Join Date
    Sep 2008
    Posts
    6
    Hi, I also need help in the code... after trying out with the modified code, i still receive error upon running... and also unable to get program to show the answer
    Can anyone help? Need it to be done urgently... ):

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    double Addition(double num1, double num2){
        return num1+num2;
    }
    double Subtraction(double num1, double num2){
        return num1-num2;
    }
    double Multiplication(double num1, double num2){
        return num1*num2;
    }
    double Division(double num1, double num2){
        return num1/num2;
    }
    main (int argc, char *argv[]){
        char operator;
        double first_num, second_num;
        printf("Type 2 numbers and an operator\n");
        scanf("&#37;f %c %f", &first_num, &operator, &second_num);
    
        if (&operator == '+') {
            printf("%f\n", Addition(first_num, second_num));
     }else if (&operator == '-') {
            printf("%f\n", Subtraction(first_num, second_num));
     }else if (&operator == '*'){
            printf("%f\n", Multiplication(first_num, second_num));
     }else if (&operator == '/'){
            printf("%f\n", Division(first_num, second_num));
    }
    }

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    And you didn't read Pete's first response because why?

  13. #13
    Registered User
    Join Date
    Sep 2008
    Posts
    6
    eh it's because I don't really understand.. i'm totally new to this..
    and i did add in the brackets to if and if else statements, changing the " " to ' ' , &#37;If to %f
    it still don't work..
    Last edited by Zihui02; 09-04-2008 at 09:48 AM.

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    And if you do the other bit mentioned, namely getting rid of &, what happens?

    And if you don't understand what he said, look up the words in the index of your textbook to get a brush-up on the vocabulary.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The & operator takes the ADDRESS of something and the ADDRESS of something generates an extra indirection, ie char becomes char*, and char* is not the same as char. The end.
    And you still haven't fixed main either.
    But do say, do you have a book or is it a course from where you're teaching yourself?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help with Calculator program
    By Sshakey6791 in forum C++ Programming
    Replies: 3
    Last Post: 12-21-2008, 09:36 AM
  2. Need some help with Calculator program
    By Sshakey6791 in forum C++ Programming
    Replies: 0
    Last Post: 12-21-2008, 09:16 AM
  3. Help with calculator program.
    By banjordan in forum C++ Programming
    Replies: 1
    Last Post: 12-03-2003, 10:01 PM
  4. Newbie Needs Help on Calculator Program
    By CompWiz84 in forum C++ Programming
    Replies: 13
    Last Post: 06-26-2002, 02:21 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM