### Help with if and else statements

Hello again, I need help with exercise 3.11 in the book programming: Principles and practice using c++...

the task is to write a program that outputs the amount of pennies, nickels, quarters, halves and dollars that the user has.

I completed the program fine but the second part of the task is to improve the program so that it is grammatically correct e.g if the user has one penny it outputs one penny instead of one pennies... The problem is that when i enter that i have 2 pennies or any other of the values it gives me the singular value after the number e.g you have 2 penny or you have 56 dime... I haven't fully grasped the if and else statements yet so i believe the problem lies within them.

 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 #include using namespace std; int main (){ int pennies = 0; int nickels = 0; int dimes = 0; int quarters = 0; int half = 0; int dollar = 0; int total; cout << "How many pennies do you have? "; cin >> pennies; cout << "\nHow many nickels do you have? "; cin >> nickels; cout << "\nHow many dimes do you have? "; cin >> dimes; cout << "\nHow manu quarters do you have? "; cin >> quarters; cout << "\nHow many half dollars do you have? "; cin >> half; cout << "\nHow many dollars do you have? "; cin >> dollar; if (pennies = 1){ cout << "\nYou have " << pennies << " penny";} else cout << "\nYou have " << pennies << " pennies"; if (nickels = 1){ cout << "\nYou have " << nickels << " nickel";} else cout << "You have " << nickels << "nickels"; if (dimes = 1){ cout << "\nYou have " << dimes << " dime";} else cout << "\nYou have " << dimes << " dimes"; if (quarters = 1){ cout << "\nYou have " << quarters << " quarter";} else cout <<"\nYou have " << quarters << " quarters"; if (half = 1){ cout << "\nYou have " << half << " half of a dollar";} else cout << "\nYou have " << half << " halves of a dollar"; if (dollar = 1){ cout << "\nYou have " << dollar << " doallar";} else cout << "\nYou have " << dollar << " dollars"; nickels = nickels * 5; dimes = dimes * 10; quarters = quarters * 25; half = half * 50; dollar = dollar * 100; total = pennies + nickels + dimes + quarters + half + dollar; cout << "\nIn total you have " << total << " cents\n"; }

This

if (pennies = 1){

is not a comparision. It is an assignment. Comparison is done by means of the comparision operator ==
That was spot on vlad, working now! I should have known that im aware of the of the comparison operator... Thanks ! Another quick Question... the third improvement you have to make is to the report the total in dollars for e.g. if the user has 563 cents it should output "You have \$5.62 dollars" I'm thinking that every 100 cents = 1 dollar but how would i go about writing this in code? something to do with the modulo operator?
Last edited on
total number of cents / 100 will give you the number of dollars.
and
total number of cents % 100 will give you the number of cents.
sound vald, worked a treat :)
Topic archived. No new replies allowed.