Hello, i'm new here but have heard great things about this community. So I figured I would sign up and check it out.
Now I don't nessicarily have a problem with my assignment I was just looking for some feedback or tips on anything that I should change or redo. I just figured having more then my eyes look over it might help me get a better grade :).
What the program is suppose to do: This program is suppose to act like a cash register. Where the user enters the amount a item costs and also the cash the customer has gave them. It then tells the user how much change is due along with how many dollars, quarters, dimes, nickels, and pennies to give the customer.
Here is my code it compiles and runs good as far as I can tell. I haven't put the finishing touches on the text formatting yet though.
Also one thing that has been giving me trouble is getting the remaining change to output as something like $6.50 instead of $6.5. I have tried setprecision(), but I don't think that will work because there is no extra 0 after the .5 as far as I can tell. Anyways any feedback would be great and try not to be to harsh I have only been programming for a month or so.
write it using fixed before the output. So you would write it this way:
1 2
cout.setprecision(3);
cout << "The remaining change is: $" << fixed << change << endl;
fixed notation specifies how many digits you want to represent, regardless of if there is trailing zeroes or not. Hope that helps.
note: I set setprecision to 3 because that was the format in the original code. But yes, you would change it to 2 if you wanted to properly display change (as in 2 places after the decimal).
Also any idea's on what to do about the if statement in the CalcNickels() function? When I enter 13.50 for the price and $20 it tells me that there is $6.50 change to be given which is right. But it then tells me I need to give
That is with the if statement in there though or did you delete it? Sorry Relize I didn't explain that I was wondering why I had to have the if statement on in there and not on the others. Nickels was the only one that messed up the count on without a if statement out of all the tests I did.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
int CalcNickels(double &ChangeRemaining)
{
int nickels = 0;
// Having trouble here not sure why I have to have the if statement here
// But if I don't it screws up the program sometimes.
if (ChangeRemaining >= 0.05f)
{
while (ChangeRemaining >= 0.05);
{
nickels++;
ChangeRemaining -= 0.05;
}
}
return nickels;
}
Found it.
Delete if statement.
Look at the line 9.
You put semicolon after while so next block isn't related to while! It will execute once and only once everytime.
That's why I prefer K&R brace style over Allman.
Ohhh thank you knew it would probably be something quite stupid lol
Perfect thanks again guys for the help fixing them things. Now to add some formatting to the output, and maybe add a extra feature and hope to get extra credit :).
If anyone else notices any bad practices I used or anything else let me know I love the criticism.
Line 39: comparsion floating point variables using == isn't safe.
You can use std::islessgreater(double, double) function from <cmath> instead of !=. Or use it with NOT operator (!) to replace ==
or if you are not using C++11 you can use something like: