I am working on building a calculator in C, and have encountered a problem regarding scanf for characters. I have defined a "string" called operationValue, but when I try to do the scanf function (the one that I have a comment right next to it), it immediately prints out an invalid character instead of the operation I typed in. I needed the string instead of just a character because I have the other operation for powers and roots, but even they don't work when I type them in. When I complete the code it prints "Not Valid Operation."
Sorry if this is not what is conventional in stack overflow, but this is the first time I'm on here. If the pieces of code are too long or something, please help me edit it so I can learn not to do that next time.
#include <stdio.h>
void theOperation(double num1, char operationValue, double num2)
{
if(operationValue == "+")
{
printf("%lf\n", num1 + num2);
} else if(operationValue == "-")
{
printf("%lf\n", num1 - num2);
} else if(operationValue == "*")
{
printf("%lf\n", num1 * num2);
} else if(operationValue == "/")
{
printf("%lf\n", num1 / num2);
} else if(operationValue == "pow")
{
printf("%lf\n", pow(num1, num2));
} else if(operationValue == "root")
{
printf("%lf\n", pow(num1, (1/num2)));
} else
{
printf("Not valid operation");
}
}
int main()
{
double num1, num2;
char operationValue[10];
printf("This is a calculator.\n");
printf("Enter first number: ");
scanf("%lf", &num1);
printf("%lf\n", num1);
printf("Enter operation: ");
scanf(" %s", &operationValue);
printf("%c\n", operationValue); // This line fails when I type in any operation I define: '+', '-', '*', '/' 'pow' 'root'
printf("Enter second number: ");
scanf("%lf", &num2);
printf("%lf\n", num2);
theOperation(num1, operationValue, num2);
return 0;
}