#include #include //for the "i/o manipulators" setw and setprecision #include using namespace std; //Interest compounded annually. int main() { cout << "Please type the principal in dollars: "; double principal {0.00}; //start with this many dollars cin >> principal; if (!cin) { cerr << "Could not perform input.\n"; return EXIT_FAILURE; } if (principal <= 0.00) { cerr << "Sorry, principal must be positive.\n"; return EXIT_FAILURE; } cout << "For how many years are you investing? "; int nyears {0}; cin >> nyears; if (!cin) { cerr << "Could not perform input.\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.0}; cin >> rate; if (!cin) { cerr << "Could not perform input.\n"; return EXIT_FAILURE; } if (rate <= 0) { cerr << "Sorry, the interest rate must be positive.\n"; return EXIT_FAILURE; } //Each year, multiply the principal by the following factor. //For example, if rate is 6, then factor will be 1.06 const 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; }