Thread: The Volume of The Swimming Pool

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    8

    The Volume of The Swimming Pool

    Hello

    i am a newbie here and just started to study c.

    Is the source below correct?

    I know the maximum number is 4,294,967,295.

    but if i input random numbers sometimes it gives me wrong number.

    How can i rewrite it better??

    Thank you

    Code:
    #include<stdio.h>
    
    int main()
    {
        unsigned int length, width, averagedepth, volume;
        
        printf("Enter the length of the swimming pool: ");
        scanf("%d", &length);
        printf("Enter the width of the swimming pool: ");
        scanf("%d", &width);
        printf("Enter the average depth of the swimming pool: ");
        scanf("%d", &averagedepth);
    
        volume = length * width * averagedepth;
        
        printf("The volume of the pool is ");
        printf("%d",volume);
        
        return 0;
        
    }
    Last edited by minyoungan; 09-22-2010 at 08:22 PM.

  2. #2
    Registered User cph's Avatar
    Join Date
    Sep 2008
    Location
    Indonesia
    Posts
    86
    you should use the %u instead of %d.

    and please use the [code][/code] tag

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    What about upgrading the data type to unsigned long int? Or even unsigned long long int, if your compiler supports it?

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    8
    Thank you

    I will keep those in mind.

    Quote Originally Posted by cph View Post
    you should use the %u instead of %d.

    and please use the [code][/code] tag

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    8
    Thank you!! I use Dev-C++. Does it support unsigned long long int? I upgraded the data type to unsigned long int. It works perfect from 0 to 4,294,967,295. I tried with float but doesn't work at all. Whats wrong with the code below??

    Code:
    #include<stdio.h>
    
    int main() 
    {
        float length, width, averagedepth, volume;
        
        printf("Enter the length of the swimming pool: ");
        scanf("%f", &length);
        printf("Enter the width of the swimming pool: ");
        scanf("%f", &width);
        printf("Enter the average depth of the swimming pool: ");
        scanf("%f", &averagedepth);
    
        volume = length * width * averagedepth;
        
        printf("\nThe volume of the pool is ");
        printf("%f",volume);
        
        return 0;
        
    }
    Quote Originally Posted by Adak View Post
    What about upgrading the data type to unsigned long int? Or even unsigned long long int, if your compiler supports it?

  6. #6
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    What do you mean by "Doesn't work at all"?

  7. #7
    Registered User cph's Avatar
    Join Date
    Sep 2008
    Location
    Indonesia
    Posts
    86
    float? it works for me

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Made me ^^^ chuckle!

  9. #9
    Registered User
    Join Date
    Sep 2010
    Posts
    8
    Quote Originally Posted by kermit View Post
    What do you mean by "Doesn't work at all"?

    Sorryyyyy it works just fine. sometimes the result shows as "0" I dont know why....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 44
    Last Post: 03-19-2010, 04:06 PM
  2. Detect the Audio And Microphone Level In Windows
    By Kelderic in forum C Programming
    Replies: 6
    Last Post: 11-18-2009, 12:21 AM
  3. Volume of a Cone Equation always equals 0
    By Devolution in forum C Programming
    Replies: 11
    Last Post: 01-28-2009, 03:13 AM
  4. Help! Right Math, Wrong Output
    By verd in forum C Programming
    Replies: 12
    Last Post: 03-15-2005, 07:49 PM
  5. What am I doing wrong? Help please...
    By SprinterSteve in forum C Programming
    Replies: 9
    Last Post: 04-17-2003, 09:35 PM