而Loop不会循环C ++

时间:2013-02-23 08:18:33

标签: c++ function loops parameters while-loop

我似乎无法弄清楚为什么这个while循环停止循环。在我移动一些代码之前,它做得很好。现在我得到了其他工作,它只是不循环。我也尝试过将一个bool设置为true并尝试让它循环而它是真的,直到用户点击4退出,在这种情况下它将把它变为false但是这不起作用。我也尝试在showMenu的函数中添加一个while循环但是也没有用。我知道它一定是简单的我无法抓住它。 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; 

1 个答案:

答案 0 :(得分:6)

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