0

This code supposed to: 1 - take input balance on your bank atm 2 - identify credit or debit transaction 3 - Using switch case to perform bank transaction 4 - Display new balance

I have to change the scanf() function in line 9 to include the \n, the program does what it does even though I'm not sure if there is a better workaround. What can I do to improve my code in line 9?

#include <stdio.h>
int main()
{
    float amt, debit, credit;
    char acct;
    printf("Enter initial amount:\n");
    scanf("%f",&amt);
    printf("Enter c for credit\n d for debit\n b for balance\n");
    scanf ("\n%c",&acct);
    switch(acct)
        {
        case 'c':
            printf("Enter the credit amount:\n");
            scanf("%f",&credit);
            amt += credit;
            printf("New amount=%f",amt);
            break;
        case 'd':
            printf("Enter the debit amount:\n");
            scanf("%f",&debit);
            if(amt>=debit)
            {
            amt -= debit;
            printf("New amount=%f",amt);
            }
            else 
            printf("insufficient amount\n");
            break;
        case 'b':
            printf("Your balance is: %f",amt);
            break;
        default:
            printf("invalid input");
            break;
        }
}
phatx88
  • 35
  • 5

2 Answers2

0

When you write a letter and press enter, two characters will be placed at the keyboard buffer: what you wrote + the new line special character \n.

When you use %d to read a number, you will not get any problem because the scanf() will eat any special character to give you only the number.

But when you want to read a character using %c, the scanf() will return the first character from the keyboard buffer, which is a \n if you have type something before and pressed enter.

So what you need to do is to clean that special new line character after pressing enter. There are few ways to do that.

  1. Using a space before the %c
scanf(" %c")
  1. Using fflush(stdin) to clean the keyboard buffer
fflush(stdin);
  1. Calling getchar() to eat the \n character
getchar()

A more detailed explanation can be found here: Why is this statement printed twice in while loop?

mdze
  • 71
  • 4
  • 1
    Who or what text suggest `fflush(stdin);`? – chux - Reinstate Monica Jun 28 '20 at 04:46
  • 1
    OP is already doing `scanf ("\n%c",&acct);`. Do you suggest that this is different form `scanf (" %c",&acct);`? – chux - Reinstate Monica Jun 28 '20 at 04:48
  • 1
    Thanks for noticing this. I think I expressed myself poorly and did not answer the OP question that I would like to know about a better alternative. I don't want to suggest using fflush as a good practice, I was trying to explain why there is a special character in the keyboard buffer and also listing alternatives to eliminate it from the buffer. I would like to add that the use of fflush is not recommended because of its undefined behavior. And also that the use of `blank space` will have the same behavior as `\n` in the scanf implementations that I checked. – mdze Jun 28 '20 at 20:14
0

The simplest solution for your problem is to declare your variables. Ex:

char acct = ' ';

acct can be any value, so long as it's not '\n' or '\0'. This value will then be overridden by your scanf or getchar

Danish Saeed
  • 49
  • 1
  • 5