Page 1 of 3 123 LastLast
Results 1 to 10 of 24

Thread: C++ help

  1. #1
    Join Date
    Jan 2006
    Beans
    239

    Question C++ help

    I'm writing a program that takes change less than a dollar and it shows how many quarters, dimes, nickles, and pennies you get back.

    My problem is that it's showing quarters with a decimal and I don't want that as you can see here.

    Code:
    Enter an amount less than $1.00: .34
    You have 1.36 quarters.
    If I change quarters from a Float to an Int, the code won't compile.

    I thought using modulus would help but that wouldn't even compile.

    here's my code.

    Code:
    # include <iostream>
    
    float amount;
    float quarters;
    //float dimes;
    //float nickles;
    //float pennies;
    
    
    int main()
    {
    
    std::cout << "Enter an amount less than $1.00: ";
    std::cin >> amount;
    
    
    
    quarters = (amount / 0.25);
    
    //dimes = (amount / 0.10);
    
    //nickles = (amount / 0.05);
    
    //pennies = (amount / 0.01);
    
    std::cout << "You have " << quarters << " quarters" << ".\n";
    //std::cout << "You have " << dimes << " dimes" << ".\n";
    //std::cout << "You have " << nickles << " nickles" << ".\n";
    //std::cout << "You have " << pennies << " pennies" << ".\n";
    
    return (0);
    
    }

  2. #2
    Join Date
    Oct 2006
    Location
    Austin, Texas
    Beans
    2,715

    Re: C++ help

    You can cast it...

    Code:
    float x = (int)(4.0 / 3.0);
    EDIT:

    I was reminded by another forum member of the fact that C++ users tend to frown on the use of C type casting... So, I'd also like to offer the use of:

    Code:
    float x = static_cast<int>(4.0 / 3.0);
    Which is the typical C++ way. And you can also use the math "floor" operator which shaves off the decimal digits...

    Code:
    float x = floor(4.0 / 3.0);
    (remember to "#include <cmath>" if you use "floor")
    Last edited by Wybiral; July 20th, 2007 at 03:54 AM.

  3. #3
    Join Date
    Jan 2006
    Beans
    239

    Re: C++ help

    The book says: Given an amount less than $1.00, compute the number of quarters, dimes, nickels, and pennies needed.

    Does this look right?

    It compiles and runs.

    Code:
    # include <iostream>
    
    float amount;
    float quarters;
    float dimes;
    float nickels;
    float pennies;
    
    
    int main()
    {
    while (true){
    
    std::cout << "Enter an amount less than $1.00: ";
    std::cin >> amount;
    
    if (amount == 0)
        break;
    
    quarters = (int)(amount / 0.25);
    
    dimes = (int)(amount / 0.10);
    
    nickles = (int)(amount / 0.05);
    
    pennies = (int)(amount / 0.01);
    
    
    std::cout << "You have " << quarters << " quarters" << ".\n";
    std::cout << "You have " << dimes << " dimes" << ".\n";
    std::cout << "You have " << nickels << " nickels" << ".\n";
    std::cout << "You have " << pennies << " pennies" << ".\n";
    }
    return (0);
    
    }

    Code:
    sdmike@desktop:~/Desktop/programs$ ./change
    Enter an amount less than $1.00:0 .97
    You have 3 quarters.
    You have 9 dimes.
    You have 19 nickels.
    You have 97 pennies.
    Enter an amount less than $1.00: 0
    sdmike@desktop:~/Desktop/programs$

  4. #4
    Join Date
    Oct 2006
    Location
    Austin, Texas
    Beans
    2,715

    Re: C++ help

    It works, but after you cast them to an int, you don't need the variables receiving them to be stored as a float.

  5. #5
    Join Date
    Apr 2007
    Location
    Arkansas
    Beans
    222
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: C++ help

    You have way too many dimes! It's been a while since I've done any programming, but you need to do something like this:

    dimes = (int)((amount - .25 x quarters) / 0.10);

    and so on for the other coins.
    Last edited by jonathanmotes; July 20th, 2007 at 05:03 AM.

  6. #6
    Join Date
    Jan 2006
    Beans
    239

    Re: C++ help

    Quote Originally Posted by jonathanmotes View Post
    You have way too many dimes! It's been a while since I've done any programming, but you need to do something like this:

    dimes = (int)(amount / 0.10) - .25 x quarters;

    and so on for the other coins.

    WAIT - im wrong -- hold on a second!
    This was way before you posted this BTW.

    I tried it this way, but it would just give me zero's.

    So I got frustrated and did it this easy way until I thought out how to do it the correct way.

  7. #7
    Join Date
    Jan 2006
    Beans
    239

    Re: C++ help

    Quote Originally Posted by Wybiral View Post
    It works, but after you cast them to an int, you don't need the variables receiving them to be stored as a float.
    If I then change the variables to int, instead of float, the program just looks like it breaks.

  8. #8
    Join Date
    Oct 2006
    Location
    Austin, Texas
    Beans
    2,715

    Re: C++ help

    Amount will still need to be float, but the others should work with ints... Unless I'm overlooking something, but it looks fine to me.

  9. #9
    Join Date
    Jan 2006
    Beans
    239

    Re: C++ help

    When I try it this way. It compiles but it's still out of whack.

    For example if someone gave me 86 cents,

    3 quarters
    1 dime
    0 nickel
    1 pennies

    Code:
    # include <iostream>
    
    float amount;
    float quarters;
    float dimes;
    float nickels;
    float pennies;
    
    
    int main()
    {
    while (true){
    
    std::cout << "Enter an amount less than $1.00: ";
    std::cin >> amount;
    
    if (amount == 0)
        break;
    
    quarters = (int)(amount / 0.25);
    
    dimes = (int)(amount - (quarters * 0.25) / 0.10);
    
    nickels = (int)(amount - (dimes * .10) / 0.05);
    
    pennies = (int)(amount - (nickels * 0.05) / 0.01);
    
    
    std::cout << "You have " << quarters << " quarters" << ".\n";
    std::cout << "You have " << dimes << " dimes" << ".\n";
    std::cout << "You have " << nickels << " nickels" << ".\n";
    std::cout << "You have " << pennies << " pennies" << ".\n";
    }
    return (0);
    
    }
    Last edited by sdmike; July 20th, 2007 at 06:06 AM.

  10. #10
    Join Date
    Jan 2006
    Beans
    239

    Re: C++ help

    lets say for dimes:

    dimes = (int) (0.10 / (amount - (quarters * 0.25)));

    lets say 0.87
    mathematically this works but the out put is saying 0 dimes when there should be 1.
    Last edited by sdmike; July 20th, 2007 at 06:19 AM.

Page 1 of 3 123 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •