Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 代码工作数学不';T_C++_Loops_Math_Nested Loops_Formula Editor - Fatal编程技术网

C++ 代码工作数学不';T

C++ 代码工作数学不';T,c++,loops,math,nested-loops,formula-editor,C++,Loops,Math,Nested Loops,Formula Editor,我的代码编译得很好,但是我使用的数学公式没有提供正确的结果。我需要计算所有3个月的余额、提款和利息。我还需要验证用户的输入。出于这些目的,我使用嵌套循环。如果你发现我的错误,请告诉我。谢谢你们这些可爱的人 cout << "Please enter the starting balance: "; cin >> startBalance; cout << "Please enter the annual interest rate: "; cin &

我的代码编译得很好,但是我使用的数学公式没有提供正确的结果。我需要计算所有3个月的余额、提款和利息。我还需要验证用户的输入。出于这些目的,我使用嵌套循环。如果你发现我的错误,请告诉我。谢谢你们这些可爱的人

cout << "Please enter the starting balance: ";
cin  >>  startBalance;  
cout << "Please enter the annual interest rate: ";
cin  >> annualInterest;

for (int month = 1; month <= 3; month++) {

       do { 
        cout << setprecision(5);
        cout << "Please enter the total amount deposited on month " << month << ": ";
        cin >> balance; 

        if (balance <0)  {  
            goodChoice = false;
            cout << "\n\t***ERROR " << balance << " ***";
            cout << "*** Choice must be positive***\n" << endl;
            }

            else {
             goodChoice = true;
            }

        startBalance += balance; //need to calculate the balance for all 3 months

           } while (!goodChoice); 

       do { 
        cout << setprecision(5);
        cout << "Please enter the total amount withdrawn on " << month << ": ";
        cin >> withdrawn; 

            if ( (withdrawn <0) || (withdrawn > startBalance) ) {  
            goodChoice = false;
            cout << "***ERROR " << withdrawn << " ***";
            cout << "*** Choice must be positive or greater than the balance***" << endl;
            }

            else {
             goodChoice = true;
            }
             finalWithdrawn += withdrawn; // the total amount of withdrawn

             finalBalance = startBalance - withdrawn;

          monthInterest = ((startBalance + finalBalance) / 2) * (annualInterest / 12);
          totalInterest += monthInterest; //total interest for all 3 months
          endBalance = monthInterest + finalBalance;


        } while (!goodChoice);  
}

cout << "Total Deposit: " << endBalance << endl;
cout << "Total Withdrawn: " << finalWithdrawn << endl;
cout << "Total Interest: " << totalInterest << endl;
cout << "Final Balance: " << endBalance << endl;
cout>startBalance;
年度利益;

对于(int month=1;month您定义了许多不需要的额外变量。此外,您的利率可能是百分比,而不是十进制数字,即10%=0.1。此外,您的
monthinterst
取平均值,然后应用利息。我写了这篇文章,它似乎有效

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    float totalDeposit = 0.0f;
    float totalWithdrawn = 0.0f;
    float totalInterest = 0.0f;
    float totalBalance = 0.0f;

    float interestRate = 0.0f;

    setprecision(5); //?

    cout << "Enter starting balance: ";
    cin >> totalBalance;
    cout << "Enter annual interest rate (%): ";
    cin >> interestRate;

    // Convert to monthly and non-percent; i.e. 10% = 0.1 = 10 / 100
    interestRate = interestRate / 100.0f / 12.0f;

    for (int month = 1; month <= 3; month++)
    {
        float deposit = -1.0; // Default to an error state

        while (deposit < 0.0)
        {
            cout << "Enter total deposited in month " << month << ": ";
            cin >> deposit;

            if (deposit < 0.0f)
            {
                cout << "ERROR: Invalid amount" << endl;
                continue;
            }
        }

        float withdrawn = -1.0f; // Default to an error state

        while (withdrawn < 0.0f)
        {
            cout << "Enter total withdrawn in month " << month << ": ";
            cin >> withdrawn;

            if (withdrawn < 0.0f)
            {
                cout << "ERROR: Invalid amount" << endl;
                continue;
            }
        }

        totalDeposit += deposit;
        totalWithdrawn += withdrawn;
        totalBalance = totalBalance + deposit - withdrawn;
        totalBalance += totalBalance * interestRate;
        totalInterest += totalBalance * interestRate;
    }

    cout << "Total Deposit: " << totalDeposit << endl;
    cout << "Total Withdrawn: " << totalWithdrawn << endl;
    cout << "Total Interest: " << totalInterest << endl;
    cout << "Final Balance: " << totalBalance << endl;

    int wait; // Pause so console window doesn't close. Delete this line.
    cin >> wait;

    return 0;
}
#包括
#包括
使用名称空间std;
int main()
{
浮动总存款=0.0f;
浮点数=0.0f;
浮动总利息=0.0f;
浮动总平衡=0.0f;
浮动利率=0.0f;
设置精度(5);/?
总平衡;
库特>酯;
//转换为月度和非百分比;即10%=0.1=10/100
利率=利率/100.0f/12.0f;

对于(int month=1;month请发布一个和期望的行为。您的输入是什么?期望的输出是什么?两件事:1.我们需要知道变量的声明;2.在底部,您比较了“撤回”和“开始平衡”(我认为两者都是浮动的)。你不能用这种方式直接比较浮点数。请参阅:了解详细信息。1)我已将所有变量设置为双精度,bool goodChoice=false。2)我是说结果是错误的。在我插入数字后,我得到了一个负的结果。尽管我所有的输入都是正的,但对于总提取量,我得到了-1