注册 登录
编程论坛 C++教室

如何改正错误

lyy0012345 发布于 2020-11-07 13:02, 1683 次点击
1>C:\Users\Administrator\source\repos\ConsoleApplication5\ConsoleApplication5\ConsoleApplication5.cpp(9,6): error C2371: “month”: 重定义;不同的基类型
1>C:\Users\Administrator\source\repos\ConsoleApplication5\ConsoleApplication5\ConsoleApplication5.cpp(8): message : 参见“month”的声明
1>C:\Users\Administrator\source\repos\ConsoleApplication5\ConsoleApplication5\ConsoleApplication5.cpp(26,11): error C2088: “<<”: 对于 class 非法
1>C:\Users\Administrator\source\repos\ConsoleApplication5\ConsoleApplication5\ConsoleApplication5.cpp(33,12): error C2088: “<<”: 对于 class 非法
1>C:\Users\Administrator\source\repos\ConsoleApplication5\ConsoleApplication5\ConsoleApplication5.cpp(36,42): error C2088: “<<”: 对于 class 非法
1>C:\Users\Administrator\source\repos\ConsoleApplication5\ConsoleApplication5\ConsoleApplication5.cpp(37,95): error C2088: “<<”: 对于 class 非法
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;

int main()
{
    double loanamount; double annualinterestrate; double monthlypayment; double month; double currentfacevalue;  double interest; double repaidPrincipal;
    int month = 1;
    cout << "" << endl;
    cout << "Welcome to the Personal Loan System" << endl;
    cout << "========================================================================================================================" << endl;
    cout << "" << endl;
    cout << "Please enter the loan amount:";
    cin >> loanamount;
    cout << "Please enter the annual interestrate(%):";
    cin >> annualinterestrate;
    cout << "Please enter the monthlypayment:";
    cin >> monthlypayment;
    cout << "" << endl;
    cout << "Month"  << setw(20) <<"Current face value" << setw(20) << "Interest"  << setw(25) << "Repaid Principal" << endl;
    cout << "================================================================================================================" << endl;
    currentfacevalue = loanamount;
    interest = loanamount * annualinterestrate * 1 / 1200;
    repaidPrincipal = monthlypayment - interest;
    cout <<""<< month << setw(20) << "$" << currentfacevalue << setw(20) << "$" << interest << setw(25) << "$" << repaidPrincipal << endl;
    while (currentfacevalue >= repaidPrincipal)
    {
        month += 1;
        currentfacevalue -= repaidPrincipal;
        interest = loanamount * annualinterestrate * 1 / 1200;
        repaidPrincipal = monthlypayment - interest;
        cout <<""<< month << setw(20) << "$" << currentfacevalue << setw(20) << "$" << interest << setw(25) << "$" << repaidPrincipal << endl;
        if (currentfacevalue < repaidPrincipal)
        {
            cout << "Remaining Balance in month " << month << "=" << currentfacevalue << endl;
            cout << "(Note: The remaining balance" << currentfacevalue << "with settled in the month " << month << "!)";
        }
    }
2 回复
#2
rjsp2020-11-07 14:34
你重复定义了 month 等
既有 double month
又有 int month = 1
#3
rjsp2020-11-07 14:46
代码我看不懂,只是帮你改了错误

程序代码:
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;

int main()
{
    cout << "\nWelcome to the Personal Loan System\n"
            "========================================================================================================================\n\n";

    double loanamount;
    cout << "Please enter the loan amount:";
    cin >> loanamount;

    double annualinterestrate;
    cout << "Please enter the annual interestrate(%):";
    cin >> annualinterestrate;

    double monthlypayment;
    cout << "Please enter the monthlypayment:";
    cin >> monthlypayment;

    cout << "\nMonth" << setw(20) << "Current face value" << setw(20) << "Interest"  << setw(25) << "Repaid Principal\n";
    cout << "================================================================================================================" << endl;
 
    int month = 1;
    double currentfacevalue = loanamount;
    double interest = loanamount * annualinterestrate * 1 / 1200;
    double repaidPrincipal = monthlypayment - interest;
    cout << month << setw(20) << "$" << currentfacevalue << setw(20) << "$" << interest << setw(25) << "$" << repaidPrincipal << endl;

    while (currentfacevalue >= repaidPrincipal)
    {
        month += 1;
        currentfacevalue -= repaidPrincipal;
        interest = loanamount * annualinterestrate * 1 / 1200;
        repaidPrincipal = monthlypayment - interest;
        cout << month << setw(20) << "$" << currentfacevalue << setw(20) << "$" << interest << setw(25) << "$" << repaidPrincipal << endl;
        if (currentfacevalue < repaidPrincipal)
        {
            cout << "Remaining Balance in month " << month << "=" << currentfacevalue << endl;
            cout << "(Note: The remaining balance" << currentfacevalue << "with settled in the month " << month << "!)";
        }
    }
}
1