首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我的C++程序在输入之后过早地退出和结束。这一切为什么要发生?

我的C++程序在输入之后过早地退出和结束。这一切为什么要发生?
EN

Stack Overflow用户
提问于 2020-10-13 17:42:27
回答 1查看 90关注 0票数 0

如果我使用了错误的编程短语,很抱歉

我这里有一个程序来计算和处理用户的提款交易。我正在使用Visual Studio 2019。程序在不同的输入下工作得很好,但问题是在withdrawProcess()的第二次函数调用期间,当输入一个新的withdrawAmount时,程序退出并结束于此,而不会进入下一个命令行;如while (withdrawAmount > 500)等。

代码语言:javascript
复制
#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
}

因此,如果我像这样输入变量;Debug

您可以在我进入withdrawalAmount之后看到它退出。

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-13 17:55:57

在函数transaction()中调用withdrawProcess()之后,不会调用transaction()

添加call of将改善行为。

代码语言:javascript
复制
        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
        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64332677

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档