Problem with validation function and call to it in C++ mortgage calculator

I have been trying to call the get_int function in my program, but I get an error when I comment in the function I get a bunch of errors that look like

c:\documents and settings\administrator\my documents\visual studio 2005\projects\mortgage2func\mortgage2func\mortgage2func.cpp(119) : error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
        c:\program files\microsoft visual studio 8\vc\include\istream(1137): could be 'std::basic_istream<_Elem,_Traits> &std::operator >><std::char_traits<char>>(std::basic_istream<_Elem,_Traits> &,signed char *)'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>
        ]

I would like some help on this issue and calling this function if possible.
Thanks!
ghost8067Asked:
Who is Participating?
 
Infinity08Commented:
>> but the laon calculation comes out to 0.

Look at this line :

  return 0;
0
 
Infinity08Commented:
Did you #include <string> ?

If that doesn't help, can you show the code ?

Btw, it's better to use getline when inputting a string.
0
 
itsmeandnobodyelseCommented:
>>>> but I get an error when I comment in the function
What do you mean by that?

Can you post both the get_int function and mark the line where the error refers to?

Regards, Alex
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
itsmeandnobodyelseCommented:
>>>> C2679: binary '>>' :
>>>> no operator found  which takes a right-hand operand of type 'std::string'
That error means that either you forgot to include <string> *or* - more likely - the left operand isn't a input stream like 'cin' but an output stream (e. g. cout)

   cout >> str;  //  error C2679 cannot stream from cout

0
 
ghost8067Author Commented:
Thanks, adding the include got me past that issue. I commented in the code and tried to call it, but I now get the following errors.

c:\documents and settings\administrator\my documents\visual studio 2005\projects\mortgage2func\mortgage2func\mortgage2func.cpp(36) : error C3861: 'read_int': identifier not found
c:\documents and settings\administrator\my documents\visual studio 2005\projects\mortgage2func\mortgage2func\mortgage2func.cpp(116) : error C2365: 'read_int' : redefinition; previous definition was 'formerly unknown identifier'

Any assistance would be appreciated.

/*  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>
#include <string>

using namespace std;

void printtitle()
{
  //Print blank line followed by title
  cout << endl; // add blank line before title.
  cout << "\t\t\tJon's Mortgage Calculator" << endl << endl; // Calculator Title
}

double getloanamount()
{
  double l = 0.00;
  while (l <= 0.00)
  //check for positive value
  {
    cout << "\n\n \t\t\t Loan amount: $"; // ask user for loan amount
      read_int();
    cin >> l;

  }
  return l;
}

int getterm()
{
  int t = 0;
  while (t <= 0)
  //check for positive value
  {
    cout << "\n      \n \t\t\t Loan Term In Years: "; // ask user for loan term
    cin >> t; // capture loan term
  }
  return t;
}

double getinterestrate()
{
  double i = 0;
  while (i <= 0)
  //check for positive value
  {
    cout << "\n\n \t\t\t Yearly Interest Rate: "; // ask user for interest rate
    cin >> i; // capture interest rate
  }
  return i;
}

int calctermmonths(int a)
{
  int m;
  {
    m = (a *12);
  }
  return m;
}

double calcmonthlyrate(double a)
{
  double mr;
  {
    mr = ((a / 12) / 100);
  }
  return mr;
}

double calcmonthlypayment(double a, double b, int c)
{
  double payment;
  {
    payment = (a *b) / (1-pow(1.0 / (1.0 + b), c));

  }
  return payment;
}

double printpayment(double a)
{
  {
    cout << "\n\n \t\t\t Monthly Payment $" << a << endl << endl;
  }
  return a;
}

char offernewmortgage()
{
  char newmortgage = 'N';
  {
    cout << (
      "\n\n\t Enter Y or y to calculate another mortgage, any other key to quit  ");
    newmortgage = _getch(); // capture response for another calculation                                          
    system("cls"); // clear the screen

  }
  return newmortgage;
}

int read_int() {

  string s;
  int n = 0;
  char* pCnvEnd;

  while(1) {
    cin >> s;
    n = strtol(s.c_str(),&pCnvEnd,10);
    if (*pCnvEnd != NULL) { // invalid chars in string, not an integer
      cout << "Not a valid number, please retry" << endl;
    } else {
      break; //valid, stop loop
    }
  }

  return n;
}

int calcmortgage()
{
  //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;
  do
  {
    loanAmount = 0.00;
    termYears = 0;
    interestRate = 0.00;

    printtitle();
    loanAmount = getloanamount();
    termYears = getterm();
    interestRate = getinterestrate();
    termMonths = calctermmonths(termYears);
    interestRateMonthly = calcmonthlyrate(interestRate);
    monthlyPayment = calcmonthlypayment(loanAmount, interestRateMonthly,
      termMonths);
    printpayment(monthlyPayment);
    again = offernewmortgage();
  }
  while (again == 'Y' || again == 'y'); // End of loop for another go.



  system("PAUSE")
    ;
  return 0;
}



int main()
{
  int iret = calcmortgage();

  return iret;
 
}
0
 
itsmeandnobodyelseCommented:
>>>> error C2365: 'read_int' :
Either do th eimplementation of read_int above the first calling or make a prototype above:

int read_int();  // forward declaration

double getloanamount()
{
     ...
     int i = read_int();
0
 
Infinity08Commented:
You are calling read_int before it has been declared. Move the definition of the read_int function ABOVE the first call to it.
0
 
ghost8067Author Commented:
Thanks, it now works and detects if the user enters a non-numeric value. Right now, the way it is written, it does not ask for the value again. SHould I just call the function for get loan amount again? When it is finished, I want to use it for all 3 input variables.
Thanks

/*  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>
#include <string>

using namespace std;

void printtitle()
{
  //Print blank line followed by title
  cout << endl; // add blank line before title.
  cout << "\t\t\tJon's Mortgage Calculator" << endl << endl; // Calculator Title
}

int read_int() {

  string s;
  int n = 0;
  char* pCnvEnd;

  while(1) {
    cin >> s;
    n = strtol(s.c_str(),&pCnvEnd,10);
    if (*pCnvEnd != NULL) { // invalid chars in string, not an integer
      cout << "Not a valid number, please retry" << endl;
    } else {
      break; //valid, stop loop
    }
  }

  return n;
}


double getloanamount()
{
  double l = 0.00;
  while (l <= 0.00)
  //check for positive value
  {
    cout << "\n\n \t\t\t Loan amount: $"; // ask user for loan amount
      read_int();
    cin >> l;

  }
  return l;
}

int getterm()
{
  int t = 0;
  while (t <= 0)
  //check for positive value
  {
    cout << "\n      \n \t\t\t Loan Term In Years: "; // ask user for loan term
    cin >> t; // capture loan term
  }
  return t;
}

double getinterestrate()
{
  double i = 0;
  while (i <= 0)
  //check for positive value
  {
    cout << "\n\n \t\t\t Yearly Interest Rate: "; // ask user for interest rate
    cin >> i; // capture interest rate
  }
  return i;
}

int calctermmonths(int a)
{
  int m;
  {
    m = (a *12);
  }
  return m;
}

double calcmonthlyrate(double a)
{
  double mr;
  {
    mr = ((a / 12) / 100);
  }
  return mr;
}

double calcmonthlypayment(double a, double b, int c)
{
  double payment;
  {
    payment = (a *b) / (1-pow(1.0 / (1.0 + b), c));

  }
  return payment;
}

double printpayment(double a)
{
  {
    cout << "\n\n \t\t\t Monthly Payment $" << a << endl << endl;
  }
  return a;
}

char offernewmortgage()
{
  char newmortgage = 'N';
  {
    cout << (
      "\n\n\t Enter Y or y to calculate another mortgage, any other key to quit  ");
    newmortgage = _getch(); // capture response for another calculation                                          
    system("cls"); // clear the screen

  }
  return newmortgage;
}


int calcmortgage()
{
  //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;
  do
  {
    loanAmount = 0.00;
    termYears = 0;
    interestRate = 0.00;

    printtitle();
    loanAmount = getloanamount();
    termYears = getterm();
    interestRate = getinterestrate();
    termMonths = calctermmonths(termYears);
    interestRateMonthly = calcmonthlyrate(interestRate);
    monthlyPayment = calcmonthlypayment(loanAmount, interestRateMonthly,
      termMonths);
    printpayment(monthlyPayment);
    again = offernewmortgage();
  }
  while (again == 'Y' || again == 'y'); // End of loop for another go.



  system("PAUSE")
    ;
  return 0;
}



int main()
{
  int iret = calcmortgage();

  return iret;
 
}
0
 
Infinity08Commented:
What's the point of this :

      read_int();
      cin >> l;

?? read_int reads an integer value, but you discard it. And then you read a double value ... ???
0
 
ghost8067Author Commented:
I catch your meaning. I chnged the code and it now seems to accet a value, but the laon calculation comes out to 0.
Any tips?

/*  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>
#include <string>

using namespace std;

void printtitle()
{
  //Print blank line followed by title
  cout << endl; // add blank line before title.
  cout << "\t\t\tJon's Mortgage Calculator" << endl << endl; // Calculator Title
}

int read_int() {

  string s;
  int n = 0;
  char* pCnvEnd;

  while(1) {
    cin >> s;
    n = strtol(s.c_str(),&pCnvEnd,10);
    if (*pCnvEnd != NULL) { // invalid chars in string, not an integer
      cout << "Not a valid number, please retry" << endl;
    } else {
      break; //valid, stop loop
    }
  }

  return n;
}


double getloanamount()
{
  double l = 0.00;
  while (l <= 0.00)
  //check for positive value
  {
    cout << "\n\n \t\t\t Loan amount: $"; // ask user for loan amount
      l = read_int();
//    cin >> l;

  }
  return 0;
}

int getterm()
{
  int t = 0;
  while (t <= 0)
  //check for positive value
  {
    cout << "\n      \n \t\t\t Loan Term In Years: "; // ask user for loan term
    cin >> t; // capture loan term
  }
  return t;
}

double getinterestrate()
{
  double i = 0;
  while (i <= 0)
  //check for positive value
  {
    cout << "\n\n \t\t\t Yearly Interest Rate: "; // ask user for interest rate
    cin >> i; // capture interest rate
  }
  return i;
}

int calctermmonths(int a)
{
  int m;
  {
    m = (a *12);
  }
  return m;
}

double calcmonthlyrate(double a)
{
  double mr;
  {
    mr = ((a / 12) / 100);
  }
  return mr;
}

double calcmonthlypayment(double a, double b, int c)
{
  double payment;
  {
    payment = (a *b) / (1-pow(1.0 / (1.0 + b), c));

  }
  return payment;
}

double printpayment(double a)
{
  {
    cout << "\n\n \t\t\t Monthly Payment $" << a << endl << endl;
  }
  return a;
}

char offernewmortgage()
{
  char newmortgage = 'N';
  {
    cout << (
      "\n\n\t Enter Y or y to calculate another mortgage, any other key to quit  ");
    newmortgage = _getch(); // capture response for another calculation                                          
    system("cls"); // clear the screen

  }
  return newmortgage;
}


int calcmortgage()
{
  //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;
  do
  {
    loanAmount = 0.00;
    termYears = 0;
    interestRate = 0.00;

    printtitle();
    loanAmount = getloanamount();
    termYears = getterm();
    interestRate = getinterestrate();
    termMonths = calctermmonths(termYears);
    interestRateMonthly = calcmonthlyrate(interestRate);
    monthlyPayment = calcmonthlypayment(loanAmount, interestRateMonthly,
      termMonths);
    printpayment(monthlyPayment);
    again = offernewmortgage();
  }
  while (again == 'Y' || again == 'y'); // End of loop for another go.



  system("PAUSE")
    ;
  return 0;
}



int main()
{
  int iret = calcmortgage();

  return iret;
 
}

0
 
itsmeandnobodyelseCommented:
>>>> but the laon calculation comes out to 0.
the loanAmount is a double while read_int reads an integer? Is that intended?

>>>> mr = ((a / 12) / 100);
That seems wrong, why are dividing by 100 ?


>>>> payment = (a *b) / (1-pow(1.0 / (1.0 + b), c));
That is wrong either. The a argument is 0, hence the result is always 0 as well.

>>>> double getloanamount()

The last statement always returns 0. You must change to

   return l;
0
 
itsmeandnobodyelseCommented:
>>>> int read_int()
You should add an 'read_double()' as well and use it in getloanamount();
You should use the read_int() in getterm().
You should use the read_double() in getinterestrate().

0
 
ghost8067Author Commented:
I made some changes and the program works fine, calculates correctly, and validates the mortgage amount and term for each integer entered. For example, if a user enters 200000b or 200b00, it will detect the error and ask for the value again. If ossible, I would also like to validate the interest rate the user enters. The problem is, when I use the get_int function, it thinks the entry is invalid due to the decimal point as in 5.75 meaning 5.75%. Any tis on how to achieve this?
Thanks!

/*  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>
#include <string>

using namespace std;

void printtitle()
{
  //Print blank line followed by title
  cout << endl; // add blank line before title.
  cout << "\t\t\tJon's Mortgage Calculator" << endl << endl; // Calculator Title
}

int read_int() {

  string s;
  int n = 0;
  char* pCnvEnd;

  while(1) {
    cin >> s;
    n = strtol(s.c_str(),&pCnvEnd,10);
    if (*pCnvEnd != NULL) { // invalid chars in string, not an integer
      cout << "Not a valid number, please re-enter the value" << " ";
    } else {
      break; //valid, stop loop
    }
  }

  return n;
}


double getloanamount()
{
  double l = 0.00;
  while (l <= 0.00)
  //check for positive value
  {
    cout << "\n\n \t\t\t Loan amount: $"; // ask user for loan amount
      l = read_int();
  }
  return l;
}

int getterm()
{
  int t = 0;
  while (t <= 0)
  //check for positive value
  {
    cout << "\n      \n \t\t\t Loan Term In Years: "; // ask user for loan term
      t = read_int();
  }
  return t;
}

double getinterestrate()
{
  double i = 0;
  while (i <= 0)
  //check for positive value
  {
    cout << "\n\n \t\t\t Yearly Interest Rate: "; // ask user for interest rate
    cin >> i; // capture interest rate
  }
  return i;
}

int calctermmonths(int a)
{
  int m;
  {
    m = (a *12);
  }
  return m;
}

double calcmonthlyrate(double a)
{
  double mr;
  {
    mr = ((a / 12) / 100);
  }
  return mr;
}

double calcmonthlypayment(double a, double b, int c)
{
  double payment;
  {
    payment = (a *b) / (1-pow(1.0 / (1.0 + b), c));

  }
  return payment;
}

double printpayment(double a)
{
  {
    cout << "\n\n \t\t\t Monthly Payment $" << a << endl << endl;
  }
  return a;
}

char offernewmortgage()
{
  char newmortgage = 'N';
  {
    cout << (
      "\n\n\t Enter Y or y to calculate another mortgage, any other key to quit  ");
    newmortgage = _getch(); // capture response for another calculation                                          
    system("cls"); // clear the screen

  }
  return newmortgage;
}


int calcmortgage()
{
  //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;
  do
  {
    loanAmount = 0.00;
    termYears = 0;
    interestRate = 0.00;

    printtitle();
    loanAmount = getloanamount();
    termYears = getterm();
    interestRate = getinterestrate();
    termMonths = calctermmonths(termYears);
    interestRateMonthly = calcmonthlyrate(interestRate);
    monthlyPayment = calcmonthlypayment(loanAmount, interestRateMonthly,
      termMonths);
    printpayment(monthlyPayment);
    again = offernewmortgage();
  }
  while (again == 'Y' || again == 'y'); // End of loop for another go.



  system("PAUSE")
    ;
  return 0;
}



int main()
{
  int iret = calcmortgage();

  return iret;
 
}
   
0
 
itsmeandnobodyelseCommented:
As already told you should implement a get_double(). You may use the strtof function to check for valid input.
0
 
ghost8067Author Commented:
Thanks to you both. I am splitting the points between you. I probably should have opened new questions rather than having them all in one post, as you certainly deserve more than the 250 apiece.
Thanks!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.