Hi again all!! I am trying to write my program below to validate user input. In other words, if the user inputs something like "1a2.34", I need to return a message that states invalid data and for them to retry. I am really lost on this one, and I really need some help. Thanks so much!!
Code:
#include <iostream> #include <iomanip> #include <cmath> #include <cctype> #include <string> #include <sstream> using namespace std; int main() { double a; //This is the amount of the mortgage the user must enter double i; //This is the interest rate the user must enter int y; //This is the amount of years for the mortgage the user must enter double mPayment; //This is a variable for ouputting the payment char YesNo = 'Y'; do //In order to allow the user to be able to re-enter data, we must have a loop { cout << "What is the amount of the mortgage? For example 200000" << endl; cout << "Press enter." << endl; cin >> a; cout << endl; cout << "What is the amount of years the mortgage will be financed? For example 30" << endl; cout << "Press enter." << endl; cin >> y; cout << endl; cout << "What is the interest rate? For example 5.75" << endl; cout << "Press enter." << endl; cin >> i; cout << endl; //These are the variables required to calculate the information the user inputs double monInterest = i / 12 / 100; //Calcualtes the interest monthly int t = y * 12; //This is the loan term in the amount of months //This is the actual formula for calculating the mortgage payment amount mPayment = (a * monInterest) / (1-pow((1+monInterest),-t)); //This allows the user to view what they entered and also what the monthly payment would be cout << "Amount of mortgage = $" << a << endl; cout << "Year financed = " << i << "%" << endl; cout << "Interest Rate = " << y << " years" << endl; cout << endl; cout << "Monthly Payment Amount = $" << mPayment << endl; cout << endl; //This allows the user to either enter in new information, or exit the program cout << "If you would like to enter different information, please press Yes." << endl; cout << "If you would like to exit this program, please press No." << endl; cout << endl; cin >> YesNo; } while ((YesNo == 'Y') || (YesNo == 'y')); //End of the loop return 0; }
Comment