-4

I am trying to code a calculator in C and it works fine, but the second time I run it, it doesn't work. I've tried renaming the label I use to restart the program, I've tried putting the label in a different place, I've tried putting the label in a different place. I am running windows 10.

Here's my code:

#include <stdio.h>


 int main ()
{               //main body

char calctype;      //adding variables

  double userinput, input2; //adding variables

Loop: 
  printf ("\n");
  printf("Hi! Welcome to the calculator. Please enter a calculating type to continue. (+, -, *, /).");
  scanf ("%c,", &calctype);
  printf("\n");
  printf ("Next, enter your numbers.");
  scanf ("%lf %lf", &userinput, &input2);


  switch (calctype)
    {               //main body 2
    case '+':
      printf ("%.1lf + %.1lf = %.1lf", userinput, input2, userinput + input2);  /*detecting operator
                                           and calculating */
      break;

    case '-':
    printf ("%.1lf - %.1lf = %.1lf", userinput, input2, userinput - input2);
    break;
    
    case '*':
    printf ("%.1lf * %.1lf = %.1lf", userinput, input2, userinput * input2);
    break;
    
    case '/':
    printf ("%.1lf / %.1lf = %.1lf", userinput, input2, userinput / input2);
    break;
    
    default:
    printf ("The specified operator is not defined");
    break;
    
    }
    
goto Loop;
  
  
  return 0;
}

Here is the output:

Hi! Welcome to the calculator. Please enter a calculating type to continue. (+, -, *, / ).+ 
Next, enter your numbers.
12345 1234 
12345.0 + 1234.0 = 13579.0 
Hi! Welcome to the calculator. Please enter a calculating type to continue. (+, -, *, / ).+ 
Next, enter your numbers.The specified operator is not defined 
Hi! Welcome to the calculator. Please enter a calculating type to continue. (+, -, *, / ).
  • 1
    `scanf ("%c,", &calctype);` ==> `scanf (" %c", &calctype);` with an added space and a comma removed. Please see [scanf() leaves the newline char in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer). – Weather Vane Mar 24 '21 at 19:48
  • Silly question - why do you have `"%c,"` (with the comma after the %c) as your scanf pattern? – tsc_chazz Mar 24 '21 at 19:50
  • Get rid of the `goto`. Use a `while`. Set a condition you can use to terminate the loop, like `while (!done)`. – tadman Mar 24 '21 at 20:07
  • A Stack Overflow question should not be about your program or your assignment (or you), but about _your problem_, with only the shortest program that lets others see that problem themselves when run without changes, and a title that describes that problem. (See the [mre] definition re: the former). – Charles Duffy Mar 24 '21 at 21:27

2 Answers2

0

The reason for this behavior is because of scanf.

When scanf is waiting for input character (%c) it will take the first character it will find at standard input buffer (stdin).

So with your code when you are giving the first input for the first time and you are storing a character (+,*,-,/) to char calctype; and you are pressing enter aswell which equals to the character \n.

That \n will be stored to stdin and will not be used until it finds a scanf that waits a (%c) character.

So when you finish the first iteration of the program and you are getting your result, stdin will look something like this -->[\n,\n] and then you will start over from the start and at that point is where the things goes bad and you are getting that behavior.

The reason for this is because scanf will take automatically the first character it will find at stdin.

So what will happen?

scanf will take \n as value and it will store it to char calctype.

And then you will get what's expected the default of switch statement.

So what's the solution to this?

We have to somehow remove those \n and we remove them with getchar().

Check the solution below.

Solution:

#include <stdio.h>


 int main ()
{               //main body

char calctype;      //adding variables

  double userinput, input2; //adding variables

Loop: 
  printf ("\n");
  printf("Hi! Welcome to the calculator. Please enter a calculating type to continue. (+, -, *, /).");
  scanf ("%c", &calctype); <==== Remove ,
  getchar(); <===== We add getchar() function so it removes the \n character from buffer.
  printf("\n");
  printf ("Next, enter your numbers.");
  scanf ("%lf %lf", &userinput, &input2);
  getchar(); <===== Same goes here.


  switch (calctype)
    {               //main body 2
    case '+':
      printf ("%.1lf + %.1lf = %.1lf", userinput, input2, userinput + input2);  /*detecting operator
                                           and calculating */
      break;

    case '-':
    printf ("%.1lf - %.1lf = %.1lf", userinput, input2, userinput - input2);
    break;
    
    case '*':
    printf ("%.1lf * %.1lf = %.1lf", userinput, input2, userinput * input2);
    break;
    
    case '/':
    printf ("%.1lf / %.1lf = %.1lf", userinput, input2, userinput / input2);
    break;
    
    default:
    printf ("The specified operator is not defined");
    break;
    
    }
    
goto Loop;
  
  
  return 0;
}

However you shouldn't really use goto if you don't wanna end up with spaghetti code and you could use getchar() to get an actual character instead of scanf.

Spiros Gkogkas
  • 432
  • 5
  • 11
-1

Use this one it will work fine for you:

#include <stdio.h>


 int main ()
{               //main body

  char calctype;      //adding variables

  double userinput, input2; //adding variables

  int exitfrom;
  printf ("Enter negative number to exit 0 to continue :");
  scanf ("%d",&exitfrom);
  while(exitfrom>=0)
  {

  printf("\n");
  printf ("Next, enter your numbers.");
  scanf ("%lf %lf", &userinput, &input2);
  printf ("Enter calculations type(+ ,- ,× ,/ ):");
  scanf("%s",&calctype);


  switch (calctype)
    {               //main body 2
case '+':
      printf ("%.1lf + %.1lf = %.1lf", userinput, input2, userinput + input2);  /*detecting operator
                                       and calculating */
      break;

      case '-':
    printf ("%.1lf - %.1lf = %.1lf", userinput, input2, userinput - input2);
    break;

    case '*':
    printf ("%.1lf * %.1lf = %.1lf", userinput, input2, userinput * input2);
      break;

    case '/':
    printf ("%.1lf / %.1lf = %.1lf", userinput, input2, userinput / input2);
    break;

    default:
    printf ("The specified operator is not defined");
    break;

    }

  }
  return 0;
  }
H Hasan
  • 55
  • 1
  • 8