# C++ While Loop Won't Loop



## AphexDreamer (Feb 23, 2013)

*[SOLVED]*

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.


```
#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;

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;
```


----------



## de.das.dude (Feb 23, 2013)

i think something is wrong with your switch statement.
it seems to be selecting the "Case EXIT " everytime.

try using a default case ;


```
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;
                       default: cout<<"default";
```


this way you will know if its having some logical problem there 


i just noticed that you dont seem to be taking the input for menuSwitch at the end of each case.


also you can run the while loop with menuSwitch by : while(menuSwitch!=0)
where "0" corresponds to exit.


also menuSwitch is declared an integer, but in the cases you seem to be using it as a string? that would explain your switch error


----------



## syeef (Feb 23, 2013)

Try removing from the loop:

```
return 0;
```


----------



## AphexDreamer (Feb 23, 2013)

syeef said:


> Try removing from the loop:
> 
> ```
> return 0;
> ```



lol thanks.


----------



## OnePostWonder (Feb 23, 2013)

Just something to keep in mind for next time.  When you edit your original post to remove all the info and put in its place "solved", it doesn't do much to help someone having the same issue.  People's replies might still help, but it's nice to compare the original problem to one's own.

I'm not doing any programming in C++, I'm just saying it would be better practice to put in bold at the front of the post "Solved" and leave the rest.

Either way, 'glad you were able to resolve your issue!


----------



## Jimmy6 (Dec 5, 2013)

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


----------



## Kreij (Dec 5, 2013)

OnePostWonder said:


> Just something to keep in mind for next time.  When you edit your original post to remove all the info and put in its place "solved", it doesn't do much to help someone having the same issue.  People's replies might still help, but it's nice to compare the original problem to one's own.
> 
> I'm not doing any programming in C++, I'm just saying it would be better practice to put in bold at the front of the post "Solved" and leave the rest.
> 
> Either way, 'glad you were able to resolve your issue!



You are right, OPW. I put back the original for future reference and marked it solved in the OP.
Happy coding, all.


----------

