Thread: Just a simple program just confused

  1. #1
    Registered User
    Join Date
    Jul 2015
    Posts
    19

    Question Just a simple program just confused

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
        float totalProfit;
        int priceOfmalunggay;
        int tax;
        int totalMalunggay;
    
    
        printf("How many malunggay did you get?\n");
        scanf("%d", &totalMalunggay);
        printf("How much did you sell the Malunggay?\n");
        scanf("%d", &priceOfmalunggay);
        printf("What is the percentage of your value added tax?\n");
        scanf("%d", &tax);
        tax = tax/100;
        totalProfit = ((float)priceOfmalunggay * (float)totalMalunggay) * (float)tax;
    
    
        printf("Your total profit is: %.2f", totalProfit);
    
    
    
    
        return 0;
    }
    Just want to know why it is 0 everytime.

  2. #2
    spaghetticode
    Guest
    Because 'tax' is an integer, and in line 19 you divide that integer, which is most likely something < 100, by 100. The result is always 0, making the result of your calculation in line 20 also 0.

  3. #3
    Registered User
    Join Date
    Jul 2015
    Posts
    19
    Quote Originally Posted by spaghetticode View Post
    Because 'tax' is an integer, and in line 19 you divide that integer, which is most likely something < 100, by 100. The result is always 0, making the result of your calculation in line 20 also 0.
    What should I put? I changed tax to float still it wont work.

  4. #4
    spaghetticode
    Guest
    Show your updated code, example input and the according output.

  5. #5
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Quote Originally Posted by YaZao View Post
    What should I put? I changed tax to float still it wont work.
    Make tax and priceOfmalunggay both floats, change scanf() to input both as floats, and you should be OK. Are the tax and price usually integers, or floating point values? As @spaghetticode said, please post your updated code.

  6. #6
    spaghetticode
    Guest
    Quote Originally Posted by rstanley View Post
    change scanf() to input both as floats
    Oh sure, I did not think about that one, you're still reading in integers!

  7. #7
    Registered User
    Join Date
    Jul 2015
    Posts
    19
    Code:
    #include #include int main(){    float totalProfit;    int priceOfmalunggay;    float tax;    int totalMalunggay;    printf("How many malunggay did you get?\n");    scanf("%d", &totalMalunggay);    printf("How much did you sell the Malunggay?\n");    scanf("%d", &priceOfmalunggay);    printf("What is the percentage of your value added tax?\n");    scanf("%f", &tax);    tax = tax/100;    totalProfit = ((float)priceOfmalunggay * (float)totalMalunggay) * (float)tax;    printf("Your total profit is: %.2f", totalProfit);    return 0;}
    okay what I did is in line 18 I just changed "%d" to "%f" just like you guys were saying. And changed "int tax" to "float tax" and it went well.

  8. #8
    spaghetticode
    Guest
    Good to hear and good job!

  9. #9
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Besides all your code appearing one one line here, you could also drop the casts as you have changed two of the three variables to floats, and the price variable will promote to.floating point in the calculation. Are you sure the price variable should not be floating point?

  10. #10
    spaghetticode
    Guest
    Quote Originally Posted by rstanley View Post
    Are you sure the price variable should not be floating point?
    Whenever a program deals with money values, it literally cries for floating point data types. The amount of coins < 1 Euro in my wallet says so, too.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by spaghetticode
    Whenever a program deals with money values, it literally cries for floating point data types. The amount of coins < 1 Euro in my wallet says so, too.
    No, it cries for types that can accurately handle money values, e.g., fixed point decimal. Floating point binary types are not among them, but as alternatives are likely to be beyond the scope of this exercise, resorting to float or double will do.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    spaghetticode
    Guest
    Sure, that was not accurate. What I meant was 'non-integer numeric data types'.

  13. #13
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Quote Originally Posted by laserlight View Post
    No, it cries for types that can accurately handle money values, e.g., fixed point decimal. Floating point binary types are not among them, but as alternatives are likely to be beyond the scope of this exercise, resorting to float or double will do.
    I took into consideration the level of C knowledge of the OP, learning on his/her own, or in a beginners class, when I suggested using floats. Yes, accurate calculations of money would require more advanced code, or a pre-compiled library. I didn't consider it appropriate to suggest a more advanced solution when offering a response that will help the OP (And others) to understand the problem in the code presented.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. being confused about a program segment
    By muhtar in forum C Programming
    Replies: 2
    Last Post: 06-08-2015, 09:18 PM
  2. Got confused over a C program
    By dotnet13 in forum C Programming
    Replies: 12
    Last Post: 04-27-2013, 01:48 AM
  3. confused on how the logic for this program
    By confused in c in forum C Programming
    Replies: 8
    Last Post: 11-04-2010, 05:25 PM
  4. Help! confused on how to run this program about money
    By jumbo2410 in forum C Programming
    Replies: 2
    Last Post: 06-13-2008, 10:08 PM
  5. My program is confused...
    By face_master in forum Windows Programming
    Replies: 1
    Last Post: 02-03-2002, 04:33 AM

Tags for this Thread