Hi I am brand new to programming in C. I am trying to create a calculator. I succeeded in doing this but when I try to make the program start over so the user can ask another question it does not work correctly. It looks something like this
Type what operation you want to do(+, -, *, /:)
*
Enter two operands:
8
8
The product of the two numbers is64
Type what operation you want to do(+, -, *, /:)
Enter two operands:
gg
Type what operation you want to do(+, -, *, /:)
Enter two operands:
Type what operation you want to do(+, -, *, /:)
Enter two operands:
It skips the first line of input and for however many characters I type it does the same thing. Here is my code
#include <stdio.h>
int main() {
start:;
char operator;
int a, b, sum, differnce, product, quotient;
printf("Type what operation you want to do(+, -, *, /:)\n");
scanf("%c", &operator);
printf("Enter two operands:\n " );
scanf("%d%d",&a,&b);
switch(operator)
{
case '+':
sum = a + b;
printf("The sum of the two numbers is:%d\n",sum);
break;
case '-':
differnce = a - b;
printf("The differnce of the two numbers is:%d\n",differnce);
break;
case '*':
product = a * b;
printf("The product of the two numbers is%d\n",product);
break;
case '/':
quotient = a / b;
printf("The quotient of the two numbers is %d\n", quotient);
break;
}
goto start;
return 0;
}
Now I know the goto command is not very good so if there is an alternative that would work I'm open to it.