#include<stdio.h>
void main(){
char choice1,choice2;
int a,b,c;
while(choice1 != 'n'){
printf("Enter + for addition:\n");
printf("Enter - for substraction:\n");
printf("Enter * for multiplication:\n");
printf("Enter / for division:\n");
scanf("%c",&choice2);
printf("Enter two numbers:\n");
scanf("%d %d",&a,&b);
if (choice2 == '+'){
c=a+b;
printf("Addition = %d",c);
}
else if (choice2 == '-'){
c=a-b;
printf("Substraction = %d",c);
}
else if (choice2 == '*'){
c=a*b;
printf("Multiplication = %d",c);
}
else if (choice2 == '/'){
c=a/b;
printf("Division = %d",c);
}
else{
printf("Invalid choice!");
}
printf("\nEnter y to continue and n to exit:\n ");
scanf("%c",&choice1);
}
}
When I run the above program, the while loop repeats without taking the value of choice1 from the user. Can anyone please tell me what is wrong in the above code???
do whileinstead ofwhile.while(choice1 != 'n')first time around,choice1is uninitialized, so you have undefined behaviour.choice1is being set as the newline character you haven't collected after the lastscanf()call.