Thread: Basic Sum Calculator

  1. #1
    Registered User
    Join Date
    Sep 2010
    Location
    Portugal
    Posts
    22

    Thumbs down Basic Sum Calculator

    Hey everybody, I started programming C today, and here I am posting my first doubt:

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main(void)
    {   
        int  num1;
        int   num2;
        int  sum;
        
        printf("Enter your first number: ");
        
        scanf("%d", &num1);
        
        printf("Enter your second number: ");
        
        scanf("%d", &num2);
        
        sum = num1+num2;
        
        printf("The sum is %d", sum);
        
        
        getch();
    }
    This works fine as a sum calculator, but only with INT values (I'm a programmer for a long time, but I started C today, used to program Java and Python, so I know quite a lot).

    Whenever I try to use FLOAT values, it goes crazy, try it on your computer please.



    Thanks in advance,

    ScoutDavid

  2. #2
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198
    Just declare your variables as floats, example float sum.

    here's a link that I still use:

    C variable types and declarations - Wikipedia, the free encyclopedia

  3. #3
    Registered User
    Join Date
    Sep 2010
    Location
    Portugal
    Posts
    22
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main(void)
    {   
        float  num1;
        float   num2;
        float  sum;
        
        printf("Enter your first number: ");
        
        scanf("%d", &num1);
        
        printf("Enter your second number: ");
        
        scanf("%d", &num2);
        
        sum = num1+num2;
        
        printf("The sum is %d", sum);
        
        
        getch();
    }
    Thanks for the link very useful, but I followed your tip and still crazy, it says the result is 0 even before I type the second number...

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    You need to use %f in scanf if you are trying to get a floating point number. Same goes for printf. %d is to get an int (scanf), or print an int (printf).
    Last edited by kermit; 09-25-2010 at 12:57 PM.

  5. #5
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198
    Quote Originally Posted by ScoutDavid View Post
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main(void)
    {   
        float  num1;
        float   num2;
        float  sum;
        
        printf("Enter your first number: ");
        
        scanf("%d", &num1);
        
        printf("Enter your second number: ");
        
        scanf("%d", &num2);
        
        sum = num1+num2;
        
        printf("The sum is %d", sum);
        
        
        getch();
    }
    Thanks for the link very useful, but I followed your tip and still crazy, it says the result is 0 even before I type the second number...
    I did NOT want to make all your corrections, otherwise you wont learn. You
    also have to change the scanf() format. You still have it scanning for type int,
    instead of type float.

    This question is answered on the FAQ here, shall I search for it, or do you want
    to give it a try?

    Edit:

    Nevermind, you were spoon fed the answer.
    Last edited by Char*Pntr; 09-25-2010 at 01:07 PM.

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    6
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        float  num1;
        float  num2;
        float  sum;
    
        printf("Enter your first number: ");
    
        scanf("%f", &num1);
    
        printf("Enter your second number: ");
    
        scanf("%f", &num2);
    
        sum = num1+num2;
    
        printf("The sum is %f", sum);
    
    
        getch();
    }
    This might help. U need to change the input and output to float variables also.

  7. #7
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198
    How Do I? check out this FAQ:

    Cprogramming.com FAQ

  8. #8
    Registered User
    Join Date
    Sep 2010
    Location
    Portugal
    Posts
    22
    I've gotten there:

    "You need to use %f in scanf if you are trying to get a floating point number."

    Thank you very much!

  9. #9
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198
    Quote Originally Posted by ScoutDavid View Post
    I've gotten there:

    "You need to use %f in scanf if you are trying to get a floating point number."

    Thank you very much!
    And while you're in the FAQ, check out the rest of it! There's a lot of answers
    there, so many, you'll learn the answers before you get a question.

    (The How Do I.....)

  10. #10
    Registered User
    Join Date
    Sep 2010
    Location
    Portugal
    Posts
    22
    #include <stdio.h>
    #include <conio.h>

    int main(void)
    {
    float num1;
    float num2;
    float sum;

    printf("Enter your first number: ");

    scanf("%f", &num1);

    printf("Enter your second number: ");

    scanf("%f", &num2);

    sum = num1+num2;

    printf("The sum is %f", sum);


    getch();
    }

    3.4 + 52 = 55.400002?!!

    What the heck?

  11. #11
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198
    Quote Originally Posted by ScoutDavid View Post

    What the heck?

    Hmmm... maybe I should have directed you to this link first:

    How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Sep 2010
    Location
    Halesowen, England
    Posts
    30
    A float is only accurate to a few decimal places, but you can use:
    Code:
    printf("The sum is %.2f", sum);
    to force it to show to 2 decimal places.

  13. #13
    Registered User
    Join Date
    Sep 2010
    Location
    Portugal
    Posts
    22
    Quote Originally Posted by Jamdog View Post
    A float is only accurate to a few decimal places, but you can use:
    Code:
    printf("The sum is %.2f", sum);
    to force it to show to 2 decimal places.

    Thank you very much!

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, David!

    Now repeat after me:

    "On my honor, I will do my best to do my duty, and always post my code with code tags around it."

    The forum will make your code miserable to study, if you don't use code tags (highlight your code and click on the # icon in the advanced editing window of the forum).

    With apologies to Baden-Powell.

  15. #15
    Registered User
    Join Date
    Sep 2010
    Location
    Portugal
    Posts
    22
    "On my honor, I will do my best to do my duty, and always post my code with code tags around it."


    I did it actually, not hightlight but code it

    I'm in a Python Forum as well and am a long time programmer, I guess i know the rules :lol:

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Math calculator and compiler errors
    By littlefermat245 in forum C Programming
    Replies: 2
    Last Post: 10-12-2009, 09:45 PM
  2. Minor Problem
    By stewie1986 in forum C Programming
    Replies: 6
    Last Post: 11-30-2007, 08:40 AM
  3. Basic Calculator
    By Surge in forum C Programming
    Replies: 18
    Last Post: 12-02-2006, 10:20 PM
  4. need help with my basic c++ calculator
    By iCouch_Potato in forum C++ Programming
    Replies: 4
    Last Post: 03-24-2004, 06:23 PM