Help possibly cleaning up code

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";


}
Bjarne Stroustrup is the inventor of C++. Though, I don't think his presentation style is very beginner-friendly...

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.


With what you know right now, I'd say this is pretty close to the level you should be at.

As you learn more concepts, you'll find ways to write the same program in just a couple lines.
I could see arrays, loops and strings being useful to you. Everyone writes bloated code at the very beginning of their programming journey, so you're not alone!

Also, I must say that I'm happy you think it looks too cluttered! Many beginners don't want to venture further than writing tons and tons of if statements for each and every possible code branch, and then they wonder why their tic-tac-toe or text adventure games are so hard to work with. Like I said, becoming comfortable with the features of the language will enable you to write data-driven code.
Last edited on
Xismn,

I had no idea he was the inventor! I feel a bit daft, I've been reading a book he helped write without realizing. Honestly, I think his writing is extremely easy to follow. It even seems a bit slow at times.

For example, the book is focused towards beginners and first time programmers. The second chapter takes about 10 pages explaining how to define a variable and what exactly is happening when you do it! Can be a bit dry, but it's really the pace I need to move at.

You made me feel a bit better knowing this is where I should technically be. I'm also glad to know there will be tools further down the road to clean this mess up. I just always felt programming was supposed to be elegant, and flow well. While writing this, I couldn't help but feel it was the exact opposite.
Topic archived. No new replies allowed.