The question is: Write a program Q3.c that does the following steps:
- Initially prompt the user to enter the balance amount.
- 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.
How do I prevent this?