# Thread: What am I doing wrong

1. ## What am I doing wrong

I am a beginner and attempted to make an calculator which uses i else to add or subtract. Whenever i choose add it still subtracts.Here is the code:
Code:
```#include <stdio.h>
main()
{
int x, y, z, sum, sub;   /* make variables for storing numbers to be added or subtracted and the variables to store the sum and difference */
char type; /* variable to determine addition or subtraction */
printf("This calculator only knows how to add and subtract, so enter you want to add or subtract by A for add and anything else for subtract.\n ");
scanf(" %c", &type);
if (type == "A") /* if the user's input is A then there will be addition */
{
printf("Type the first number.\n "); /* inputs first number */
scanf(" %d", &x);
printf("Type the second one.\n ");
scanf(" %d", &y);
printf("Type the last one.\n ");
scanf(" %d", &z);
sum = x + y + z; /* determines the value of the sum*/
printf("The sum is: %d", sum); /* prints the sum*/
}
else /*otherwise there will be subtraction*/
{

printf("Type the first number.\n ");
scanf(" %d", &x);
printf("Type the second one.\n ");
scanf(" %d", &y);
printf("Type the last one.\n ");
scanf(" %d", &z);
sub = x - y - z;
printf("The difference is: %d", sub);
}
return 0;
}```
Even if I type A the calculator would subtract the numbers

2. scanf scans the stdin stream and tries to convert the input string to the object pointed using the format specified...
scanf returns the number of successful conversions made... if you don't check this, no data is writen to the target object. You could do:
Since the conversion fails (and scanf will return 0 [or -1 in case of error]), stdin sill has the previous string to be rescanned. There is no portable way to flush the input stream (and using fflush( stdin ) is an undefined behavior or a plain ERROR).

3. Not paying attention to your compiler error messages would be something you're doing wrong.
Code:
```main.c:2:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
main()
^~~~
main.c: In function ‘main’:
main.c:8:14: warning: comparison between pointer and integer
if (type == "A") /* if the user's input is A then there will be addition */
^~```
If you're not seeing this, or something like it, then you need to either increase the warning level or get a better compiler.

For the comparison, use
Code:
` if (type == 'A' )`
Single quotes are for lone characters.
Double quotes are for strings.

4. Thanks, I didn't notice the warning but it still says:
Code:
```warning: return type defaults to 'int' [-Wimplicit-int]|
||=== Build finished: 0 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|```

5. Originally Posted by Silver2000
Thanks, I didn't notice the warning but it still says:
Code:
```warning: return type defaults to 'int' [-Wimplicit-int]|
||=== Build finished: 0 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|```
main() should be explicitly defined to return an int.
Code:
```int main(void)
{
// ...
return 0;
}```

6. Thanks, I should have done more research. This board really is helpful.