Help me with this, please open to see my problem
May 24, 2014 at 4:38pm UTC
Im making a simple ATM Machine program for my school project. The main menu works fine, but whenever I choose [1] Balance Inquiry then choose [3]Main Menu sub option, it asks again for the PIN, but I don't want the pin to be entered again because I already entered it from the beginning. Now that's my problem so far, and I can't think of a solution. Please help me.
This is my code so far...
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
#include <iostream>
#include <windows.h>
#include <string>
using namespace std;
//Global Declarations
int option;
int currentPin = 1234;
//Functions
int displayMenu();
int checkBalance(double , double );
double getMoney(double , double );
int main()
{
int pin;
cout<<"Enter your 4-digit PIN: " ;
bool code = true ;
while (code)
{
cin>>pin;
try {
if (pin!=currentPin)
throw pin;
double current = 0, savings = 60000;
displayMenu();
switch (option)
{
case 1:
checkBalance(current, savings);
break ;
case 2:
getMoney(current, savings);
break ;
}//end switch
}//end try
catch (int p)
{
cout<<"Invalid PIN, please enter again.\n" ;
continue ;
}//end catch
code=false ;
}//end while
system("pause>0" );
}//end main
///////////////////////////////////////////////////////////
int displayMenu()
{
system("cls" );
cout<<" Automated Teller Machine\n" ;
cout<<"___________________________________\n" ;
cout<<"[1] Balance Inquiry\n"
<<"[2] Withdrawal\n"
<<"[3] Change Pin Number\n"
<<"[4] Pay Bills\n"
<<"[5] Deposit\n"
<<"[6] Cancel Transaction\n" ;
cout<<"Enter your option: " ;
cin>>option;
bool ans=true ;
while (ans)
{
try
{
if (option<1||option>6)
throw option;
}//end try
catch (int x)
{
cout<<"Invalid option, please try again.\n" ;
continue ;
}//end catch
ans=false ;
}//end while
}//end diplayMenu
///////////////////////////////////////////////////////////
int checkBalance(double cur, double sav)
{
system("cls" );
cout<<" Balance Inquiry\n"
<<"___________________________________\n"
<<"[1] Current Account\n"
<<"[2] Savings Account\n"
<<"[3] Main Menu\n\n" ;
bool ans=true ;
while (ans)
{
cout<<"Enter your option: " ;
cin>>option;
try
{
if (option<1||option>3)
throw option;
switch (option)
{
case 1:
cout<<"Your Current Account has PHP " <<cur;
break ;
case 2:
cout<<"Your Savings Account has PHP " <<sav;
break ;
case 3:
main();
}//end switch
}//end try
catch (int x)
{
cout<<"Invalid option, please try again.\n" ;
continue ;
}//end catch
ans=false ;
}//end while
}//end checkBalance
///////////////////////////////////////////////////////////////
double getMoney(double curr, double savi)
{
system("cls" );
cout<<" Withdrawal\n"
<<"___________________________________\n"
<<"[1] Current Account\n"
<<"[2] Savings Account\n"
<<"[3] Main Menu\n\n" ;
bool ans=true ;
while (ans)
{
cout<<"Enter your option: " ;
cin>>option;
try
{
if (option<1||option>3)
throw option;
}
catch (int x)
{
cout<<"Invalid option, please try again.\n" ;
continue ;
}
ans=false ;
}
}
Last edited on May 24, 2014 at 4:42pm UTC
May 24, 2014 at 4:47pm UTC
Loop on lines 26-36 instead of whole main() function
May 24, 2014 at 4:51pm UTC
how? i already tried it when i replaced main() with displayMenu() but the result only shows the menu but can't enter values again
Last edited on May 24, 2014 at 4:57pm UTC
May 24, 2014 at 5:00pm UTC
Your displaymenu function is not returning anything although it declared to return int
Get rid of global state: remove global option variable and make current pin constant.
NEVER ever call main(). it is explicitly forbidden by standard.
Do
not use exceptions for normal flow control
How your code might look:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
//Assuming displayMenu() returns integer representing your choice
bool loop = true ;
while (loop) {
switch (displayMenu()) {
case 1:
checkBalance(current, savings);
break ;
case 2:
getMoney(current, savings);
break ;
case 0: //Assume 0 is an exit option
loop = false ;
break ;
}
}
Last edited on May 24, 2014 at 5:04pm UTC
Topic archived. No new replies allowed.