0

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;
}
Chris
  • 26,361
  • 5
  • 21
  • 42
lmgesus
  • 9
  • 4
  • 4
    `scanf_s("%d", &mfunc);` is reading an integer, but the prompt makes it seem like you're interested in a string. `if (mfunc == "add")` also seems to imply you believe you have a string, but even if you did that's not how to compare them for equality. You need to use `strcmp`. It's also important to know when things go wrong. `scanf_s` and other functions have return values for this reason. If you'd checked to see if the input was converted you would have known it was not and there was a problem. – Retired Ninja Nov 02 '21 at 06:35
  • You need to study how strings work in C – Support Ukraine Nov 02 '21 at 06:51
  • @RetiredNinja They do know that there is a problem. But using the return value they would know where. I wish teachers would consequently reduce points for any code which ignores those return values. Not because it is needed for code (not in all circumstances), but to help pupils to help themselves. – Yunnosch Nov 02 '21 at 06:53
  • 2
    Which C compiler are you using? I don't know of any that wouldn't warn about things in this program - At least not with a certain amount of compiler options added. – Ted Lyngmo Nov 02 '21 at 06:57
  • @TedLyngmo visual studio 2019 – lmgesus Nov 02 '21 at 07:01
  • Fixed a couple of obvious stupid mistakes like changing "int mfunc" to "char mfunc". Embarrassing mistake but code still refuses to work properly. – lmgesus Nov 02 '21 at 07:04
  • Are you using `/std:c11` or `/std:c17`? I don't even need to add `/W4` or `/permissive-` to see warnings/errors when I try it in [compiler explorer](https://godbolt.org/z/hbqqocb84) – Ted Lyngmo Nov 02 '21 at 07:19

1 Answers1

-1

you can't compare "add" with myfunc. add is a string (array of chars of size 4), myfunc is an int. In C you can't compare int to string. you can compare string to string so you can ask from the user to input his func by write "add", but it is a bit more complex for you right now (you need to scan it to a proper array of chars and compare the strings using function which might be case sensitive, etc.). also, you can use instruction for the user - some kind of map so the user could insert 0 for add, 1 for sub and so on..

also, I used "define" witch is macro (an pre-processor directive) to more readable code;


#include <stdio.h>
#include <stdlib.h>

#define ADD 0
#define SUB 1
#define DIV 3
#define MUL 4


int main()
{
int num1;
int num2;
int mfunc;

printf("type in a mathematical function: add, subtract, divide or multiply \n");
printf("to add insert 0,\nto subtract insert 1,\n");
printf("to divide insert 2,\nto multiply insert 3\n");
scanf("%d", &mfunc);

if (mfunc == ADD) {
    printf("enter first number: ");
    scanf("%d", &num1);
    printf("enter a second number: ");
    scanf("%d", &num2);
    printf("sum of two number is: %d", num1 + num2);
}
else if (mfunc == SUB) {
    printf("enter first number: ");
    scanf("%d", &num1);
    printf("enter a second number: ");
    scanf("%d", &num2);
    printf("difference is: %d", num1 - num2);
}
else if (mfunc == DIV) {
    printf("enter first number: ");
    scanf("%d", &num1);
    printf("enter a second number: ");
    scanf("%d", &num2);
    printf("quotent is: %d\n", num1 / num2);
}
else if (mfunc == MUL) {
    printf("enter first number: ");
    scanf("%d", &num1);
    printf("enter a second number: ");
    scanf("%d", &num2);
    printf("product is: %d", num1 * num2);
}
else {
    printf("type a given mathematical function");
}

return 0;
}

if you really want the user to input string you should be do that:


#include <stdio.h>
#include <stdlib.h>
#include <string.h>



int main()
{
int num1;
int num2;
char mfunc[256];

printf("type in a mathematical function: add, subtract, divide or multiply \n");

gets(mfunc);

if (strcmp(mfunc, "ADD") == 0||strcmp(mfunc, "add") == 0) {
    printf("enter first number: ");
    scanf("%d", &num1);
    printf("enter a second number: ");
    scanf("%d", &num2);
    printf("sum of two number is: %d", num1 + num2);
}
else if (strcmp(mfunc, "SUB") == 0||strcmp(mfunc, "sub") == 0) {
    printf("enter first number: ");
    scanf("%d", &num1);
    printf("enter a second number: ");
    scanf("%d", &num2);
    printf("difference is: %d", num1 - num2);
}
else if ((strcmp(mfunc, "DIV") == 0||strcmp(mfunc, "div") == 0)) {
    printf("enter first number: ");
    scanf("%d", &num1);
    printf("enter a second number: ");
    scanf("%d", &num2);
    printf("quotent is: %d\n", num1 / num2);
}
else if ((strcmp(mfunc, "MUL") == 0||strcmp(mfunc, "mul") == 0)) {
    printf("enter first number: ");
    scanf("%d", &num1);
    printf("enter a second number: ");
    scanf("%d", &num2);
    printf("product is: %d", num1 * num2);
}
else {
    printf("type a given mathematical function");
}

return 0;
}

I know I used gets(), see paul-kapustin in this Q. How to read a line from the console in C?

Asaf Itach
  • 300
  • 6
  • 13
  • So if I change "#define ADD 0" to "#define ADD add" will I get the program I want which is typing in "add" in the console to add two numbers? – lmgesus Nov 02 '21 at 07:00
  • No, you are scanning integer from the user scanf("%d", &mfunc); so the user can't insert "add" he can only insert numbers. if you want the user to insert string it will be more complicated – Asaf Itach Nov 02 '21 at 07:04
  • 3
    @Imgesus: You have already been told that strings are not first class citizens in C language. They are just *by convention* represented with null terminated character arrays, and the standard library has a bunch of functions to copy or compare such arrays. But you cannot use the standard operators (`=`, `==`, ...) on C-strings. – Serge Ballesta Nov 02 '21 at 07:05
  • This does not really help the OP since she wants to enter words, not numbers. – the busybee Nov 02 '21 at 07:10
  • @AsafItach I just found out that my original code had "int" instead of "char", but even with the change I think I need to approach this entire code differently from scratch if I want it to work in the way that I originally intended. – lmgesus Nov 02 '21 at 07:12
  • @lmgesus if it was helpful I will appreciated if you could choose my answer, Tnx – Asaf Itach Nov 02 '21 at 07:47
  • 1
    Don't use `gets` ! Use `fgets` instead – Support Ukraine Nov 02 '21 at 08:10
  • Don't need the defines in the string example. Don't really need the int example either since that was never the intent. `gets` is not available at all in Visual Studio. Alternatives are `fgets` or `gets_s`. If `fgets` is used you should explain how it also captures the newline and how to remove it. I'd convert the input to lowercase first so things like "aDd" would work. You could also use the non standard `_stricmp` in Visual Studio with the original string. – Retired Ninja Nov 02 '21 at 12:32