简体   繁体   中英

Error checking in c++

I can't get this working for some reason =/ Been googling all day and no luck

I created a function called cFunc to do my error checking and i call it every time after the user input to let them know the information they added is not valid. But for some reason this is not working. Any help would be great!

#include "math.h"
#include <iostream>
#include <limits>

using namespace std;

int loanAmount; //amount of the loan
double loanInterest; // the loan interest rate
int loanYears; //years of the loan
int loanTerm = loanYears; //loan term in months
double loanPay; //variable for outputting the payment

int main()
{
cout<<"Enter Loan Amount";
cin>>loanAmount;
cFunc();
cout<<"Enter Loan Interest";
cin>>loanInterest;
cFunc();
cout<<"Enter Loan Years";
cin>>loanYears;
cFunc();


loanPay = (loanAmount * loanInterest) / (1 - pow(1+loanInterest,-loanYears)); //Formula to figure mortgage payment amount

cout<< "Your Monthly Payment Amount is: $"<< loanPay; //prints out monthly payment amount
return 0;
}

void cFunc(){
    int main(){
    cout << "Enter an int: ";
    int x = 0;
    while(!(cin >> x)){
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cout << "Invalid input.  Try again: ";
    }
    cout << "You enterd: " << x << endl;           
    }
    return x;
}

First i see: In the function you have declared a main. Second: I included the prototype of the function Third: The funcion has been declared void, and, return an int, why?

This is the code working. At least, logically. good luck, and if you need something, let me know

#include "math.h"
#include <iostream>
#include <limits>

using namespace std;

int loanAmount; //amount of the loan
double loanInterest; // the loan interest rate
int loanYears; //years of the loan
int loanTerm = loanYears; //loan term in months
double loanPay; //variable for outputting the payment

void cFunc();

int main()
{
cout<<"Enter Loan Amount";
cin>>loanAmount;
cFunc();
cout<<"Enter Loan Interest";
cin>>loanInterest;
cFunc();
cout<<"Enter Loan Years";
cin>>loanYears;
cFunc();


loanPay = (loanAmount * loanInterest) / (1 - pow(1+loanInterest,-loanYears)); //Formula to figure mortgage payment amount

cout<< "Your Monthly Payment Amount is: $"<< loanPay; //prints out monthly payment amount
return 0;
}

void cFunc(){
    cout << "Enter an int: ";
    int x = 0;
    while(!(cin >> x)){
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cout << "Invalid input.  Try again: ";
    }
    cout << "You enterd: " << x << endl;
    // Why have a return int a function declared void?
    // return x;
}

From the code on pastebin

void _loanAmount(int x) {
    cout<<"Enter Loan Amount";
    cin>>loanAmount;

    while (!(cin >> x)) {
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(),'\n');
        cout << "Please enter a numerical value" << endl;
    }
}

should be

void _loanAmount() {
    cout<<"Enter Loan Amount";
    while (!(cin >> loanAmount)) {
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(),'\n');
        cout << "Please enter a numerical value" << endl;
    }
}

You're likely adapting code you've seen elsewhere without sufficient understanding what you are doing. As one of the comments said earlier this is cargo cult programming. Stop googling and read a book would be my advice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM