i am doing my final project which is a program that imitates an atm machine. the code is not complete yet but i had trouble with the switch part of this coding. everytime i tried to run this program it will say that the .exe has stopped working. Help me!
printf("Welcome... \n\n\nEnter your PIN code: ");
scanf("%d",&password);
if(password == 1228336)
acc1();
else if(password == 1224738)
acc2();
else if(password == 1212312)
acc3();
else if(password == 1218420)
acc4();
else
printf("\nWrong code.\nEnter a new PIN code: ");
scanf("%d",&password);
system("pause");
return 0;
}
void acc1 (void)
{
int balance = 100;
data(balance);
return;
}
void acc2 (void)
{
int balance = 200;
data(balance);
return;
}
void acc3 (void)
{
int balance = 300;
data(balance);
return;
}
void acc4 (void)
{
int balance = 400;
data(balance);
return;
}
void data (int balance)
{
char ans;
printf("\nSelect:\n\n'B' to check balance\n\n'W' to withdraw money\n\n'N' to exit\n\n");
fflush(stdin);
scanf("%s",&ans);
switch(ans)
{
case 'B' :
case 'b' : system("cls");printf("Money balance : RM%d\n\n",balance);break;
case 'W' :
case 'w' : withdraw(balance);break;
case 'N' :
case 'n' : system("cls");printf("Thank you for using our programme.\n\n");
default : printf("Please enter again.\n");
}
return;
}
void withdraw (int balance)
{
int new_balance, withdraw;
system("cls");
printf("Enter the amount you want to withdraw: RM ");
scanf("%d",&withdraw);
new_balance = balance - withdraw;
printf("Your current balance is RM %d\n",new_balance);
1. Do you have to do use C rather than C++? scanf's are bad and not safe.
2. Hint on your issue: Your problem is to do with the 'ans' variable.
3. remove 'return' from all of your void methods.
4. why not have the user press a number rather than a letter (check balance, withdraw money etc)?
5. can you use code tags please?