I've been playing around in C for a few weeks and decided to write a program that somewhat simulates an ATM machine. I'll give you the code at the end of the post in case you're not interested in dealing with the question at all.
Some facts: There are 2 languages in the program, the 2nd is Slovak. The code for it is the same as the English version, so you can ignore it if you want.
The program isn't perfect. It only deals with integers and usually any user input outside of the pre-defined format makes it do strange things. What I wanted to accomplish is user's ability to at least perform simple operations without the program exiting every time (hence several while (1)
loops within the program).
What I'm looking for: I'd like your input on my coding itself. I have a feeling that my code is very cumbersome, bulky, and probably repetitive. Is there anything you'd like to point out? Correct? Is the whole thing completely worthless? Any ideas for improvement?
Reason for asking: I have no previous experience with programming and don't want to get into bad habits right away. Your input is really appreciated.
Anyway, here's the code.
#include <stdio.h>
#include <stdlib.h>
/*ATM machine simulation program*/
void main (){
int lang, pin, operation, amount, i;
int userpin [50];
userpin [0]= 1234;
userpin [1]= 9999;
int userbal [50];
userbal[0] = 1000;
userbal[1] = 1000;
char user[50]; //used 50 just for buffer purposes. User ID's would be limited to 50 characters
while (1){
system ("cls");
printf ("\tWELCOME TO THE C-BANK ATM MACHINE PROGRAM!\n\n");
printf ("Please select 1 for English.\nStlacte 2 pre Slovencinu.\n");
scanf ("%d", &lang);
if (lang == 1){
while (1) {
system ("cls");
printf ("Welcome! Please, enter your user ID and press enter\n");
scanf ("%s", user);
if (strcmp(user, "Lukas") == 0){
system ("cls");
printf ("Hi Lukas. Please, enter your 4-digit PIN\n");
scanf ("%int", &pin);
if (pin== userpin[0]){
while (1){
system ("cls");
printf("Welcome to your account Lukas! Select one of the following operations and press enter or enter 0 to exit:\n\n\n");
printf("1.Check balance\t\t\t2. Widthdraw money\t\n");
scanf ("%int", &operation);
switch (operation) {
case 1:
system ("cls");
printf ("Your current balance is $%d\n", userbal[0]);
system ("pause > nul");
break;
case 2:
system ("cls");
printf ("Your current balance is $%d. Please, enter the amount you'd like to withdraw and press enter\n", userbal[0]);
scanf ("%int", &amount);
if (amount <= userbal[0]) {
system ("cls");
userbal[0]= userbal[0] - amount;
printf ("You withdrew $%d. Your remaining balance is $%d", amount, userbal[0]);
system ("pause > nul");
}
else {
printf ("Insufficient available funds.\n");
system ("pause > nul");
}
break;
case 0:
exit (0);
break;
default:
printf ("Invalid entry\n");
system ("pause > nul");
}
}
}
else {
printf ("Incorrect PIN\n");
system ("pause > nul");
fflush (stdin);
}
}
else if (strcmp(user, "Adam") == 0) {
system ("cls");
printf ("Hi Adam. Please, enter your 4-digit PIN\n");
scanf ("%int", &pin);
if (pin== userpin[1]){
while (1) {
system ("cls");
printf("Welcome to your account Adam! Select one of the following operations and press enter or enter 0 to exit:\n\n\n");
printf("1.Check balance\t\t\t2. Widthdraw money\t\n");
scanf ("%int", &operation);
switch (operation) {
case 1:
system ("cls");
printf ("Your current balance is $%d\n", userbal[1]);
system ("pause > nul");
break;
case 2:
system ("cls");
printf ("Your current balance is $%d. Please, enter the amount you'd like to withdraw and press enter\n", userbal[1]);
scanf ("%int", &amount);
if (amount <= userbal[1]) {
system ("cls");
userbal[1]= userbal[1] - amount;
printf ("You withdrew $%d. Your remaining balance is $%d", amount, userbal[1]);
system ("pause > nul");
}
else {
printf ("Insufficient available funds.\n");
system ("pause > nul");
}
break;
case 0:
exit (0);
break;
default:
printf ("Invalid entry\n");
system ("pause > nul");
break;
}
}
}
else {
printf ("Incorrect PIN\n");
system ("pause > nul");
fflush (stdin);
}
}
else {
printf ("Incorrect user ID\n");
system ("pause > nul");
fflush (stdin);
};
};
}
else if (lang == 2) {
while (1) {
system ("cls");
printf ("Vitajte! Prosim, zadajte svoje uzivatelske meno a stlacte enter.\n");
scanf ("%s", user);
if (strcmp(user, "Lukas") == 0){
system ("cls");
printf ("Ahoj Lukas. Prosim, zadaj svoj 4-miestny PIN\n");
scanf ("%int", &pin);
if (pin== userpin[0]){
while (1) {
system ("cls");
printf("Vitaj Lukas. Prosim, vyber si jednu z nasledujucich operacii a stlac enter alebo zadaj 0 na ukoncenie programu:\n\n\n");
printf("1.Skontrolovat stav\t\t\t2. Vyber hotovosti\t\n");
scanf ("%int", &operation);
switch (operation) {
case 1:
system ("cls");
printf ("Vas stav na ucte je $%d\n", userbal[0]);
system ("pause > nul");
break;
case 2:
system ("cls");
printf ("Vas stav na ucte je $%d. Prosim, zadajte sumu, ktoru chcete vybrat a stlacte enter\n", userbal[0]);
scanf ("%int", &amount);
if (amount <= userbal[0]) {
system ("cls");
userbal[0]= userbal[0] - amount;
printf ("Vybrali ste $%d. Vas zostatok je $%d", amount, userbal[0]);
system ("pause > nul");
}
else {
printf ("Nedostatocny stav na ucte!\n");
system ("pause > nul");
}
break;
case 0:
exit (0);
default:
printf ("Neznamy prikaz\n");
system ("pause > nul");
break;
}
}
}
else {
printf ("Nespravny PIN\n");
system ("pause > nul");
fflush (stdin);
}
}
else if (strcmp(user, "Adam") == 0) {
system ("cls");
printf ("Ahoj Adam. Prosim, zadaj svoj 4-miestny PIN\n");
scanf ("%int", &pin);
if (pin== userpin[1]){
while (1) {
system ("cls");
printf("Vitaj Adam. Prosim, vyber si jednu z nasledujucich operacii a stlac enter alebo zadaj 0 pre ukoncenie programu:\n\n\n");
printf("1.Skontrolovat stav\t\t\t2. Vyber hotovosti\t\n");
scanf ("%int", &operation);
switch (operation) {
case 1:
system ("cls");
printf ("Vas stav na ucte je $%d\n", userbal[1]);
system ("pause > nul");
break;
case 2:
system ("cls");
printf ("Vas stav na ucte je $%d. Prosim, zadajte sumu, ktoru chcete vybrat a stlacte enter\n", userbal[1]);
scanf ("%int", &amount);
if (amount <= userbal[1]) {
system ("cls");
userbal[1]= userbal[1] - amount;
printf ("Vybrali ste $%d. Vas zostatok je $%d", amount, userbal[1]);
system ("pause > nul");
}
else {
printf ("Nedostatocny stav na ucte!\n");
system ("pause > nul");
}
break;
case 0:
exit (0);
default:
printf ("Neznamy prikaz\n");
system ("pause > nul");
break;
}
}
}
else {
printf ("Nespravny PIN\n");
system ("pause > nul");
fflush (stdin);
}
}
else {
printf ("Nespravne uzivatelske meno.\n");
system ("pause > nul");
fflush (stdin);
};
};
}
else {
system ("cls");
printf ("Invalid command. Enter any character to return to the home screen\n");
system ("pause > nul");
};
};
getch();
}
ATM
is short forAutomatic Teller Machine
. Saying "ATM Machine" is like saying Personal PIN Number. ;) \$\endgroup\$