So, I've begun to learn how to program. I'm making my programs work, but I can't help but feel I'm not "thinking like a programmer." Now, I'm using a very specific book called
"programming principles and practice using c++ 2nd edition"
by a professor Stroustrup. I enjoy the book, because it's great for beginners, in my opinion. Sadly, it lacks any answers to the exercises that I see, so there isn't any real way to compare my work to a well structured program.
Now, at this point the book has covered "if" statements, and a little bit on while loops. Other than that, I've covered nothing else.
The problem:
Write a program that prompts the user to enter some number of pennies (1-cent coins), nickels (5-cent coins), dimes (10cent coins), quarters (25-cent coins), half dollars (50-cent coins), and one-dollar coins (100-cent coins). Query the user separately for the number of each size coin, e.g., “How many pennies do you have?” Then your program should print out something like this: Click here to view code image
You have 23 pennies. You have 17 nickels. You have 14 dimes. You have 7 quarters. You have 3 half dollars. The value of all of your coins is 573 cents. Make some improvements: if only one of a coin is reported, make the output grammatically correct, e.g., 14 dimes and 1 dime (not 1 dimes). Also, report the sum in dollars and cents, i.e., $5.73 instead of 573 cents.
Now, I have the program working, but there has got to be a better way to make this program. Even with the little I know. Everything I write just seems so long, and piled with if statements. Any advice would be appreciated.
My Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
#include "../../../std_lib_facilities.h"
int main()
{
double penny, nickel, dime, quarter, half, full;
cout << "How many pennies do you have?\n";
cin >> penny;
cout << "How many nickels do you have?\n";
cin >> nickel;
cout << "How many dimes do you have?\n";
cin >> dime;
cout << "How many quarters do you have?\n";
cin >> quarter;
cout << "How many half-dollars do you have?\n";
cin >> half;
cout << "How many one-dollar coins do you have?\n";
cin >> full;
if (penny == 1)
{
cout << "You have " << penny << " penny.\n";
}
else
{
cout << "You have " << penny << " pennies.\n";
}
if (nickel == 1)
{
cout << "You have " << nickel << " nickel.\n";
}
else
{
cout << "You have " << nickel << " nickels.\n";
}
if (dime == 1)
{
cout << "You have " << dime << " dime.\n";
}
else
{
cout << "You have " << dime << " dimes.\n";
}
if (quarter == 1)
{
cout << "You have " << quarter << " quarter.\n";
}
else
{
cout << "You have " << quarter << " quarters.\n";
}
if (half == 1)
{
cout << "You have " << half << " half-dollar coin.\n";
}
else
{
cout << "You have " << half << " half-dollar coins.\n";
}
if (full == 1)
{
cout << "You have " << full << " full-dollar coin.\n";
}
else
{
cout << "You have " << full << " full-dollar coins.\n";
}
cout << "You're total is: " << '$' << penny * .01 + nickel * .05 + dime * .1 + quarter * .25
+ half * .5 + full * 1 << ".\n";
}
|