Calculation Troubles

This is a discussion on Calculation Troubles within the C++ Programming forums, part of the General Programming Boards category; I have to take a 4-digit number and calculate it like this: First 70 cubic meters $5.00 minimum cost Next ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    4

    Unhappy Calculation Troubles

    I have to take a 4-digit number and calculate it like this:

    First 70 cubic meters $5.00 minimum cost
    Next 100 cubic meters 5.0 cents per cubic meter
    Next 230 cubic meters 2.5 cents per cubic meter
    Above 400 cubic meters 1.5 cents per cubic meter

    Soo, for the number 203, it would calculate $10.83. Ok, I've set it up numerous ways, and can't get anywhere near the correct price. Basically what I need to know is -- How can I break up one number into those 4 different calculations?

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Ever heard of ifs? Sounds like they'd come in handy here.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #3
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Loops would be another option. But you need to show us what you got before we can go into any more detail

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    4
    I'll attach what I've already got. Keep in mind I'm very new to this, hehehe. Thanks for the help so far.
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    4
    I tried doing this for the calculations, but my numbers are still off by a few dollars.

    cost1 = gasused%10 * 5.00;
    cost2 = (gasused/10)%10 * 0.05;
    cost3 = (gasused/10/10)%10 * 0.025;
    cost4 = (gasused/10/10/10)%10 * 0.015;
    totalcharges = cost1 + cost2 + cost3 + cost4;

  6. #6
    mov.w #$1337,D0 Jeremy G's Avatar
    Join Date
    Nov 2001
    Posts
    704
    This was fun:

    PHP Code:
    #include <iostream.h>
    #include <windows.h>
    #include <stdio.h>

    /* RULES:
    First 70 cubic meters $5.00 minimum cost
    Next 100 cubic meters 5.0 cents per cubic meter
    Next 230 cubic meters 2.5 cents per cubic meter
    Above 400 cubic meters 1.5 cents per cubic meter
    */
    int main() 
    {
        
    int number;
        
    float total 5.0f;
        
    float price;
        
    printf("Enter cubic meters:\n");
        
    cin >> number;

        if( 
    number <= 70 ) {
            
    printf("Total price: $%f.\n"total);
            return 
    0;
        } else if( 
    number <= 170 
            
    price 0.05f;
        else if( 
    number 400 )
            
    price 0.025f;
        else if( 
    number 400 )
            
    price 0.015f;

            
        
    total += price number;

        
    printf("The total price is: $%f."total  );
        return 
    0;

    c++->visualc++->directx->opengl->c++;
    (it should be realized my posts are all in a light hearted manner. And should not be taken offense to.)

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    4
    I'm glad you thought it was fun. Thanks for the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Date calculation program
    By putty88 in forum C Programming
    Replies: 5
    Last Post: 04-17-2009, 07:24 AM
  2. Problem with modulo calculation
    By manutdfan in forum C Programming
    Replies: 6
    Last Post: 01-12-2009, 02:37 PM
  3. C# calculation problem
    By Diablo02 in forum C# Programming
    Replies: 13
    Last Post: 10-25-2007, 08:42 PM
  4. Help with calculation and loop
    By Betty in forum C Programming
    Replies: 7
    Last Post: 04-10-2006, 05:37 AM
  5. help with pay calculation program
    By shugstone in forum C++ Programming
    Replies: 2
    Last Post: 02-17-2003, 08:38 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21