簡體   English   中英

我的 C++ 程序在輸入后退出並過早結束。 為什么會發生這種情況?

[英]My C++ program exits and ends too early after input. Why does this happen?

[抱歉,如果我使用了錯誤的編程短語]

我這里有一個程序來計算和處理用戶的提款交易。 我正在使用 Visual Studio 2019。該程序在不同的輸入下都可以正常工作,但問題是在第二次調用 drawProcess() 函數期間,當輸入一個新的提款金額時,程序退出並在那里結束,而沒有轉到下一個命令行; 比如while (withdrawAmount > 500)等。

#include <iostream>
using namespace std;

double currentBalance = 0; //current bank balance
double withdrawAmount = 0; //withdrawal amount
double withdrawFee = 0; //withdrawal fee
char confirmation; //user's confrimation of 'y' or 'n'

void withdrawProcess() //the processes related ot the user's withdrawal
{
    withdrawAmount = 0; withdrawFee = 0; //sets the withdrawal amount and fee to 0

    cout << "\nEnter the amount you wish to withdraw* (max withdrawal amount : $500/day). *Withdrawal fees may apply : "; cin >> withdrawAmount; //inputs the user's withdrawal amount

    while (withdrawAmount > 500) //loops maximum withdrawal limit message if user's withdrawal amount is greater than 500
    {
        cout << "\nYour withdrawal amount cannot exceed the maximum withdrawl limit ($500/day). Please  enter a valid withdrawal amount : "; cin >> withdrawAmount; //inputs the user's new withdawal amount
    }
    while (withdrawAmount > currentBalance) //loops if user's withdrawal amount is greater than the user's current bank balance
    {
        cout << "\nYour withdrawal amount cannot exceed your current balance. Please enter a valid withdrawal amount : "; cin >> withdrawAmount; ////inputs the user's new withdawal amount
    }

    if (withdrawAmount > 300) //checks if the user's witdrawal amount is greater than 0
    {
        withdrawFee = ((withdrawAmount - 300) * 0.04); //the withdrawal fee is 4% of the total withdrawal amount
    }
    else if (currentBalance < (withdrawAmount + withdrawFee)) //checks if the user's current balance is less than the withdrawal amount plus the withdrawal fee
    {
        withdrawFee = 25; //withdrawal fee is set to 25
    }
}

void transaction() //the processes related the user's transaction
{

    cout << endl << "\nYou have chosen to withdraw $" << withdrawAmount << ".\n"; //displays the user's desired withdrawal amount

    (withdrawFee > 0) ? cout << "You will be charged a withdraw fee of $" << withdrawFee << ".\n" : cout << "You will not be charged a withdraw fee.\n"; //determines if the withdrawal fee is greater than 0, then displays  the appropriate message

    if (currentBalance > (withdrawAmount + withdrawFee)) //checks if the user can withdraw and pay for the fee
    {
        cout << "\nWould you like to continue? [y/n] : "; cin >> confirmation; //asks the user if they would like to continue with the withdrawal
    }
    else
    {
        cout << "\nYou cannot withdraw that amount, would you like to withdraw another amount? [y/n] : "; cin >> confirmation; //asks if the user would like to enter a new withdrawal amount or cancel the withdrawal
    }

    while (confirmation != 'y' && confirmation != 'n') //function loops until gives the correct confirmation input
    {
        cout << "\nWrong input. Please confrim whether you would like to continue with your withdrawal [y/n] : "; cin >> confirmation; //inputs the user's new confirmation input
    }

    switch (confirmation) //switch case to determine whether the user inputs 'y' or 'n'
    {
    case 'y':
    {
        if (currentBalance > (withdrawAmount + withdrawFee))
        {
            cout << "\nYou have withdrawn $" << withdrawAmount << ", and a the withdrawal fee of $" << withdrawFee << " is deducted from your account. Your current balance is $" << currentBalance - (withdrawAmount + withdrawFee) << ".\n"; //withdrawal is processed
        }
        else
        {
            withdrawProcess(); //calls withdrawProcess
        }
        break;
    }
    case 'n':
    {
        cout << "\nWithdrawal cancelled.\n"; //withdrawal is cancelled
    }
    default:
        break;
    }
}

int main()
{
    cout << "Enter your current bank balance : "; cin >> currentBalance; //inputs the user's current bank account balance

    withdrawProcess(); //calls the interface
    transaction(); //goes to transaction

    return 0; //ends program
}

所以如果我在這里輸入變量; 調試

你可以在我輸入提款金額后看到它退出。

我需要做任何提示或更正嗎?

transaction()的呼叫后,不叫withdrawProcess()函數中transaction()

添加調用將改善行為。

        if (currentBalance > (withdrawAmount + withdrawFee))
        {
            cout << "\nYou have withdrawn $" << withdrawAmount << ", and a the withdrawal fee of $" << withdrawFee << " is deducted from your account. Your current balance is $" << currentBalance - (withdrawAmount + withdrawFee) << ".\n"; //withdrawal is processed
        }
        else
        {
            withdrawProcess(); //calls withdrawProcess
            transaction(); // add this
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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