while循环不会循环C ++ [英] While Loop Won't loop C++

查看:129
本文介绍了while循环不会循环C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法弄清楚为什么while循环停止循环。在我移动一些代码之前,它做得很好。现在我有其他的工作,它不会循环。我也试着让退出一个布尔设置为true,并试图让它循环,而这是真实的,直到用户命中4退出,在这种情况下,它会把它变成false,但没有奏效。我也尝试添加一个while循环showMenu的功能,但也没有工作。我知道它一定是简单的,我无法捕捉它。 gggrrrr。

I can't seem to figure out why this while loop stopped looping. It was doing fine before I moved some code around. Now I got something else working and it just doesn't loop. I've also tried making quit a bool set to true and tried to have it loop while it was true until the user hit 4 to exit in which case it would turn it to false but that didn't work. I also tried adding a while loop to the function of showMenu but that also didn't work. I know it must be something simple I just can't catch it. gggrrrr.

#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。

You return 0 within the switch brackets, ie inside the while loop. Change it so that you return 0 outside of the while loop.

这篇关于while循环不会循环C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆