There still this bug that when I enter a character like 'a' as "dollar" or "cents" it'll go on forever. I don't know how to fix that.
- Code: Select all
#include <iostream>
using namespace std;
// Canadian money
int dollar; // Amount in dollars
int cents; // amount in cents
int five; // amount of five dollar bills
int ten; // amount of ten dollar bills
int twenty; // amount of twenty dollar bills
int fifty; // amount of fifty dollar bills
int hundred; // amount of hundred dollar bills
int toonies; // amount of toonies
int loonies; // amount of loonies
int quarters; // amount of quarters
int dimes; // amount of dimes
int nickels; // amount of nickels
int pennies; // amount of pennies
char sign;
int main()
{
while (true) {
cout << "Enter amount in dollars and cents"; // Put $ sign in front of dollar separated by space
cout << " or type 'q' to quit: ";
cin >> sign >> dollar >> cents; // Gets the values of "sign", "dollar" and "cents"
cin.ignore();
hundred = dollar / 100;
fifty = (dollar % 100) / 50;
twenty = ((dollar % 100) % 50) / 20;
ten = (((dollar % 100) % 50) % 20) / 10;
five = ((((dollar % 100) % 50) % 20) % 10) / 5;
toonies = (((((dollar % 100) % 50) % 20) % 10) % 5) / 2;
loonies = (((((dollar % 100) % 50) % 20) % 10) % 5) % 2;
quarters = cents / 25;
dimes = (cents % 25) / 10;
nickels = ((cents % 25) % 10) / 5;
pennies = ((cents % 25) % 10) % 5;
if ((sign == '$') && (dollar >= 0) && (cents >= 0)) {
cout << '\n' << dollar << " dollars and " << cents << " cents = \n";
cout << hundred << " hundred dollar bills\n";
cout << fifty << " fifty dollar bills\n";
cout << twenty << " twenty dollar bills\n";
cout << ten << " ten dollar bills\n";
cout << five << " five dollar bills\n";
cout << toonies << " toonies\n";
cout << loonies << " loonies\n";
cout << quarters << " quarters\n";
cout << dimes << " dimes\n";
cout << nickels << " nickels\n";
cout << pennies << " pennies\n\n";
cout << "------------------------------------------------------\n\n";
}
else if ((sign == 'q') || (sign == 'Q')) {
break;
}
else if (sign != '$') {
cout << '\n' << "Not a valid sign, please try again\n\n";
cout << "-------------------------------------------------\n\n";
}
else {
break;
}
}
}
Thanks for any help


