0

Hello Guys
I am new to C programming and as a part of an assignment I have made an ATM algorithm in C. Though the compiler shows no errors, the code goes into an infinite loop after the first input.
Please Help.
Here's the code:

#include<stdio.h>
#include<conio.h>
void main(){
float bal=0,dep=0,withd=0;
int n1,n2;
clrscr();
do{
clrscr();
printf("\n\t\t************************\t\t\n");
printf("\t\tWelcome To ATM Services\n");
printf("\t\tSelect Your Choice:\n");
printf( "\t\t1)Deposit Cash\n\t\t2)Withdraw Cash\n\t\t3)Bank Statement\n\t\t4)Fast Cash\n\t\t5)Exit\n");
printf("\t\t***********************\t\t\n");
scanf("%d",&n1);
switch(n1){
case 1: printf("Enter the amount to be deposited\n");
scanf("%f",&dep);
if(dep>0){
bal+=dep;
printf("You have deposited %f cash. Your current account balance is %f\n",dep,bal); }
else printf("Invalid amount entered\n");
printf("Press any key to continue\n");
break;
case 2: printf("Enter the amount to be withdrawn\n");
scanf("%f",&withd);
if(withd<bal && withd>0){
bal-=withd;
printf("You have withdrawn %f cash. Your current account balance is %f\n",withd,bal);}
else printf("Insufficient funds in your account\n");
printf("Press any key to continue\n");
break;
case 3:  printf("\t\tYour Current Bank Statement Is:\n");
printf("\t\t Your account balance is %f.\n",bal);
printf("Enter any key to continue");
break;
case 4: printf("\t\tWelcome To FastCash\n");
printf("\t\tSelect an alternative:\n");
printf("\t\t1)Rs100\n\t\t2)Rs500\n\t\t3)Rs1000\n");
scanf("%d",&n2);
switch(n2){
case 1: printf("You have chosen to withdraw Rs100\n");
if(bal>=100){
bal-=100;
printf("You have withdrawn Rs100. Your balance is %f\n",bal);
printf("\t\tPlease Collect Your Cash\n");}
else printf("Insufficient Funds in account");
break;
case 2: printf("You have chosen to withdraw Rs500.\n");
if(bal>=500){
bal-=500;
printf("You have withdrawn Rs500. Your account balance is %f\n",bal);
printf("\t\tPlease Collect Your Cash\n"); }
else printf("insufficient Funds");
break;
case 3: printf("You have chosen to withdraw Rs1000\n");
if(bal>=1000){
bal-=1000;
printf("You have withdrawn Rs1000. Your account balance is %f\n",bal);
printf("\t\tPlease Collect Your Cash");}
else printf("Insufficient Funds");
break;
default: printf("\t\tWrong Choice Entered");  } }   }
while(n1!=5) ;
getch();  }
2
Contributors
3
Replies
50
Views
3 Months
Discussion Span
Last Post by Gribouillis
1

See how your code looks better after I used the astyle beautifier

astyle --style=python --align-pointer=middle --max-code-length=79 atm.c

For human readers, formatting matters in programming

#include<stdio.h>
#include<conio.h>
void main() {
    float bal=0,dep=0,withd=0;
    int n1,n2;
    clrscr();
    do {
        clrscr();
        printf("\n\t\t************************\t\t\n");
        printf("\t\tWelcome To ATM Services\n");
        printf("\t\tSelect Your Choice:\n");
        printf( "\t\t1)Deposit Cash\n\t\t2)Withdraw Cash\n\t\t3)Bank Statement\n\t\t4)Fast Cash\n\t\t5)Exit\n");
        printf("\t\t***********************\t\t\n");
        scanf("%d",&n1);
        switch(n1) {
        case 1: printf("Enter the amount to be deposited\n");
            scanf("%f",&dep);
            if(dep>0) {
                bal+=dep;
                printf("You have deposited %f cash. Your current account balance is %f\n",dep,
                    bal); }
            else printf("Invalid amount entered\n");
            printf("Press any key to continue\n");
            break;
        case 2: printf("Enter the amount to be withdrawn\n");
            scanf("%f",&withd);
            if(withd<bal && withd>0) {
                bal-=withd;
                printf("You have withdrawn %f cash. Your current account balance is %f\n",
                    withd,bal); }
            else printf("Insufficient funds in your account\n");
            printf("Press any key to continue\n");
            break;
        case 3:  printf("\t\tYour Current Bank Statement Is:\n");
            printf("\t\t Your account balance is %f.\n",bal);
            printf("Enter any key to continue");
            break;
        case 4: printf("\t\tWelcome To FastCash\n");
            printf("\t\tSelect an alternative:\n");
            printf("\t\t1)Rs100\n\t\t2)Rs500\n\t\t3)Rs1000\n");
            scanf("%d",&n2);
            switch(n2) {
            case 1: printf("You have chosen to withdraw Rs100\n");
                if(bal>=100) {
                    bal-=100;
                    printf("You have withdrawn Rs100. Your balance is %f\n",bal);
                    printf("\t\tPlease Collect Your Cash\n"); }
                else printf("Insufficient Funds in account");
                break;
            case 2: printf("You have chosen to withdraw Rs500.\n");
                if(bal>=500) {
                    bal-=500;
                    printf("You have withdrawn Rs500. Your account balance is %f\n",bal);
                    printf("\t\tPlease Collect Your Cash\n"); }
                else printf("insufficient Funds");
                break;
            case 3: printf("You have chosen to withdraw Rs1000\n");
                if(bal>=1000) {
                    bal-=1000;
                    printf("You have withdrawn Rs1000. Your account balance is %f\n",bal);
                    printf("\t\tPlease Collect Your Cash"); }
                else printf("Insufficient Funds");
                break;
            default: printf("\t\tWrong Choice Entered");  } }   }
    while(n1!=5) ;
    getch();  }

How do you know that the code goes in an infinite loop ? The only loop here is the do {...} while(n1 != 5);. What happens if you enter 5 for n1 ?

Edited by Gribouillis

0

When I run the code, after the first input the output screen goes into an infinite loop.
If I enter 5 for n1, that corresponds to the 5th case of the ATM menu which is for exiting the menu. I employed the do-while loop only if the user wants another go at running the algorithm.
Do tell if there are any logical errors in the code.
Thanks.

Edited by Sumit_12

0

I removed the calls clrscr() and getch() because I'm on Linux. When I run the program, it doesn't go into an infinite loop. When I enter 5 in the main menu, it exits the program as expected. I don't think there are logical errors except perhaps some misplaced messages to type any character to continue. What makes you think the program enters an infinite loop?

Edited by Gribouillis

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.