I have to find all the errors to make this program run in C++: #include // We wi
ID: 3528240 • Letter: I
Question
I have to find all the errors to make this program run in C++: #include // We will use cents for all monetary values. This will let us // work with integer, rather than floating-point, variables. const int TWINKIE_PRICE = 350; // Prompt the user to insert coins until enough has been paid to buy // a twinkie. The total amount inserted, in cents, is returned. int accepts_money(); // Returns the amount of change that should be returned to the user. int compute_change(int total_paid); int main() { using namespace std; // Declare variables for the amount of money that the user enters, // along with the change that is to be returned to them. int money_entered, change; // Make sure that monetary values we output are formatted with // two digits after the decimal point. cout.setf(ios::fixed); cout.setf(ios::showpoints); cout.precision(2); // Collect money from the user monies_entered = accept_money(); // Figure out how much change to return change = compute_change(money_entered); // Dispense the twinkie cout << " Enjoy your deep-fried twinkie. Your change is $" << change/100.0 << endl; return 0; } // Prompt for coins until at least TWINKIE_PRICE has been // entered int accept_money() { using namespace std; int inserted = 0; char coins; do { cout << " Please insert coins: n - Nickel d - Dime " << " q - Quarter D - Dollar "; cout << "A fried twinkie costs $" << (TWINKIE_PRICE/100.0) << ". You have inserted $" << (inserted/100.0) << ". Next coin: "; cin >> coin; switch (coin) { case 'n': inserted += 5; break; case 'd': inserted += 10; break; case 'q': inserted += 25; break; case 'D': inserted += 100; break; default: cout << " '" << coin << "' is not recognized as a coin. "; } } while (inserted_once < TWINKIE_PRICES); return inserted; } int compute_change(int total_paid_out) { return (total_paid - TWINKIE_PRICE); }Explanation / Answer
/** i found all the errors...this is the corrected code and it's working****/
#include <iostream>
// We will use cents for all monetary values. This will let us
// work with integer, rather than floating-point, variables.
const int TWINKIE_PRICE = 350;
// Prompt the user to insert coins until enough has been paid to buy
// a twinkie. The total amount inserted, in cents, is returned.
int accept_money();
// Returns the amount of change that should be returned to the user.
int compute_change(int total_paid);
int main() {
using namespace std;
// Declare variables for the amount of money that the user enters,
// along with the change that is to be returned to them.
int money_entered, change;
// Make sure that monetary values we output are formatted with
// two digits after the decimal point.
cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2);
// Collect money from the user
money_entered = accept_money();
// Figure out how much change to return
change = compute_change(money_entered);
// Dispense the twinkie
cout << " Enjoy your deep-fried twinkie. Your change is $" << change/100.0 << endl;
return 0;
}
// Prompt for coins until at least TWINKIE_PRICE has been
// entered
int accept_money() {
using namespace std;
int inserted = 0; char coin;
do {
cout << " Please insert coins: n - Nickel d - Dime " << " q - Quarter D - Dollar ";
cout << "A fried twinkie costs $" << (TWINKIE_PRICE/100.0) << ". You have inserted $" << (inserted/100.0) << ". Next coin: ";
cin >> coin;
switch (coin) {
case 'n': inserted += 5;
break;
case 'd': inserted += 10; break;
case 'q': inserted += 25; break;
case 'D': inserted += 100; break;
default: cout << " '" << coin << "' is not recognized as a coin. ";
}
} while (inserted < TWINKIE_PRICE);
return inserted;
}
int compute_change(int total_paid_out) { return (total_paid_out - TWINKIE_PRICE); }