• We've upgraded our forums. Please post any issues/requests in this thread.

C++ While Loop Won't Loop

Joined
Jun 17, 2007
Messages
7,325 (1.91/day)
Likes
995
Location
C:\Program Files (x86)\Aphexdreamer\
System Name Unknown
Processor AMD Bulldozer FX8320 @ 4.4Ghz
Motherboard Asus Crosshair V
Cooling XSPC Raystorm 750 EX240 for CPU
Memory 8 GB CORSAIR Vengeance Red DDR3 RAM 1922mhz (10-11-9-27)
Video Card(s) XFX R9 290
Storage Samsung SSD 254GB and Western Digital Caviar Black 1TB 64MB Cache SATA 6.0Gb/s
Display(s) AOC 23" @ 1920x1080 + Asus 27" 1440p
Case HAF X
Audio Device(s) X Fi Titanium 5.1 Surround Sound
Power Supply 750 Watt PP&C Silencer Black
Software Windows 8.1 Pro 64-bit
#1
[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.:confused:

Code:
#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;
 
Last edited by a moderator:

de.das.dude

Pro Indian Modder
Joined
Jun 13, 2010
Messages
8,202 (2.99/day)
Likes
2,390
Location
Pune
System Name la de da... | Samsung NP355V5C-S05IN
Processor FX 8320 | AMD A8-4500
Motherboard ASRock 990FX Extreme 4 | Samsung something something
Cooling CM Hyper 212 EVO push:pull+ 1 panaflow 113CFM + 2 x 120mm NZXTs | Coolermaster Notepal
Memory 2x4GB DDR3 Corsair Vengeance 1600MHz CL9 | Samsunng 4GB + 2GB DDR3 1600MHz
Video Card(s) AMD 7790 GHZ edition! | 7670M + 7640G
Storage 1TB WD10EZEX | W.D 500GB Green AADS | Samsung 1TB
Display(s) dell S2240l 1080p IPS 21.5" | Samsung antiglare 15.6"
Case NZXT Guardian 921RB(@home) and Antec ASK4000B U3(Current)
Audio Device(s) Realtek ALC892 with THX True Studio | Realtek something
Power Supply Corsair GS600 | 90W Brick, 6 cell 48000mAh battery
Mouse Logitech G400, GOD TIER MOUSE!! LOGitech F310 gamepad!
Keyboard Logitech K200
Software Windows 8.1 x64 | Windows 8.1 x64
Benchmark Scores real men do bench presses not bench marks.
#2
i think something is wrong with your switch statement.
it seems to be selecting the "Case EXIT " everytime.

try using a default case ;

HTML:
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
 
Joined
Jul 5, 2008
Messages
272 (0.08/day)
Likes
65
System Name WorkStation
Processor Intel i7 3770k @ 4.4GHz
Motherboard ASRock Z77 Extreme6
Cooling Corsair H110 Water Cooler AIO
Memory Corsair Vengeance 8GB DDR3 1600MHz
Video Card(s) MSI GTX680 Twin Frozr III OC
Storage WD 1TB Sata III
Display(s) Samsung 22-inch LED 1080p
Case Corsair Carbide Air 540
Audio Device(s) Onboard Realtek 898 HD
Power Supply Corsair CS750M Gold
Software Windows 8.1 Pro x64
#3
Try removing from the loop:
Code:
return 0;
 
Joined
Jun 17, 2007
Messages
7,325 (1.91/day)
Likes
995
Location
C:\Program Files (x86)\Aphexdreamer\
System Name Unknown
Processor AMD Bulldozer FX8320 @ 4.4Ghz
Motherboard Asus Crosshair V
Cooling XSPC Raystorm 750 EX240 for CPU
Memory 8 GB CORSAIR Vengeance Red DDR3 RAM 1922mhz (10-11-9-27)
Video Card(s) XFX R9 290
Storage Samsung SSD 254GB and Western Digital Caviar Black 1TB 64MB Cache SATA 6.0Gb/s
Display(s) AOC 23" @ 1920x1080 + Asus 27" 1440p
Case HAF X
Audio Device(s) X Fi Titanium 5.1 Surround Sound
Power Supply 750 Watt PP&C Silencer Black
Software Windows 8.1 Pro 64-bit
#4
Joined
Dec 27, 2008
Messages
587 (0.18/day)
Likes
104
System Name New / Breakin' the Lol
Processor i5 2500k / Athlon64 X2 4200+ (65W)
Motherboard P8Z68-V PRO/GEN3 / Asus M2N4-SLI
Cooling / Air (1 x 250mm, 2 x 120mm, 1 x 90mm)
Memory DDR3-1600 (8GB) / Crucial Ballistix Tracer PC2-6400 (2gb)
Video Card(s) / Visiontek 4870
Storage HD103SJ /
Display(s) Viewsonic 22" VX2240W
Case Xigmatek Midgard II / Thermaltake VA8003SWA
Power Supply PC Power & Cooling Silencer 750W
#5
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!
 
Joined
Nov 12, 2013
Messages
74 (0.05/day)
Likes
2
#6
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

Senior Monkey Moderator
Staff member
Joined
Feb 6, 2007
Messages
13,817 (3.48/day)
Likes
5,524
Location
Cheeseland (Wisconsin, USA)
Processor Intel Core 2 Quad QX9650 Extreme @ 3.0 GHz
Motherboard Asus Rampage Formula
Cooling ZeroTherm Nirvana NV120 Premium
Memory 8GB (4 x 2GB) Corsair Dominator PC2-8500
Video Card(s) 2 x Sapphire Radeon HD6970
Storage 2 x Seagate Barracuda 320GB in RAID 0
Display(s) Dell 3007WFP 30" LCD (2560 x 1600)
Case Thermaltake Armor w/ 250mm Side Fan
Audio Device(s) SupremeFX 8ch Audio
Power Supply Thermaltake Toughpower 750W Modular
Software Win8 Pro x64 / Cat 12.10
#7
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.