HiveBrain v1.2.0
Get Started
← Back to all entries
patterncMinor

ATM Machine Simulator

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
machinesimulatoratm

Problem

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
#include
/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. Plea

Solution

The problem here is the lack of functions or subroutines to avoid repeating code, thus making the code less mantainable.

There are here other issues which are also important, for example, multilanguage support, which I'll address in the following

To carry out multilanguage, I would declare an n x m array to store the n strings in m languages. To provide an example of what I mean, to store the greeting message, an array like the following needs to be declared

char multilanguageArray[1][2] = {
    {"WELCOME TO THE C-BANK ATM MACHINE PROGRAM!"},
    {"Vitajte! Prosim, zadajte svoje uzivatelske meno a stlacte enter."}
}


To explain this array, the first dimension of the array declares the amount of messages stored, while the second dimension declares the amount of languages one can output the information.

So, if we would like to print in any of the languages, the following line would just do that.

scanf ("%d", &lang);
printf ("\t%s\n\n", multilanguageArray[1][lang - 1]);


This code would make the greeting valid for both languages, while also making the code more mantainable if you want to change the greeting message for any language/s by having all the messages in the same place.

Keep in mind I substract 1 from lang since arrays are zero-index based.

Implementing language this way can save you a lot of code, make it more readable and also more mantainable since you don't have to look in n lines to fix an error that has propagated through all your languages due to copy pasting code.

This can be applied too to handle any amount of users, further reducing your code and making it, again, more manageable when maintenance or improvements happen (which you are bound to do).

If you need help in developing anything, comment my post with what you need and I'll try to give you an answer as clear as I can. Hope this helped explain how to handle multilanguage.

Note: To ease the maintenance of interface messages, you can switch the matrix, n for languages, m for messages, that way you will have in the same first dimension the same message for all the languages, rather than having all the messages for a language in the first dimension.

Code Snippets

char multilanguageArray[1][2] = {
    {"WELCOME TO THE C-BANK ATM MACHINE PROGRAM!"},
    {"Vitajte! Prosim, zadajte svoje uzivatelske meno a stlacte enter."}
}
scanf ("%d", &lang);
printf ("\t%s\n\n", multilanguageArray[1][lang - 1]);

Context

StackExchange Code Review Q#112578, answer score: 7

Revisions (0)

No revisions yet.