Ask your own question, for FREE!
Computer Science 16 Online
OpenStudy (kikuo):

Mind showing me what I did wrong? http://prntscr.com/bjcjv6

OpenStudy (kikuo):

//Ashton Dreiling //Money exercise #include <iostream> #include <stdlib.h> using namespace std; //Module prototypes void calculateTheValueOfPensNicksDimesQuarts (double pennies, double nickles, double dimes, double quaters, double &valueOfPennies, double &valueOfNickles, double &valueOfDimes, double &valueOfQuarters); //Global constants that will be used to calculate the total amount the user has after they input values const double COSTOFPENNY= .01; const double COSTOFNICKLE = .05; const double COSTOFDIME = .10; const double COSTOFQUARTER = .25; const double ADOLLAR = 1.00; int main() { //some variables to hold money values double pennies; double nickles; double dimes; double quarters; double valueOfPennies; double valueOfNickles; double valueOfDimes; double valueOfQuarters; double total; cout << "Hello, the point of this program is to make no more than one dollar. Let's see if you can do it." << endl; cout << "Please enter the number of pennies you have." << endl; cin >> pennies; cout << "You said you had " << pennies << endl; cout << "Please enter the number of nickles you have." << endl; cin >> nickles; cout << "You said you had " << nickles << endl; cout << "Please enter the number of dimes you have." << endl; cin >> dimes; cout << "You said you had " << dimes << endl; cout << "Please enter the number of quarters you have." << endl; cin >> quarters; cout << "You said you had " << quarters << endl; cout << "We will now calculate if you made a dollar or went over a dollar." << endl; //passvariables to calculateTheValueOfPensNicksDimesQuarts calculateTheValueOfPensNicksDimesQuarts (pennies, nickles, dimes, quarters, valueOfPennies, valueOfNickles, valueOfDimes, valueOfQuarters); //final amount to compare in an if-then statement to see if they went over a dollar of if they met exactly one dollar total = valueOfPennies + valueOfNickles + valueOfDimes + valueOfQuarters; cout << "Your total is " << total << endl; if (total == ADOLLAR) cout << "Congratulations! You did it!" << endl; else { if (total < ADOLLAR) cout << "Sorry, you had less than a dollar" << endl; else { if (total > ADOLLAR) cout << "Sorry, you went over a dollar." endl; else } return 0; system("Pause"); }//end main void calculateTheValueOfPensNicksDimesQuarts(double pennies, double nickles, double dimes, double quarters, double &valueOfPennies, double &valueOfNickles, double &valueOfDimes, double &valueOfQuarters) { valueOfPennies = pennies * COSTOFPENNY; valueOfNickles = nickles * COSTOFNICKLE; valueOfDimes = dimes * COSTOFDIME; valueOfQuarters = quarters * COSTOFQUARTER; }//end of calculateTheValueOfPensNicksDimesQuarts module

OpenStudy (kikuo):

@rsmith6559

OpenStudy (kikuo):

This is C++ by the way

OpenStudy (kikuo):

I'm also aware you and do if-then-else statements without brackets, but I decided I'd do mine with brackets since I'd like to know what I'm doing wrong.

OpenStudy (rsmith6559):

You've got an interesting set of variable scopes. It would be simpler if you had a smaller calculate function: double calculate( double howMany, double cost ) { return( howMany * cost; } This could be called by, say: valueOfPennies = calculate( pennies, COSTOFPENNY ); That would keep all your valueOf variables totally within the main function. BTW, do you really want partial pennies, nickels, dimes and quarters? IMO, int's would be better for those. You could get rid of all your valueOf variables by doing: total = calculate( pennies, COSTOFPENNY ) + calculate( nickels, COSTOFNICKLE ) + calculate( dimes, COSTOFDIME ) + calculate( quarters, COSOFQUARTER ); You may want to consider: if( ) else if( ) else statement. And just for clarity: a one line block of code doesn't need braces in an if/else statement. Multiple lines of code require braces.

OpenStudy (kikuo):

@rsmith6559 I'll change it to int. As for what you're saying about a simpler code, you'd have to show me side-to-side comparisons with your edits if I were to understand.

OpenStudy (kikuo):

But that still didn't answer my questions haha : )

OpenStudy (kikuo):

Never mind I found it. Though, I am interested in your simpler code idea. This is a beginners class, so I believe my instructor is purposely trying to make us be a little bit more lengthy to make sure we understand.

OpenStudy (rsmith6559):

The calculate function is very complex. It's header is huge, it's hard to call and just basically does too much. The calculate function that I posted last night, and the statement for total is all that's needed besides your cout, cin and if/else for the program. You also wouldn't need any of the valueOf* variables. If that "total=" makes you uncomfortable, remember that C and C derived languages are free form. You can space things any way you want. It will just keep adding until it finds the semi-colon to terminate the line. Here's a different version: ``` #include <iostream> #include <stdlib.h> using namespace std; // Global constants that will be used to calculate the total amount the user has after they input values const double COSTOFPENNY= .01; const double COSTOFNICKLE = .05; const double COSTOFDIME = .10; const double COSTOFQUARTER = .25; const double ADOLLAR = 1.00; double calculate( int num, double value ) { return( num * value ); } int main() { int pennies, nickles, dimes, quarters; double total; cout << "Hello, the point of this program is to make no more than one dollar. Let's see if you can do it." << endl; cout << "Please enter the number of pennies you have." << endl; cin >> pennies; cout << "You said you had " << pennies << endl; cout << "Please enter the number of nickles you have." << endl; cin >> nickles; cout << "You said you had " << nickles << endl; cout << "Please enter the number of dimes you have." << endl; cin >> dimes; cout << "You said you had " << dimes << endl; cout << "Please enter the number of quarters you have." << endl; cin >> quarters; cout << "You said you had " << quarters << endl; cout << "We will now calculate if you made a dollar or went over a dollar." << endl; total = calculate( pennies, COSTOFPENNY )\ + calculate( nickles, COSTOFNICKLE )\ + calculate( dimes, COSTOFDIME )\ + calculate( quarters, COSTOFQUARTER ); cout << "Your total is " << total << endl; if( total == ADOLLAR ) cout << "Congratulations! You did it!" << endl; else if( total < ADOLLAR ) cout << "Sorry, you had less than a dollar" << endl; else cout << "Sorry, you went over a dollar." << endl; return( 0 ); } ``` BTW, I used g++ on OSX.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!