patterncMinor
ATM program in C
Viewed 0 times
atmprogramstackoverflow
Problem
Had to create this for my class, figured I'd get some advice/insight on how I can advance my program while making it more efficient. Also want to help anyone who may come across the same assignment and is in need of direction; with this said, I would have regretted copying anyone work because I learned a lot about C syntax while making this rather simple program.
```
#include
#include
#include
#include
#include
//clrscr();
//make a for loop to have the user enter the acc num and pin only having 3 chances before the program terminates -- kinda met
int account_number, pin, chk_acc_bal, sav_acc_bal;
char choice = 'y' ;
void validate_acc(){
int user_acc_try, i = 0;
int user_has_not_entered_right_acc = 1;
int retries = 3;
while(retries > 0 && user_has_not_entered_right_acc == 1){
printf("\nPlease enter your account number: ");
scanf("%d", &user_acc_try);
if(user_acc_try != account_number){
printf("You entered the wrong account number\n");
retries--;
}
else{
user_has_not_entered_right_acc = 0;
}
}
}
void validate_pin(){
int user_pin_try, i=0;
int user_has_not_entered_right_pin = 1;
int retries = 3;
while(retries > 0 && user_has_not_entered_right_pin == 1){
printf("Please enter your pin number: ");
scanf("%d", &user_pin_try);
if(user_pin_try != pin){
printf("You entered the wrong pin number.\n");
retries--;
}
else{
user_has_not_entered_right_pin = 0;
}
}
if(retries = 0){
printf("You have reached maximum tri
```
#include
#include
#include
#include
#include
//clrscr();
//make a for loop to have the user enter the acc num and pin only having 3 chances before the program terminates -- kinda met
int account_number, pin, chk_acc_bal, sav_acc_bal;
char choice = 'y' ;
void validate_acc(){
int user_acc_try, i = 0;
int user_has_not_entered_right_acc = 1;
int retries = 3;
while(retries > 0 && user_has_not_entered_right_acc == 1){
printf("\nPlease enter your account number: ");
scanf("%d", &user_acc_try);
if(user_acc_try != account_number){
printf("You entered the wrong account number\n");
retries--;
}
else{
user_has_not_entered_right_acc = 0;
}
}
}
void validate_pin(){
int user_pin_try, i=0;
int user_has_not_entered_right_pin = 1;
int retries = 3;
while(retries > 0 && user_has_not_entered_right_pin == 1){
printf("Please enter your pin number: ");
scanf("%d", &user_pin_try);
if(user_pin_try != pin){
printf("You entered the wrong pin number.\n");
retries--;
}
else{
user_has_not_entered_right_pin = 0;
}
}
if(retries = 0){
printf("You have reached maximum tri
Solution
Some of the suggestions that I think you might find useful are:
-
Functions are great because you get to reuse the code. I noticed that you have two separate functions to perform validations. Instead, you could simply pass the value to be validated as a parameter.
An example of such a function that can be reused over and over again would be:
- Indentation
-
Functions are great because you get to reuse the code. I noticed that you have two separate functions to perform validations. Instead, you could simply pass the value to be validated as a parameter.
An example of such a function that can be reused over and over again would be:
int main(int argc, char *argv[]){
int validate_state=0;
//(1)----------Validating account number
validate_state=0;
validate_state=validate(account_number, 3);
if (validate_state==0) printf("Invalid Account Number");
//(2)----------Validating Pin number
validate_state=0;
validate_state=validate(pin_number, 3);
if (validate_state==0) printf("Invalid Pin Number");
//(n)----------Validating n'th value with k attempts
validate_state=0;
validate_state=validate(n_value, k);
if (validate_state==0) printf("Invalid Some Number");
}
int validate (int expected_val, int retries){
int input;
scanf("%d", &input); // maybe fgets as the prev comment
while(retries>0){
if (input==expected_val){
return 1;
}else{
scanf("%d", &input);
retries --;
}
}
return 0;
}Code Snippets
int main(int argc, char *argv[]){
int validate_state=0;
//(1)----------Validating account number
validate_state=0;
validate_state=validate(account_number, 3);
if (validate_state==0) printf("Invalid Account Number");
//(2)----------Validating Pin number
validate_state=0;
validate_state=validate(pin_number, 3);
if (validate_state==0) printf("Invalid Pin Number");
//(n)----------Validating n'th value with k attempts
validate_state=0;
validate_state=validate(n_value, k);
if (validate_state==0) printf("Invalid Some Number");
}
int validate (int expected_val, int retries){
int input;
scanf("%d", &input); // maybe fgets as the prev comment
while(retries>0){
if (input==expected_val){
return 1;
}else{
scanf("%d", &input);
retries --;
}
}
return 0;
}Context
StackExchange Code Review Q#84314, answer score: 5
Revisions (0)
No revisions yet.