gpt4 book ai didi

c++ - While Loop 不会循环 C++

转载 作者:太空宇宙 更新时间:2023-11-04 15:46:59 24 4
gpt4 key购买 nike

我似乎无法弄清楚为什么这个 while 循环停止循环。在我移动一些代码之前它运行良好。现在我有别的东西在工作,但它只是不循环。我还尝试将 quit 一个 bool 设置为 true 并尝试让它在它为 true 时循环,直到用户按 4 退出,在这种情况下它会把它变成 false 但这没有用。我还尝试在 showMenu 的函数中添加一个 while 循环,但这也没有用。我知道这一定很简单,我就是听不懂。格格格。

#include "stdafx.h"
#include <iostream>
#include <iomanip>

using namespace std;

enum transType { SETUP=1, DEPOSITE, WITHDRAW, EXIT};

int showMenu(double balance);
double transaction(double amount, double balance, transType trans);

int menuSwitch;
int quit=0;

int _tmain(int argc, _TCHAR* argv[]){

int amount=0,balance=0;
while(quit!=4){

showMenu(balance);
switch (menuSwitch){
case DEPOSITE:
cout<<"Enter the amount of deposit: ";
cin>>amount;
cout<<"Your current balance is: "<<transaction(amount,balance,DEPOSITE)<<endl<<endl;
break;
case WITHDRAW:
cout<<"Enter the amount of withdraw: ";
cin>>amount;
if(amount>balance){
cout<<"*** Insufficient funds."<<"Your current balance is: "<<transaction(amount,balance,WITHDRAW)<<endl<<endl;
}
else cout<<"Your current balance is: "<<transaction(amount,balance,WITHDRAW)<<endl<<endl;
break;
case EXIT:
cout<<"Have a Nice Day."<<endl;
quit=4;
break;

}

return 0;
}
}
int showMenu(double balance){
// while(quit==true){
cout<<"Your Online Checking Account System"<<endl;
cout<<"-------------------------------------------"<<endl;
cout<<"Select an option:"<<endl<<endl;
cout<<" 1. Set up the account."<<endl;
cout<<" 2. Deposit Funds into your Account."<<endl;
cout<<" 3. Withdraw Funds out of your Account."<<endl;
cout<<" 4. Exit"<<endl;
cout<<endl<<">>";
cin>>menuSwitch;
switch (menuSwitch){
case SETUP:
cout<<"Enter the balance: ";
cin>>balance;
cout<<endl<<"Your current balance is: "<<balance<<endl<<endl;
break;
}

return balance;
// }
}
double transaction(double amount, double balance, transType trans){
double withdraw = balance-amount;
double deposite = balance+amount;
if(trans=DEPOSITE){
return deposite;
}
else
return withdraw;





}
//return balance;

最佳答案

您在开关括号内返回 0,即在 while 循环内。更改它,以便在 while 循环之外返回 0。

关于c++ - While Loop 不会循环 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15038584/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com