Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2007-10-01
15
Medium Priority
?
257 Views
Last Modified: 2012-05-05
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!
0
Comment
Question by:ghost8067
  • 6
  • 5
  • 4
15 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 19990629
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
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 19990660
>>>> 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
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 19990686
>>>> 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:ghost8067
ID: 19990713
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
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 19990770
>>>> 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
 
LVL 53

Expert Comment

by:Infinity08
ID: 19990772
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
 

Author Comment

by:ghost8067
ID: 19990816
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 19990914
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
 

Author Comment

by:ghost8067
ID: 19990965
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
 
LVL 53

Accepted Solution

by:
Infinity08 earned 1000 total points
ID: 19991002
>> but the laon calculation comes out to 0.

Look at this line :

  return 0;
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 19991073
>>>> 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
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 19991125
>>>> 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
 

Author Comment

by:ghost8067
ID: 19991539
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
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 1000 total points
ID: 19991605
As already told you should implement a get_double(). You may use the strtof function to check for valid input.
0
 

Author Comment

by:ghost8067
ID: 19991674
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

580 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