簡體   English   中英

C ++中的錯誤檢查

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM