Ask for a value again if user does not enter anything

Posted on 2007-09-30
Last Modified: 2010-04-01
I have created the mortgage calculatir below. Everything works fine, except if the user does not enter a value and hits enter, my while statement does not catch it. How can I code so that if the user does not enter a value, I ask for it again as it works if they enter a 0.


/*  This program will accept the loan amount, The term of the loan, and the
interest rate and display the monthly payment to the screen. The user
will then be offered the choice of running another loan or quitting.
@author Jon Schab
@version 2.0
Changes for this version - Program altered to accept loan terms rather
than having them hard coded as in the previous version.      

//#include <iostream>
#include <cmath>
#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <stdlib.h>
#include <math.h>

using namespace std;

int main()

  //declare variables

  double loanAmount; // loan amount
  double interestRate; // interest rate
  double interestRateMonthly; // monthly interest rate
  int termYears; // term of loan in years
  int termMonths; // term in months
  double monthlyPayment; // monthly payment
  char again = 'N'; // NOT char again = Y;

  // Loop to allow the user another calculation      
        // reset fields for anpother mortgage
            loanAmount = 0.00;
            termYears = 0;
            interestRate = 0.00;

    //display questions and recieve input from user.
      cout << endl; // add blank line before title.
    cout << "\t\t\tJon's Mortgage Calculator" << endl << endl; // Calculator Title
      while(loanAmount <= 0.00) //check for positive value
      cout << "\n\n \t\t\t Loan amount: $"; // ask user for loan amount
      cin >> loanAmount;


      while(termYears <= 0) //check for positive value
    cout << "\n      \n \t\t\t Loan Term In Years: "; // ask user for loan term
    cin >> termYears; // capture loan term
      while(interestRate <= 0) //check for positive value
      cout << "\n\n \t\t\t Yearly Interest Rate: "; // ask user for interest rate
    cin >> interestRate; // capture interest rate

    //calculate term in years and monthly interest rate
    interestRateMonthly = ((interestRate / 12) / 100);
    termMonths = (termYears *12);

    //calculate mothly payment
    monthlyPayment = (loanAmount *interestRateMonthly) / (1 - pow(1.0 / (1.0 +
      interestRateMonthly), termMonths));

    //display terms and monthly payment to the screen.
    cout << "\n\n \t\t\t Monthly Payment $" << monthlyPayment << endl << endl;

    // Offer the user another calculation
    cout << (
      "\n\n\t Enter Y or y to calculate another mortgage, any other key to quit  ");
    again = _getch(); // capture response for another calculation                                          
    system("cls"); // clear the screen
  while (again == 'Y' || again == 'y'); // End of loop for another go.

  return 0;
Question by:ghost8067
    1 Comment
    LVL 13

    Accepted Solution

    This is really a cin issue.  Used in this form
          cin >> loanAmount;
    cin will not return until the user enters a value.  I'm not aware of any simple way to change this behavior.

    The best simple solution I'm aware of is something like this
    where buffer might be
          char buffer[256];


    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
    In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
    The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now