I'm creating a simple calculator for a class assignment in C but I'm running into an issue where when it's running the loop I get a certain line printing out twice. Basically I have an introduction portion (not included in the code below) that shows the user possible operations my calculator can do. I put the scan in a while loop because I want it to continuously prompt the user to select an operation after completing one unless they select 'E'. However when the user finishes the first operation, the "Select an operation:" prompt will be printed to the screen twice instead of once.
while(select != 'E'){
printf("\nSelect an operation: \n");
scanf("%c", &select);
if(select == 'E'){
break;
}
else if(select == 'A'){
printf("\nEnter first number: \n");
scanf("%d", &num1);
printf("\nEnter second number: \n");
scanf("%d", &num2);
answer = num1 + num2;
printf("The result is: %d", answer);
printf("\n");
}
else if(select == 'S'){
printf("\nEnter first number: \n");
scanf("%d", &num1);
printf("\nEnter second number: \n");
scanf("%d", &num2);
answer = num1 - num2;
printf("The result is: %d", answer);
printf("\n");
}
else if(select == 'M'){
printf("\nEnter first number: \n");
scanf("%d", &num1);
printf("\nEnter second number: \n");
scanf("%d", &num2);
answer = num1 * num2;
printf("The result is: %d", answer);
printf("\n");
}
else if(select == 'D'){
printf("\nEnter first number: \n");
scanf("%d", &num1);
printf("\nEnter second number: \n");
scanf("%d", &num2);
answer = num1 / num2;
printf("The result is: %d", answer);
printf("\n");
}
}```
Aucun commentaire:
Enregistrer un commentaire