#include #include // For i/o manipulators like setw and setprecision #include // For EXIT_SUCCESS and EXIT_FAILURE using namespace std; // Function to check if input is valid bool validate_input() { if (!cin) { cerr << "Invalid input. Please enter numeric values only.\n"; cin.clear(); // Clear the error flag cin.ignore(1000, '\n'); // Discard invalid input return false; } return true; } int main() { double principal {0.0}; // User-provided initial investment int years {0}; // User-provided number of years double rate {0.0}; // User-provided annual interest rate (in percent) // Input for initial investment cout << "Please enter the initial investment amount: $"; cin >> principal; while (!validate_input()) { cout << "Please enter the initial investment amount: $"; cin >> principal; } // Input for number of years cout << "Please enter the number of years: "; cin >> years; while (!validate_input()) { cout << "Please enter the number of years: "; cin >> years; } // Input for annual interest rate cout << "Please enter the annual interest rate (in percent): "; cin >> rate; while (!validate_input()) { cout << "Please enter the annual interest rate (in percent): "; cin >> rate; } // Convert rate from percentage to a decimal (e.g., 6% to 0.06) rate /= 100; // Output the column headings cout << "\nYear Principal\n" << "---- ---------\n"; // Perform the compound interest calculation for each year for (int year {1}; year <= years; ++year) { principal *= (1 + rate); // Compound interest formula cout << setw(4) << year << " $" << fixed << setprecision(2) << principal << "\n"; } return EXIT_SUCCESS; }