/*
* Adds up the coins. Enter each one as an optional integer followed by a
* a letter giving the type of coin: p, n, d, q, h. Enter s (for stop) to end.
*/
#include <iostream>
using namespace std;
int main()
{
int total = 0; // Total value of the coins.
// Go until we stop with a break.
while(true) {
// Read in the count. If there is no count, this will fail.
// If it fails at the end-of-file (not more input), then stop.
// Otherwise, assume the count was just omitted and set it to 1
// and clear the error so we can continue to use the input
// stream.
int count;
cin >> count;
if(cin.eof())
break;
else if(!cin.good()) {
count = 1;
cin.clear();
}
// Read the character for the coin type.
char coin_type;
cin >> coin_type;
// If we got a 's', or if the read failed, enough of this.
if(!cin.good() || coin_type == 's') break;
// If the count is less than one, the user needs to try
// again.
if(count < 1) {
cout << "Count is non-positive. Try again." << endl;
continue;
}
// Convert the coin type to its value.
int value;
switch(coin_type)
{
case 'p':
value = 1;
break;
case 'n':
value = 5;
break;
case 'd':
value = 10;
break;
case 'q':
value = 25;
break;
case 'h':
value = 50;
break;
default:
cout << "Unknown coin type " << coin_type << endl;
continue;
}
// Add to the total.
total += count*value;
}
// Divide the total into dollars and cents.
int dollars = total / 100;
int cents = total % 100;
// Print the total as dollars and cents, trying to make a human-like
// message.
cout << "You have ";
if(total == 0)
cout << "nothing. Sorry about that";
else {
if(dollars > 0) {
cout << dollars << " dollar";
if(dollars > 1) cout << "s";
if(cents == 0)
cout << " even";
else
cout << " and ";
}
if(cents > 0) {
cout << cents << " cent";
if(cents > 1) cout << "s";
}
}
cout << "." << endl;
}