0

I am new to Programming (starting with C), and tried to practice functions by building a calculator. But it only ever returns me the same function even though its If-Statement is not being called. This is my code:

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

int result;
int multiplication(int num1, int num2){

    result = num1 * num2;
    return result;
};

int addition(int num1, int num2){

    result = num1 + num2;
    return result;
};

int substraction(int num1, int num2){

    result = num1 - num2;
    return result;
};

int main(){
    int num1;
    int num2;
    char Math;

    printf("Do you want to do a Multiplication or an Addition, or a Substraction: ");
    scanf("%c", &Math);
    printf("Now give me a Number: ");
    scanf("%d",&num1);
    printf("Now give me another Number: ");
    scanf("%d",&num2);

    if(Math = 'M' || 'm'){
        printf("Your Mulitplication came out to %d", multiplication(num1,num2));
}
    else if(Math = 'A' || 'a'){
        printf("Your Addition came out to %d", addition(num1, num2));
}
    else if(Math = 'S' || 's'){
        printf("Your Substraction came out to %d", substraction(num1, num2));
}
    else{
        printf("Your Input was wrong");
};

return 0;



}

I would really appreciate every bit of advice i can get!

Dav7538
  • 63
  • 7
  • 2
    A few things I spotted. 1) "=" is an assignment operator. You want to use "==" in the if statement. 2) to compare if Var is 'X' or 'Y' you need to do "Var== 'X' || Var == 'Y' – Simon F Jan 25 '19 at 09:03
  • 1
    these are very common mistakes for beginners to make, and come up here frequently - eg. : [If statements not working?](https://stackoverflow.com/questions/14289635/if-statements-not-working), [Why is if statement not working](https://stackoverflow.com/questions/38554633/why-is-if-statement-not-working) – Sander De Dycker Jan 25 '19 at 09:12
  • You should always enable warnings in your compiler. For GCC you canuse -Wall -Wextra. This might show you some warnings "assignment in condition" or similar. And please always take care about all warnings. – Gerhardh Jan 25 '19 at 10:24

2 Answers2

5

This here

if(Math = 'M' || 'm')

Needs to be changed to

if((Math == 'M') || (Math == 'm'))

Because as it is, 'M' || 'm' just turns into 1, which is then assigned to Math, and that result is returned, which means the if is taken. With this change, you're actually comparing Math to 'M', and if it's not equal, to 'm'.

Likewise for else if(Math = 'A' || 'a') and so on.

Blaze
  • 16,736
  • 2
  • 25
  • 44
  • 1
    The second 'M' should be small 'm', but it's pretty clear what you meant. – visibleman Jan 25 '19 at 09:11
  • @visibleman thanks for pointing this out. I just noticed I made this mistake twice - once in the code and once in the explanation. – Blaze Jan 25 '19 at 09:13
0

note (=) is diffrent from (==)

= is assignment operator it assigns the value in your code you are assigning the value 'M' to Math and logical operators return either 0 or 1. in this case, it returns 1 since 1 is returned control flows inside if and the result of the multiplication is obtained as output