[英]Error checking in c++
由于某种原因,我无法正常工作= /整天都在搜寻Google,没有运气
我创建了一个名为cFunc的函数来进行错误检查,并在每次用户输入后都调用此函数,以使他们知道添加的信息无效。 但是由于某种原因,这是行不通的。 任何帮助将是巨大的!
#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;
}
首先我看到:在函数中,您已经声明了一个main。 第二:我包含了函数的原型。第三:函数已声明为void,并且返回一个int,为什么?
这是代码的工作。 至少在逻辑上。 祝你好运,如果您需要什么,请告诉我
#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;
}
从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;
}
}
应该
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;
}
}
您可能会在没有充分了解自己在做什么的情况下改编您在其他地方看到的代码。 正如前面的评论之一所说,这是货物崇拜编程。 停止谷歌搜索并读书是我的建议。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.