[need Guide] C++ switch

Below is only short code just for testing and its havent complete yet coz still looking the syntax for it.. so here my situation.. From the code i using switch case for the menu, once i select the withdraw option, i actually wanted it to display another option like 1. 50$ 2. 100$ 3. 150% 4.Insert other amount.. how can i do that? sound like there's another switch case in the switch case...


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <stdlib.h>
#include <conio.h>

using namespace std;

int main ()
{
	double balance = 1000, withdraw = 0;

menu :
	system("cls");

	//Menu

	cout <<"Acount balance Press 1\n";
	cout <<"Withdraw Press 2\n";

	switch(getch())
	{
	case '1':
		system("cls");
		cout<<"Your balance is :" << balance << "\n";

		system("pause");
		goto menu;

	case '2':
		system("cls");

		cout << "Withdraw : ";

         break;
        default:
        goto menu;

	}
}
          
           
Last edited on
Why not just use another switch inside your cases?

1
2
3
4
5
6
7
8
9

switch(getch()){

case 1: {std::cout<<"Withdraw: ";displayOptions();{}switch(getch()){case 1: case 2.....}}

default:break;

}
i trying it right now.. but displayOption is declared as what? i still blur but trying and i will update it here later
This is the general idea i'll leave the details to you


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <stdlib.h>
#include <conio.h>
#include <iomanip>
using namespace std;
double getAmount();  //returns a withdrawal amount from the user
int main()
{

system("cls");

	//Menu
	double amount;
	double balance = 1000, withdraw = 0;
	
	cout <<"Acount balance Press 1\n";
	cout <<"Withdraw Press 2\n";
menu:
	switch(getch())
	{
	case '1':
		system("cls");
		cout<<"Your balance is :" << balance << "\n";
		system("pause");
		goto menu;

	case '2':
		system("cls");
		amount = getAmount();
		cout << fixed << setprecision(2) << setfill('0') << "$"
 << amount << " Has been withdrawn from your account" << endl;
		break;
        default:;

	}
	cin.ignore();
	return 0;
}

//returns a withdrawal amount from the user
double getAmount()
{
	double total;
	int anser;
	bool continueLoop = true;

		while (continueLoop)
	{
	
		cout << "How much money would you like to withdraw? " << endl;;
		cout << "___________________________________________" << endl;;
	
		cout << "1. $50.00" << endl;
		cout << "2. $100.00" << endl;
		cout << "3. $150.00" << endl;
		cout << "4. Enter another amount" << endl;
		cin >> anser;
		cin.ignore(1000, '\n');

		switch (anser)
		{
		case 4:
			cout << "Enter the amount you would like to withdraw ";
			cin >> total;
			cin.ignore(1000, '\n');
			return total;
			break;
		case 3:
			return 150.00;
			break;
		case 2:
			return 100.00;
			break;
		case 1:
			return 50.00;
			break;
		default:
			{
			cerr << "Invalid input" << endl;
			continueLoop = true;
			break;
			}
		}
	}
}
Last edited on
yanson, so this switch is seperated? 1 switch for 1st option(acc detail) and another switch is for 2nd option thats withdraw part? .. uhmm kinda hard to understand it... need take time.. different way to do the code structure
closed account (Dy7SLyTq)
and side note: never ever ever ever use goto or system. EVER
Yeah you shouldn't have everything inside main. I would have removed the other switch statement from main as well, but I worked off what was already there.
and side note: never ever ever ever use goto or system. EVER


This is primarily to increase readability, nothing else.
If you're gonna have others read your code, you should avoid using goto.
Though in some cases, even when others gonna read the code, goto might be the only way.
Anyway, it's good practice to try to avoid it whenever it is the first thing that pops into mind, makes your head come up with other solutions.

When it comes to system(char*), i'm no expert, but unless you got another more efficient code to deal with OS specific tasks, i see no reason why not.

Yeah you shouldn't have everything inside main.


This would also be to increase readability.
Though, it would provide for faster execution to keep the code in main(not a problem in your case).
closed account (Dy7SLyTq)
@IndieExe: only the third is right.
first: the only reason goto is even in c is for historical reasons. it is very easy to lose control of the flow of a program with goto. and if you use goto, there is always, without fail, (i am willing to bet money)a way to do it in c(++)

two: system is horrible. system is taking the easy slacker way out. its going to take up a lot of memory and there are other better ways.

three: its only faster to keep stuff in main if your not using threads
those explaination its kindl abit deeper for me? i'm abit blur about the explaination regarding the goto and system.. coz i'm realy new in oop and c++... "bool continueLoop = true;" what is bool for actually? and in which situation that bool should be appear?
bool contains the value true(1) or false (0) so if continueLop = true(1) the wile loop evaluates to true and the wile loop executes. I don't use goto or system so I can't speak intelligently about them so I will leave it to somebody else.
Hope dont close this topic yet.. since i have been tested another way and trying diff method using class .. i will update it here soon.. i tried another way right now using class.. since theres alot of different.. but i think by create class it should be proper way... and i now having trouble with same things like putting the switch in the switch..
Last edited on
my latest code.. correct me if i;m mistake... i know its abit messy...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include<iostream>
#include<conio.h>
#include<string>
#include <iomanip>
#include <stdlib.h>
using namespace std;
double getAmount();
class atm
{
   private:
      float balance;
   public:
      atm()
      {
         balance=0;
      }
      void deposit(float a)
      {
         cout<<"Enter amount you want to deposit"<<endl;
         cin>>a;
         balance=balance+a;
      }
      void withdraw(float b)
      {
         cout<<"Enter money you want to withdraw"<<endl;
         cin>>b;
         if(b>balance)
            cout<<"Your balance is not enough to continue transaction"<<endl;
         else
            balance=balance-b;
      }
      float showBalance()
      {
         return balance;
      }
};

void main()
{
   atm ob;
   double amount;
   float y=0,z=0;
   char choice;
   string password="";
   char a;
   cout<<"Enter your password:"<<endl;
   do
   {
      a=_getch();
      switch(a)
      {
         case '\b':     // "\b" is used for BACKSPACE
            if(password.length()>0)
               {
                  password.erase(password.length()-1);
                  cout<<'\b'<<" "<<'\b';
                  break;
               }
         default:
            if(a>31&&a<127)        // 31 to 127 are ASCII code for keyboard keys    
               {
                  password.push_back(a);
                  cout<<"*";
               }
     }
   }
   while(a!='\r');           // "\r" is used for ENTER
   if(password=="tzt")
      {
         cout << "\n^^^^Access granted^^^^ \n";
         cout<<"Your current account balance is 0.\n"<<endl;
         do
         {
            int opt;
            cout<<"Enter 1 if you want to deposit money."<<endl;
            cout<<"Enter 2 if you want to withdraw money."<<endl;
            cin>>opt;
            switch(opt)
               {
                  case 1:
                        ob.deposit(y);
                        break;
                  case 2:
                        amount = getAmount();
						cout << fixed << setprecision(2) << setfill('0') << "$"
 << amount << " Has been withdrawn from your account" << endl;
                        break;
                  default:
                        cout<<"Wrong entry!"<<endl;
                        break;
               }
           cout<<"Your account balance:"<<ob.showBalance()<<endl;
           cout<<"******PRESS******\ny to continue\nn to terminate\n";
           cin>>choice;
        }
       while(choice!='n');
     }
   else
   {
      cout << "\nAccess denied...\n";
   }
   cout<<"*********GOOD BYE*********"<<endl;
   _getch();
}
double getAmount()
{
	double total;
	int anser;
	bool continueLoop = true;

		while (continueLoop)
	{
	
		cout << "How much money would you like to withdraw? " << endl;;
		cout << "___________________________________________" << endl;;
	
		cout << "1. $50.00" << endl;
		cout << "2. $100.00" << endl;
		cout << "3. $150.00" << endl;
		cout << "4. Enter another amount" << endl;
		cin >> anser;
		cin.ignore(1000, '\n');

		switch (anser)
		{
		case 4:
			cout << "Enter the amount you would like to withdraw ";
			cin >> total;
			cin.ignore(1000, '\n');
			return total;
			break;
		case 3:
			return 150.00;
			break;
		case 2:
			return 100.00;
			break;
		case 1:
			return 50.00;
			break;
		default:
			{
			cerr << "Invalid input" << endl;
			continueLoop = true;
			break;
			}
		}
	}
}
Topic archived. No new replies allowed.