Calling option from menu function

The code has main menu based on ATM macine. 1-Get cash. 2-Get Balance. 3-Quit % if user choose option 1
Mainmenu= menu('Main Menu','1-Get Cash','2-Get Balance','3-Quit');
if Mainmenu ==1
In Get Cash, Another menu that the user have to choose how much to withdraw
GetCashM= menu('Withdrawal amount','20$','40$','100$','200$');
, and after that, another menu to prompt the user to select 1- checking, 2- savings,
Acct= menu('From which account','checking','Saving');
and disp how many 20 bills were dispansed that is after verifying if there are enough funds in the selected account. And when option 1 or 2 are processed, the main menu will show again. I got that done. But, If option 3- quit selected, display New checking Balance and New Saving Balance. But how can I subtract the withdraw amount from checking savings account since menu option are string I think. Even if I called it it would be 1 or 2 or 3 or 4 respectivly to withdraw amount menu.
(ex)
Sbal = 100;
if the user withdrew 20$ from Sbal . "in option 1 from whidraw amount "
"second iteration."
if MainMenu ==. 3 % if the user choose option quit after proseccing whidraw amount
NewSavBal = GetCashMenu - SBal;
but that did not do it since values inside menu were string.
Keep in mind that
  • If the user closes the menu rather than making a selection, warning message should bedisplayed and the user given 2 more chances to make a selection from that menu. If they still close the menu on the 3rd attempt, an error message should be displayed that says the program has been terminated.

7 Commenti

menu() does not return strings or character vectors. menu() returns the relative choice in numeric form -- first option such as the '20$' returns 1, second option such as the '40$' returns 2, and so on.
Ok I got this done. But how can I do this
  • If the user closes the menu rather than making a selection, warning message should be displayed and the user given 2 more chances to make a selection from that menu. If they still close the menu on the 3rd attempt, an error message should be displayed that says the program has been terminated.
should I make another while loop just for that part. Because I aleady have a while loop from after the user input till the last part where I adeed i = i+1 at the end of the while loop and the statment is while i `~= 3
this will iterate the program 3 times only.
the rest of the code.
warncount = 0;
while true
MainMenu = menu(....)
if MainMenu == 0
if warncount == 3
error
else
warning
warncount = warncount + 1;
end
else
do something appropriate
warncount = 0;
end
end
mat geek
mat geek il 21 Apr 2019
Modificato: mat geek il 21 Apr 2019
Here is my version , and I know it's not neat, but I got everything to work except this one thing. the problem is the code does not go through Acct=1, it skips to the end of the code. I think the problem with the end function.
CBal= input('Enter your checking account balance\n');
SBal= input('Enter your Savings account balance\n');
k=0;
i = 0;
while i ~=3
Mainmenu= menu('Main Menu','1-Get Cash','2-Get Balance','3-Quit');
if Mainmenu ==1
GetCashM= menu('Withdrawal amount','20$','40$','100$','200$');
Acct= menu('From which account','Checking','Savings');
if Acct == 1 % the code does not execute this statement %% even if it's ture ???
elseif GetCashM == 1 && CBal >=20
MonOut = 1 ;
fprintf(' %d 20$ bills were dispensed\n',MonOut)
CBal = CBal - 20;
elseif GetCashM == 2 && CBal >=40
MonOut = 2;
fprintf(' %d 20$ bills were dispensed\n',MonOut)
CBal = CBal - 40;
elseif GetCashM == 3 && CBal >=100
MonOut = 5 ;
fprintf(' %d 20$ bills were dispensed\n',MonOut)
CBal = CBal - 100;
elseif GetCashM ==4 && CBal >=200
MonOut = 10 ;
fprintf(' %d 20$ bills were dispensed',MonOut)
CBal = CBal - 200;
else
fprintf('not enough funds in check account\n')
end
else
if Acct == 2
elseif GetCashM == 1 && SBal >=20
MonOut = 1 ;
fprintf(' %d 20$ bills were dispensed\n',MonOut)
NewSBal = SBal - 20;
elseif GetCashM == 2 && SBal >=40
MonOut = 2;
fprintf(' %d 20$ bills were dispensed\n',MonOut)
NewSBal = SBal - 40;
elseif GetCashM == 3 && SBal >=100
MonOut = 5 ;
fprintf(' %d 20$ bills were dispensed\n',MonOut)
NewSBal = SBal - 100;
elseif GetCashM ==4 && SBal >=200
MonOut = 10 ;
fprintf(' %d 20$ bills were dispensed',MonOut)
NewSBal = SBal - 200;
else
fprintf('not enough funds in savings account\n')
break;
end
if Mainmenu ==2
checAcct= menu('From which account','checking','Saving');
if checAcct == 1
fprintf('Your Checking Account balance is %d$\n', CBal)
elseif checAcct ==2
fprintf('Your Savings Account balance is %d$\n', SBal)
end
end
if Mainmenu == 3
NewCBal = CBal;
NewSBal = SBal;
fprintf('New Cecking account balance is %d\n',NewCBal)
fprintf('New Saving account balance is %d\n',NewSBal)
i = i+3;
end
if Mainmenu ==0
fprintf('warning')
i = i +1;
end
end
end
After your tests for specific Acct values you want plain if not elseif
ok, I will try to do that. thank you.
Thenk you for the help. It is working now just fine.

Accedi per commentare.

Risposte (0)

Tag

Richiesto:

il 20 Apr 2019

Commentato:

il 21 Apr 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by