Page 3 of 3 FirstFirst 123
Results 21 to 24 of 24

Thread: C++ help

  1. #21
    Join Date
    Nov 2005
    Beans
    403
    Distro
    Ubuntu Development Release

    Re: C++ help

    Quote Originally Posted by sdmike View Post
    What is "static_cast<int>"?
    It's a way of type casting, that is, converting data from one of C++'s number types to another. In this case you want to convert from a float to an int.

    In C you would write,
    Code:
    change = (int) (t*100)
    but I think in C++ the use of static_cast<> is safer.

    http://www.codeproject.com/cpp/static_cast.asp

  2. #22
    Join Date
    Apr 2013
    Beans
    1

    Re: C++ help

    Problem is at
    pennies = (int)(amount / 0.01);

    if you want in INT you will always have answer in 0; so you cant calculate pennies.

    so you have to change it to..

    pennies = (amount / 0.01);

    now another thing to do is.. make it round ....

    int roundingp = pennies+0.5;

    and then
    pennies = roundingp;

    if i paste your program after making some change here it will be !
    Code:
    # include <iostream.h>
    #include<conio.h>
    
    float amount;
    float quarters;
    float dimes;
    float nickels;
    float pennies;
    
    
    int main()
    {
    cout << "Enter an amount less than $1.00: ";
    cin >> amount;
    
    //if (amount == 0)
    
    
    quarters = (int)(amount / 0.25);
    amount -= (quarters * 0.25);
    
    dimes = (int)(amount / 0.10);
    amount -= (dimes * 0.10);
    
    nickels = (int)(amount / 0.05);
    amount -= (nickels * 0.05);
    
    
    pennies = (amount / 0.01);
    //cout<< pennies;
    
    cout << "You have " << quarters << " quarters" << ".\n";
    cout << "You have " << dimes << " dimes" << ".\n";
    cout << "You have " << nickels << " nickels" << ".\n";
    int roundingp = pennies+0.5;
    pennies = roundingp;
    cout << "You have " << pennies << " pennies" << ".\n";
    
    
    getch();
    return 0;
    
    }
    Result Snapshopt
    where input is 0.11

    Enter an amount less then $1.00: 0.11
    You have 0 quaters.
    You have 1 dimes.
    You have 0 quaters.
    You have 1 pennies.




    Reply if having any difficulty !

  3. #23
    Join Date
    Oct 2006
    Beans
    58,286

    Re: C++ help

    Old thread closed.

  4. #24
    Join Date
    Feb 2008
    Location
    Land of fire and drought
    Beans
    Hidden!
    Distro
    Xubuntu

    Re: C++ help

    Thread Closed

    Reason:
    Necromancy.

    Welcome to the forums. This thread is reeealllly old, hasn't seen any action in nearly six years, so better left to sleep. As you're a newcomer you might like to read the Code of Conduct, if you haven't already. Here is an excerpt:

    If a post is older than a year or so and hasn't had a new reply in that time, instead of replying to it, create a new thread. In the software world, a lot can change in a very short time, and doing things this way makes it more likely that you will find the best information. You may link to the original discussion in the new thread if you think it may be helpful.
    Good luck.

Page 3 of 3 FirstFirst 123

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
  •