c++中的错误检查
Error checking in c++
由于某些原因,我无法使此工作=/我搜了一整天都没找到
我创建了一个名为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;
}
}
你可能在没有充分理解你在做什么的情况下,修改了你在别处看到的代码。正如之前的一个评论所说,这是货物崇拜节目。我的建议是别上网了,去读本书。
相关文章:
- error_code:如何设置和检查错误
- CocoaPod - 在头文件中使用 C++ 时出现 Podspec 检查错误
- 在编写包装现有函数并检查错误的模板函数时,如何使用完美转发?
- swscanf_s - 运行时检查错误
- 为什么C++字符串迭代器不检查错误?
- 如何使用运行时检查错误来修复我的C 格斗游戏
- XCode -Mac App -Bootstrap启动时检查错误
- 挂钩发送/WSASEND返回运行时检查错误#0
- 从整数到枚举值时,如何和何处检查错误
- C .如何检查错误的输入
- C++ 输入字符而不是 int 会导致无限循环.如何检查错误的输入
- 检查错误时重载>>运算符中的无限循环
- 开发基本的C++代码+检查错误/警告
- 在C++中,如何在使用gzstream编写压缩文件时检查错误
- 如何使 cuda-memcheck 在出现泄漏检查错误时返回非零
- 是否可以使用GetLastError检查错误代码是否已恢复
- 何时检查错误,使用 QIODevice 的阻塞接口(QTcpSocket 和 QFile)
- 在调用 recv() 之前检查错误
- C++类型检查错误
- 如何修复配置 PHP 7.0.1 时C++预处理器"/lib/cpp"健全性检查错误