c++ - C++中的错误检查

标签 c++ debugging syntax

由于某种原因我不能让它工作 =/ 一整天都在谷歌搜索,但没有运气

我创建了一个名为 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. 第二: 我包含了函数的原型(prototype) 第三: 函数已经被声明为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;
}

关于c++ - C++中的错误检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32018045/

相关文章:

c++ - set_new_handler (std::new_handler func) 失败后构造函数调用,内存分配失败?

c++ - cmake - 添加子模块

c++ - 在 C++ 的宏中包装方法

reactjs - 断点无法通过 Windows 10 和 WSL2 上的 Visual Studio Code 在 Chrome 中调试 React 应用程序

c++ - 你可以在C++中将全局变量初始化为什么

c++ - 为什么 64 位版本的应用程序比 32 位版本的应用程序慢得多

c++ - 使用嵌套的 for 循环对 vector 中的元素求和

javascript - 在 C++ 中调试使用 IWebBrowser2 呈现的 javascript/html

java - 为什么 Java 不允许扩展数组类型

javascript - JavaScript 中的小数分隔符是什么?