0

The question is: Write a program Q3.c that does the following steps:

  1. Initially prompt the user to enter the balance amount.
  2. Ask the user to enter one of the following options:

○ ‘A’ followed by an amount.

■ On receiving this option, add the amount to the balance.

■ Display the balance.

■ Go to step 2.

○ ‘S’ followed by an amount.

■ On receiving this option, subtract the amount from the balance.

■ Display the balance.

■ Go to step 2.

○ ‘E’

■ On receiving this option, break from the loop and exit the program.

This is my code:

int main()
{
    int bal,new;
    char x;
    printf("Enter Balance: ");
    scanf("%d",&bal);
    
  for (int a=1; a>0;)
    {   
        printf("%dEnter Option: ",a);
        scanf("%c",&x);
        if (x=='E')
        {
            printf("Exiting...\n"); 
            break;
            return(0);
        }
        scanf("%d",&new);
        if (x=='A')
        {
            //bal = bal + new; 
            printf("Balance: %d\n",bal+new);
        }
        else if (x=='S')
        {
            //bal = bal - new; 
            printf("Balance: %d\n",bal-new);
        }
       a++; 
    }
    return 0;
}

Every time I enter a value, the loop runs another iteration while printing, like this. Terminal output

How do I prevent this?

Big Floppa
  • 21
  • 2

2 Answers2

1

regarding:

scanf("%c",&x);  

this will only input the next character in stdin which, every other time, will be a '\n'.

Suggest:

scanf(" %c",&x);

notice the leading space in the format string, which will cause the call to scanf() to consume any leading white space.

The result is that the loop only iterates one time for each user selected option.

user3629249
  • 16,402
  • 1
  • 16
  • 17
0

I think it's happening due to scanf reading newline character present in the input buffer from previous scanf read. You can avoid this by adding a getchar() after each scanf to read that trailing newline character. I would suggest using fgets() to read input into a string and sscanf() to read variables from that string.

Here is how it would look like:

printf("Enter Balance: ");
char input[100];
fgets(input, 100, stdin);
sscanf(input, "%d", &bal);

for (int a=1; a>0;) {   
    printf("%d Enter Option: ",a);
    fgets(input, 100, stdin);
    sscanf(input, "%c%d", &x, &new);
    if (x=='E') {
        printf("Exiting...\n"); 
        break;
        return(0);
    }
    if (x=='A') {
        bal = bal + new; 
        printf("Balance: %d\n",bal);
    } else if (x=='S') {
        bal = bal - new; 
        printf("Balance: %d\n",bal);
    }
    a++; 
}

I tested it and it seemed to be working okay:

c-posts : $ ./a.out 
Enter Balance: 1000
1 Enter Option: A 400
Balance: 1400
2 Enter Option: S 30
Balance: 1370
3 Enter Option: A 300
Balance: 1670
4 Enter Option: S 20
Balance: 1650
5 Enter Option: E
Exiting...

If you don't want to use gets, here is the getchar() approach, it doesn't look that clean to me but it works:

printf("Enter Balance: ");
char input[100];
scanf("%d", &bal);
getchar();

for (int a=1; a>0;) {   
    printf("%d Enter Option: ",a);
    scanf("%c", &x);
    getchar();
    if (x=='E') {
        printf("Exiting...\n"); 
        break;
        return(0);
    }
    scanf("%d", &new);
    getchar();
    if (x=='A') {
        bal = bal + new; 
        printf("Balance: %d\n",bal);
    } else if (x=='S') {
        bal = bal - new; 
        printf("Balance: %d\n",bal);
    }
    a++; 
}
Rohan Kumar
  • 5,427
  • 8
  • 25
  • 40