0

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.

melpomene
  • 84,125
  • 8
  • 85
  • 148

2 Answers2

1

After you read a char you should clean your buffer. Don't use fflush(stdin) — it is a bad practice. You can add this function instead:

void clean_stdin(void)
{
    int c;
    do {
        c = getchar();
    } while (c != '\n' && c != EOF);
}

and call it after the `scanf:

 scanf("%c", &operator);
 clean_stdin();`

About the GOTO: you can use a loop — probably a while loop or perhaps a for loop or do … while loop instead. These are easier to understand than using goto statements and labels.

Update

Or, as @BLUEPIXIE suggests, you can change your scanf in this way:

scanf(" %c", &operator); // adding a space before %c
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
granmirupa
  • 2,780
  • 16
  • 27
0

The problem is actually not the goto statement but the fact that a scanf("%c",...) will consume a new line, which is probably in the in-buffer after a previously entered integral value.

Suppose the following code:

int main() {

    char operator;
    int a;

    printf("Enter an integral value:\n " );
    scanf("%d",&a);
    printf("Try to enter an operator (will probably be skipped):\n " );
    scanf("%c", &operator);

    if (operator == '\n')
        printf("You entered a new line (did you?)\n");
    else
        printf("You entered: %c\n", operator);

    return 0;
}

If you enter an integral value and press <enter>, then a new line character will remain in the buffer. This is immediately consumed by the subsequent scanf-statement. So if you enter, for example; 19 and press <enter>, then you'll get the following output:

Enter an integral value: 19
Try to enter an operator (will probably be skipped):
 You entered a new line (did you?)

However, if you use - as suggested by melpomene - the following statement:

scanf(" %c", &operator);

then scanf will consume an arbitrary long sequence of white space characters (including new lines) before actually reading in the first non-white-space character into %c->&operator:

Enter an integral value:
 19
Enter an operator:


+
You entered: +
Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58