atm machine

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!

#include<stdio.h>
void acc1 (void);
void acc2 (void);
void acc3 (void);
void acc4 (void);
void data (int);
void withdraw (int);

int main()
{
int password;

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);

return;
}
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?
my course only taught us the basic which is C++ and i dont really know how to use C.

i modified the code according to your instruction. i can run the program but i couldnt exit from the output box.

anyway thanks for the help :)

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
#include<stdio.h>
void acc1 (void);
void acc2 (void);
void acc3 (void);
void acc4 (void);
void data (int);
void withdraw (int);
int main()
{
    int password;
    
    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);

}

void acc2 (void)
{
     int balance = 200;
     
     data(balance);
     
}

void acc3 (void)
{
     int balance = 300;
     
     data(balance);
     
}

void acc4 (void)
{
     int balance = 400;
     
     data(balance);
     
}

void data (int bal)
{
     int ans;
     
     printf("\nSelect:\n\n'1' to check balance\n\n'2' to withdraw money\n\n'3' to exit\n\n");
     scanf("%d",&ans);
     
     if(ans == 1)
     printf("\nYour money balance is RM%d\n",bal);
     
     else if(ans == 2)
     withdraw(bal);
     
     else if(ans == 3)
     printf("Thank you for using our programme.\n");
     
}

void withdraw (int bal)
{
     int withdraw, new_bal;
     
     printf("Amount you want to withdraw : RM ");
     scanf("%d",&withdraw);
     
     new_bal = bal - withdraw;
     
     printf("Your current money balance is RM%d\n",new_bal);
     
}
Last edited on
No worries :)

my course only taught us the basic which is C++ and i don't really know how to use C.


You are not using ANY C++ in your code. it's all C.


i can run the program but i couldnt exit from the output box.


change this:
1
2
3
    else
    printf("\nWrong code.\nEnter a new PIN code: ");
    scanf("%d",&password);


to


    else
   {
       printf("\nWrong code.\nEnter a new PIN code: ");
       scanf("%d",&password);
    }



otherwise that scanf will be called regardless of your user's choice.
Last edited on
it worked! :D :D :D

now all i need to do is to write all the necessary loops to complete this program.

thanks a lot :D :D :D :D
Topic archived. No new replies allowed.