You are here:

C++/Validate user input and pause display

Advertisement


Question
The user should not be able to enter any that is not numeric for the loan amount. If the user does the program should prompt the user for a valid amount and not continue until the numeric amount is entered.
I really appreciate your help I have been at this for two weeks now.
Thank you
-------------------------

Followup To

Question -
I having trouble trying to figure out how to validating user input and pause the program to display every ten payment amounts.
Here is the code

#include <cmath>
#include <iostream>
#include <iomanip>
#include <limits>

using namespace std; //needed in order to use the cout command

//define user input

double principle;
double term[] = {7, 15, 30};
double rate[] = {4.75, 5.5, 7.25};
double r;
double te;
int choice;

//define items for calculations
double t;
double periods;
double payment;
double mRate;
double conversion;
const double months = 12;
int paymentNumber;
double bal;
int arraySelectTerm;
int arraySelectRate;


int main() {

do
{
cout << "Enter the amount of the mortgage in US dollars " << endl;
cin >> principle;
cout << "Select the loan terms " << endl;
cout << "0 - " << term[0] << " years and " << rate[0] << "%" << endl;
cout << "1 - " << term[1] << " years and " << rate[1] << "%" << endl;
cout << "2 - " << term[2] << " years and " << rate[2] << "%" << endl;
cin >> arraySelectTerm;
if (arraySelectTerm = 0) {
 te = term[0];
 r = rate[0];
}
if (arraySelectTerm = 1) {
 te = term[1];
 r = rate[1];
}
if (arraySelectTerm = 2) {
 te = term[2];
 r = rate[2];
}
 
// Set output to display only 2 decimal places
cout << fixed << showpoint;
cout << setprecision(2);  

// Output to be displayed
cout << "Mortgage amount: $" << principle << endl; //output amount
cout << "Mortgage term in years: " << te << endl; //output length of loan
cout << "Mortgage interest rate: " << r << endl; //output interest rate
cout << endl;
cout << endl;

// Convert to monthly interest rate
mRate = (r * .01) / months;

// Convert years to months
periods = te * months;

// Create variable for (1 + rate)periods
t = pow(1 + mRate, periods);

// Calculate payment
payment= (principle * t * mRate)/(t - 1);

// Output Monthy payment
cout << "Monthly payment is $" << payment << endl;

// Output Amortization
cout << "Loan Balance      Interest Paid     Principle Payment" << endl;
cout << "-----------------------------------------------------" << endl;
bal = principle; //initialize outside the loop
for (paymentNumber = 1; paymentNumber <= periods; ++paymentNumber) {
   
cout << bal << "          ";
cout << bal *mRate << "          "; //interest payment
cout << payment - bal*mRate << endl; //principle payment
bal -= payment - bal*mRate; //subtract principle payment from bal
 //add loop

}

cout << "Would you like to run the mortgage calculator?" << endl;
cout << "1 for yes" << endl; //gives user choice
cout << "2 for no" << endl; //gives user choice
cin >> choice;



} // End of do
while (choice==1);

} // End of main


Answer -
Here the problem is solved for Question 2.

But i don't know what to validate with the user request  , ( that is your question is not clearly stated! )

Here with i attached the code ( With modification  , hope it will satisfy your request )


NOTE :
     If possible kindly tell me what to validate iwth te user input!


#include <cmath>
#include <iostream>
#include <iomanip>
#include <limits>



//define user input

double principle;
double term[] = {7, 15, 30};
double rate[] = {4.75, 5.5, 7.25};
double r;
double te;
int choice;

//define items for calculations
double t;
double periods;
double payment;
double mRate;
double conversion;
const double months = 12;
int paymentNumber;
double bal;
int arraySelectTerm;
int arraySelectRate;


void  main() {

do
{
cout << "Enter the amount of the mortgage in US dollars " << endl;
cin >> principle;
cout << "Select the loan terms " << endl;
cout << "0 - " << term[0] << " years and " << rate[0] << "%" << endl;
cout << "1 - " << term[1] << " years and " << rate[1] << "%" << endl;
cout << "2 - " << term[2] << " years and " << rate[2] << "%" << endl;
cin >> arraySelectTerm;
if (arraySelectTerm == 0) {
te = term[0];
r = rate[0];
}
if (arraySelectTerm == 1) {
te = term[1];
r = rate[1];
}
if (arraySelectTerm == 2) {
te = term[2];
r = rate[2];
}



// Output to be displayed
cout << "Mortgage amount: $" << principle << endl; //output amount
cout << "Mortgage term in years: " << te << endl; //output length of loan
cout << "Mortgage interest rate: " << r << endl; //output interest rate
cout << endl;
cout << endl;

// Convert to monthly interest rate
mRate = (r * .01) / months;

// Convert years to months
periods = te * months;

// Create variable for (1 + rate)periods
t = pow(1 + mRate, periods);

// Calculate payment
payment= (principle * t * mRate)/(t - 1);

// Output Monthy payment
cout << "Monthly payment is $" << payment << endl;

// Output Amortization
cout << "Loan Balance      Interest Paid     Principle Payment" << endl;
cout << "-----------------------------------------------------" << endl;
bal = principle; //initialize outside the loop
int count = 0;
for (paymentNumber = 1; paymentNumber <= periods; ++paymentNumber , count++)
{

 cout << bal << "          ";
 cout << bal *mRate << "          "; //interest payment
 cout << payment - bal*mRate << endl; //principle payment
 bal -= payment - bal*mRate; //subtract principle payment from bal
 if( count == 10)
 {
   cin.get();
   cout<<endl;
   cout << "Loan Balance      Interest Paid     Principle Payment" << endl;
   cout << "-----------------------------------------------------" << endl;

   count = 0;
 }


}

cout << "Would you like to run the mortgage calculator?" << endl;
cout << "1 for yes" << endl; //gives user choice
cout << "2 for no" << endl; //gives user choice
cin >> choice;



} // End of do
while (choice==1);

} // End of main

Thank you !


Answer
Here is your complete program.


Validation in user input is much simpler !

Just get the input in a charecter array
2. Convert the input value into float using atof() function.
3. If its a valid input the function return float value.
  else it will return 0;

Usin this result you can validate the user input !

HERE IS THE CODE !



#include <cmath>
#include <iostream>
#include <iomanip>
#include <limits>



double principle;
double term[] = {7, 15, 30};
double rate[] = {4.75, 5.5, 7.25};
double r;
double te;
int choice;
int error = 1;
//define items for calculations
double t;
double periods;
double payment;
double mRate;
double conversion;
const double months = 12;
int paymentNumber;
double bal;
int arraySelectTerm;
int arraySelectRate;
char amount[20]; /* here is the change */

void  main() {

do
{

     cout << "Enter the amount of the mortgage in US dollars " << endl;
     cin >> amount;
     // hrere is the change .
  do{
     principle = atof(amount);
     if( principle == 0)
     {
     cout<<"Enter valid input:";
     cin>>amount;
     }
     else
     error = 0;
  }while( error == 1);

      // end of change.

  cout << "Select the loan terms " << endl;
  cout << "0 - " << term[0] << " years and " << rate[0] << "%" << endl;
  cout << "1 - " << term[1] << " years and " << rate[1] << "%" << endl;
  cout << "2 - " << term[2] << " years and " << rate[2] << "%" << endl;
  cin >> arraySelectTerm;
if (arraySelectTerm == 0) {
te = term[0];
r = rate[0];
}
if (arraySelectTerm == 1) {
te = term[1];
r = rate[1];
}
if (arraySelectTerm == 2) {
te = term[2];
r = rate[2];
}



// Output to be displayed
cout << "Mortgage amount: $" << principle << endl; //output amount
cout << "Mortgage term in years: " << te << endl; //output length of loan
cout << "Mortgage interest rate: " << r << endl; //output interest rate
cout << endl;
cout << endl;

// Convert to monthly interest rate
mRate = (r * .01) / months;

// Convert years to months
periods = te * months;

// Create variable for (1 + rate)periods
t = pow(1 + mRate, periods);

// Calculate payment
payment= (principle * t * mRate)/(t - 1);

// Output Monthy payment
cout << "Monthly payment is $" << payment << endl;

// Output Amortization
cout << "Loan Balance      Interest Paid     Principle Payment" << endl;
cout << "-----------------------------------------------------" << endl;
bal = principle; //initialize outside the loop
int count = 0;
for (paymentNumber = 1; paymentNumber <= periods; ++paymentNumber , count++)
{

 cout << bal << "          ";
 cout << bal *mRate << "          "; //interest payment
 cout << payment - bal*mRate << endl; //principle payment
 bal -= payment - bal*mRate; //subtract principle payment from bal
 if( count == 10)
 {
   cin.get();
   cout<<endl;
   cout << "Loan Balance      Interest Paid     Principle Payment" << endl;
   cout << "-----------------------------------------------------" << endl;

   count = 0;
 }


}

cout << "Would you like to run the mortgage calculator?" << endl;
cout << "1 for yes" << endl; //gives user choice
cout << "2 for no" << endl; //gives user choice
cin >> choice;



} // End of do
while (choice==1);

} // End of main


NOTE : The function atof() Available in MATH Library!

THANK YOU!

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Prince M. Premnath

Expertise

Years of research in C/C++ Will bring you all facts regarding , OOPS , SVGA , MOUSE , BIOS , IVT , Feel free to query me :-)

Experience

More than 5 years

Education/Credentials
MCA

©2016 About.com. All rights reserved.