I was doing some amateur C practice. As you can see I'm trying to make a slightly more advanced calculator that allows the user to decide what they want to do. Even though I clearly defined what to write in if() function, it wont work at all. I would type in "add" or "mul" in the console but it always returns, "type a given mathematical function"
which is an outcome that I put in there to tell the user that they've given the wrong input. So far I've tried adding and removing the quotation mark in if() and replaced ==
with =
. None of these work.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num1;
int num2;
int mfunc;
printf("type in a mathematical function: add, subtract, divide or multiply ");
scanf_s("%d", &mfunc);
if (mfunc == "add") {
printf("enter first number: ");
scanf_s("%d", &num1);
printf("enter a second number: ");
scanf_s("%d", &num2);
printf("sum of two number is: %d", num1 + num2);
}
else if (mfunc == "sub") {
printf("enter first number: ");
scanf_s("%d", &num1);
printf("enter a second number: ");
scanf_s("%d", &num2);
printf("difference is: %d", num1 - num2);
}
else if (mfunc == "div") {
printf("enter first number: ");
scanf_s("%d", &num1);
printf("enter a second number: ");
scanf_s("%d", &num2);
printf("quotent is: %d\n", num1 / num2);
}
else if (mfunc == "mul") {
printf("enter first number: ");
scanf_s("%d", &num1);
printf("enter a second number: ");
scanf_s("%d", &num2);
printf("product is: %d", num1 * num2);
}
else {
printf("type a given mathematical function");
}
return 0;
}