Why my if statements will not recognize my characters inputted for char

/*Write a program that mimics a calculator. The program should take as input
two integers and the operation to be performed. It should then output the
numbers, the operator, and the result.(For division, if the denominator is
zero, output and appropriate message.) Some sample outputs follow:
3+4=7
13*5=65*/

#include <stdio.h>

int main()

{

int a,c;
char b;

printf("Input first integer: ");
scanf_s("%i", &a);

printf("What's the operation to be performed: ");
scanf_s("%s", &b);

printf("Input second integer: ");
scanf_s("%i", &c);

int A,B,C;
float D;

A = a + c;
B = a - c;
C = a * c;
D = a / c;

if (c == 0 && b == '/')

{
printf("The solution to the information given is : %i / %i = The denominator is zero and cannot be calculated\n\n", a,c);
}

else if (b == '-')

{
printf("The solution to the information given is : %i - %i = %i\n\n", a,c,B);
}

else if (b == '*')

{
printf("The solution to the information given is : %i * %i = %i\n\n", a,c,C);
}

else if (b == '/')

{
printf("The solution to the information given is : %i / % = %.2f\n\n", a,c,D);
}

else

{
printf("The solution to the information given is : %i + %i = %i\n\n", a,c,A);
}

}



my c coding is a little rusty but take a look at this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <stdio.h>

int main()

{

	int a,c;
	char b;

	printf("Input first integer: ");
	scanf("%i", &a);
	getchar();

	printf("What's the operation to be performed: ");
	scanf("%c", &b);
	getchar();

	printf("Input second integer: ");
	scanf("%i", &c);
	getchar();

	if (c == 0 && b == '/')

	{
		printf("The solution to the information given is : %i / %i = The denominator is zero and cannot be calculated\n\n", a,c);
	}

	else if (b == '-')

	{
		printf("The solution to the information given is : %i - %i = %i\n\n", a,c, a - c);
	}

	else if (b == '*')

	{
		printf("The solution to the information given is : %i * %i = %i\n\n", a,c, a * c);
	}

	else if (b == '/')

	{
		printf("The solution to the information given is : %i / %i = %.2f\n\n", a,c, (double)a / c);
	}

	else

	{
		printf("The solution to the information given is : %i + %i = %i\n\n", a,c, a + c);
	}

	getchar();
	return 0;

}
Last edited on
Better make use of C++ features. Avoid using C functions such as printf, scanf..
its for school and printf and scanf is whats told to be used
Tried the code above given by karnisr and I still get the same result
I still get the same result


What result is that?
Disch it wont recognize the char character and doesnt run through the if statements just the last one which is else
scanf returns a value. Maybe you need to pay attention to it?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <stdio.h>

int main(void)
{
    int a;
    printf("Input first integer: ");
    if (1 != scanf(" %i", &a))
    {
        printf("Input error\n");
        return 0;
    }

    char op;
    printf("Input operation: ");
    if (1 != scanf(" %c", &op))
    {
        printf("Input error\n");
        return 0;
    }

    int b;
    printf("Input second integer: ");
    if (1 != scanf(" %i", &b))
    {
        printf("Input error\n");
        return 0;
    }

    printf("The solution for the information given is: %i %c %i = ", a, op, b);

    if (op == '*')
        printf("%i\n", a * b);
    else if (op == '/')
    {
        if (b == 0)
            printf("undefined\n");
        else
            printf("%i\n", a / b);
    }
    else if (op == '-')
        printf("%i\n", a - b);
    else if (op == '+')
        printf("%i\n", a + b);
    else
        printf("unsupported\n");
}
well thank you for solving the problem but you did not explain why this is done as i can just type certain things but doesnt mean i will understand why it is done
well thank you for solving the problem but you did not explain why this is done as i can just type certain things but doesnt mean i will understand why it is done


I trust you're capable of doing the work to understand it. It isn't so hard to click on "Reference" in the upper left hand section of this page and find your way to the documentation for scanf.
thank you cire I appreciate the help
Topic archived. No new replies allowed.