-5

In my code second scanf doesn't work and couldn't read the character. How can I solve that ?

#include <stdio.h>
int main() {
int a, result;
// ***************** Menu *******************
printf("Hello !!! \n The Operations That This Calculator Can Do :");
printf("\n1. Simple Operations");
printf("\n2. Calculate The Biggest Number");
printf("\n3. Taylor expansion");
printf("\n4. Sum Digits Of a Number");
printf("\n5. Found The Prime Numbers Before The Number That You Entered");
printf("\nEnter The Number Of Operation That You Want : ");
scanf_s("%d", &a);
if (a == 1){
    char ch;
    int num1, num2;
    printf("\n Please Enter Your Operation Like That (- 5 3 ) : ");
    scanf_s("%c", &ch);
    if (ch == '-'){ scanf_s("%d", &num1); scanf_s("%d", &num2); result = num1 - num2; printf("\n > %d", result); }
    if (ch == '+'){ scanf_s("%d", &num1); scanf_s("%d", &num2); result = num1 + num2; printf("\n > %d", result); }  }  return 0; }

1 Answers1

0

try this : I changed scanf_s() to scanf and gave space to %c as the %c conversion specifier won't automatically skip any leading whitespace, so if there's a stray newline in the input stream (from a previous entry, for example) the scanf call will consume it immediately.

#include <stdio.h>
int main() {
int a, result;
// ***************** Menu *******************
printf("Hello !!! \n The Operations That This Calculator Can Do :");
printf("\n1. Simple Operations");
printf("\n2. Calculate The Biggest Number");
printf("\n3. Taylor expansion");
printf("\n4. Sum Digits Of a Number");
printf("\n5. Found The Prime Numbers Before The Number That You Entered");
printf("\nEnter The Number Of Operation That You Want : ");
scanf("%d", &a);
if (a == 1){
char ch;
int num1, num2;
printf("\n Please Enter Your Operation Like That (- 5 3 ) : ");
scanf(" %c", &ch);
if (ch == '-'){ scanf(" %d", &num1); scanf(" %d", &num2); result = num1 - 
num2; printf("\n > %d", result); }
if (ch == '+'){ scanf(" %d", &num1); scanf(" %d", &num2); result = num1 + 
num2; printf("\n > %d", result); }  }
system("pause"); }