So I am taking C++ at thomas edison state college for a degree. I needed a computer course so I chose C++ instead of PLCs. The reason being is that I used to do some basic coding back in the apple IIe days. I apparently don't have the attention span or interest anymore so I am having issues. Maybe its kids or wife or whatnot. I do understnd it is different progrmming. Now the problem I have is that I cannot figure out how to math it correctly. For instance when I enter 1.88 it gives me the appropriate amount of coins using standard US currency. When I enter any value that has 2 pennies and zero nickles it gives the wrong amount..-ie 1 penny. How do I fix this...I've been wracking my brain and well it's not in there. Please help!

PS- Not sure if this was posted somewhere else I'm new to this forum and to C++.
PSS- I can post the actual problem if that would help. I'm looking for guidance since I want to understand.

#include <iostream>                                                              

using namespace std;                                                             

int main ( )                                                                     
{                                                                                
    float change;                                                                
    int variable, quarters, dimes, nickels, pennies;                             
    cout <<"Please enter a value: ";                                             
    cin >> change;                                                               
    variable = change * 100;                                                     
    quarters = variable / 25;                                                    
    variable = variable % 25;                                                    
    dimes = variable / 10;                                                       
    variable = variable % 10;                                                    
    nickels = variable / 5;                                                      
    pennies = variable % 5;                                                      

    cout << "\nQuarters: " << quarters << "\n";       
    cout << " Dimes: " << dimes << "\n";                  
    cout << " Nickels: " << nickels << "\n";              
    cout <<" Pennies: " << pennies << "\n";               

    return (0);                                                                  
}                                    

Remember declaring ints assuming integer values. I.e whole numbers.

I would probably declare them all as doubles.

Then you can start to look at your logic.

I figured since the coin numbers would be in int values I would just convert my 3 digit currency number into a whole number by multiplying by 100. Is there a fault in my logic? It works just not for values <$1 ending in a 2 or 8. I'm not exactle sure how to go about doing that. I mean I know what you are saying but I'm programming dumb and this took me awhile to come up with..lol

You figured correctly. By multiplying by 100 you have eliminated most of the problems floating point values will cause.

By the way, when making a post, it is OK to actually view it to see how it looks and modify the post so it's readable. Just sayin'. I was going to ignore it because of the crappy format.

Try outputting the values at key spots to make sure you are generating the correct values and variable is correct each step of the way.

I don't really understand what's wrong with my format. I'm literally brand new to C++ and have never taken a computer course before. Should I add comment blocks to explain my steps? Honestly any help is appreciated, My text book is written for class work and really isn't all that helpful other than a reference.

I do apologize. Yourself and WaltP are right in that using ints will be OK. I didn't think about it long enough.

Follow Walt's suggestion, but this is a common homework question and googling alone I found an answer quickly.

My outputs seem to be good to me...it just doesn't like .x2 or .x8 for some reason...calculates tham as .x1 or .x7 so it gives the coin value for .41 when I input .42

Yeah I was trying to avoid googling...I don't want to travel down that path, mI have motivation issues already!

Yeah I was trying to avoid googling...I don't want to travel down that path, mI have motivation issues already!

Nice. Keep thinking and I'm sure you'll get there.

Also, I'm not sure, your formatting is fine unless I'm missing something? Did one of the moderators tidy it up?

Looks exactly the same as my program. How would I test the outputs? I tried adding in cout << "\ntest: " << coinname << "\n"; replacing coinname with the proper variable. also it gives the wrong count on 0.12, 0.22, 0.32, 0.42, 0.52 but yileds the proper count on 0.62 and above.

Edited 4 Years Ago by dustin.short.5

I don't really understand what's wrong with my format.

You're kidding, right? You see nothing wrong with

    variable = change *
100;                                                     
    quarters = variable /
25;                                                    
    variable = variable %
25;                                                    
    dimes = variable /
10;                                                       
    variable = variable %
10;                                                    
    nickels = variable /
5;                                                      
    pennies = variable %
5;                                                      

????

How would I test the outputs? I tried adding in cout << "\ntest: " << coinname << "\n"; replacing coinname with the proper variable.

And what did outputting variable after the line
variable = change * 100;
show you?

I didn't see that. Why does it have a value less than the input? testing the variable change with an input of 1.88 gave me a value of 187. I could just place it in parenthesis and then +1 to get the proper value. I would like to understand why this happens though.

Edited 4 Years Ago by dustin.short.5

Why would it round down though? 0.52*100=52 not 51. Once the input exceeded 1.00 it got rid of the rounding error, or perhaps I didn't check enough of the inputs to verify.

Why would it round down though? 0.52*100=52 not 51.

Because

Once the input exceeded 1.00 it got rid of the rounding error, or perhaps I didn't check enough of the inputs to verify.

You didn't check enough inputs.

This article has been dead for over six months. Start a new discussion instead.