Jump to content

[C++] Principles and Practice C3E11 - Integers get set to 1 regardless of entered value.

Norco

My code: http://pastebin.com/vrr6j2AB

I have to write a program to calculate the total value from the number of coins entered whilst being gramatically correct. i.e. "1 quarters" isn't gramatically correct and instead needs to be "1 quarter".

1 penny
1 quarter
1 dollar

total: $1.26

The problem I'm experiencing is that no matter what values I enter, I have 1 of each coin.  Removing the block of 'if' statements fixes this but then the output isn't gramatically correct.

std_lib_facilities.h is available here if you want to compile it: http://www.stroustrup.com/Programming/std_lib_facilities.h

 

Link to comment
Share on other sites

Link to post
Share on other sites

Never mind got it, used = instead of ==

Link to comment
Share on other sites

Link to post
Share on other sites

glad you figured that out
i took a look at you code it looks nice an clean but have you guys learned *if, else if *?
just an idea to make it look cleaner you can say something like

you can use the logical operators (&& and) (|| or)

if (penny == 1 && nickel ==1 )
if (penny == 1 || nickel ==1 )


as for if and else if
it should go like this

if (this....)
else if (this....)
else if (this...)
else




side note this might not work it really depends on what you are trying to do though
also something else i noticed is that you forgot to #include <iostream> for input and output unless you want to std:: all the way.. however i dont recommend you do that with c++11 anyway also #include <string>
unless you fixed it, i wouldnt know.

its always good to know a bit ahead buddy good luck with your homework

Link to comment
Share on other sites

Link to post
Share on other sites

glad you figured that out

i took a look at you code it looks nice an clean but have you guys learned *if, else if *?

just an idea to make it look cleaner you can say something like

you can use the logical operators (&& and) (|| or)

if (penny == 1 && nickel ==1 )

if (penny == 1 || nickel ==1 )

as for if and else if

it should go like this

if (this....)

else if (this....)

else if (this...)

else

side note this might not work it really depends on what you are trying to do though

also something else i noticed is that you forgot to #include <iostream> for input and output unless you want to std:: all the way.. however i dont recommend you do that with c++11 anyway also #include <string>

unless you fixed it, i wouldnt know.

its always good to know a bit ahead buddy good luck with your homework

 

Stroutrup includes that in his custom header file that he uses throughout the book's examples.

Link to comment
Share on other sites

Link to post
Share on other sites

I don't know if it helps you at all, but you can use the 'ternary' operator to neaten the code up significantly.

The syntax below is intended for Java, but it should be fairly easy to convert it to C++.

int pennies = 1; //Set pennies to 1string money = pennies + "penn"; //Convert the int to a string and add 'penn' to the end.money += (pennies > 1) ? "ies" : "y"; //If pennies is greater than 1, add 'ies' to the end of the string to make it 'pennies' otherwise add 'y' to make it 'penny'.//At this stage, the money variable is set to "1 penny".pennies = 2; //Make pennies = 2money = pennies + "penn"; //Set the string to "2 penn".money += (pennies > 1) ? "ies" : "y"; //Do the same as before to pluralise if nessesary.//At this stage, money is set to "2 pennies".

Essentially, the code before the ? is an if statement and the code either side of the : is the code executed if the statement is true or false (the code on the left of the : is executed if true, code on the right is executed if false)

Link to comment
Share on other sites

Link to post
Share on other sites

It would have made more sense to use switch statements but I think we're assuming he's working through the examples so shortened if statements and switch statements may be a little ahead.

Link to comment
Share on other sites

Link to post
Share on other sites

It would have made more sense to use switch statements but I think we're assuming he's working through the examples so shortened if statements and switch statements may be a little ahead.

 

Fair point. I thought I'd post it anyway because I ran into this problem a few weeks ago when I had to pluralise things and code tidiness is something I obsess over. :p

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×