#include #include //for the "i/o manipulators" setw and setprecision #include using namespace std; int main() { cout << "How many dollars is the principal? "; double principal {0.00}; cin >> principal; if (!cin) { cerr << "Sorry, that wasn't a number.\n"; return EXIT_FAILURE; } if (principal <= 0) { cerr << "Sorry, the principal must be positive.\n"; return EXIT_FAILURE; } cout << "For how many years? "; int nyears {0}; cin >> nyears; if (!cin) { cerr << "Sorry, that wasn't a number.\n"; return EXIT_FAILURE; } if (nyears <= 0) { cerr << "Sorry, the number of years must be positive.\n"; return EXIT_FAILURE; } cout << "What is the annual interest rate in percent (e.g., 6)? "; double rate {0.00}; cin >> rate; if (!cin) { cerr << "Sorry, that wasn't a number.\n"; return EXIT_FAILURE; } if (rate <= 0) { cerr << "Sorry, the interest rate must be positive.\n"; return EXIT_FAILURE; } double factor {1.00 + .01 * rate}; cout << "year principal\n" //column headings << "---- ---------\n"; for (int year {1}; year <= nyears; ++year) { principal *= factor; //means principal = principal * factor cout << " " setw(2) << year << " $" << fixed << setprecision(2) << principal << "\n"; } return EXIT_SUCCESS; }