-2

Why I get wrong answers for addition and multiplication sections in this code?

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

int main()
{
    char x, y, z;
    printf("Enter the calculation: ");
    scanf("%c %c %c", &x, &y, &z);

    int a = (int)x;
    int b = (int)z;
}

Problem is here:

if(y == '+'){
    printf("The answer is %d", a+b);
}

And here:

else if(y == '*'){
         printf("The answer is %d", a*b);
     }
     else{
             printf("Use only +, -, /, * signs");
     }
     return 0;
}
HDJEMAI
  • 9,436
  • 46
  • 67
  • 93
  • 2
    What is the value of `x` and `z`? Aren't they characters? So if you use a debugger, you can quickly find the answer. Learn to use a debugger; it is worth while! (Other users: let him figure it out so he learns.) – Paul Ogilvie Mar 25 '17 at 14:21
  • In general, describe what happens when you run the program - what is the "wrong answer" that it gives? – Svaberg Mar 26 '17 at 00:07

3 Answers3

2

First of all understand that you are using character variables to take user input and then typecasting them to integer variables. This typecasting will result in the value of ASCII code of the character to be copied to those integer variables. So even if you enter input as 3 + 2 , x will contain the value 51 (ASCII code) and z will contain the value 50 (ASCII code) and the answer will be 101 which, according to the code is correct. For ASCII codes,refer to http://ascii.cl/ Also, there seems to be a small mistake in your code (this may depend on the compiler being used. I use Turbo C++ which gave me error for variable declaration after any other operation like printf in this case).

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



int main()
{
char x, y, z;
int a;
int b;


printf("Enter the calculation: ");
scanf("%c %c %c", &x, &y, &z);

a=(int) x;
b=(int) z;

if(y == '+'){
printf("The answer is %d", a+b);
   }
else if(y == '*'){
    printf("The answer is %d", a*b);
   }
else{
    printf("Use only +, -, /, * signs");
   }

return 0;
}

This code works fine. If you still want to keep the same code then simply do this.

int a = x - '0';

It'll work.

Gaurav Gilalkar
  • 128
  • 2
  • 13
  • 1
    The Standard does not require that the execution character set be encoded in ASCII, and the encoding is in fact not always ASCII. But, the values of the characters from `'0'` to `'9'` _are_ required to form a continuous sequence, so that `a = x - '0'` is a reliable way to get the value represented by `x`, no matter what encoding is used. – ad absurdum Mar 25 '17 at 16:42
1

The values of characters x,y,z are ASCII characters not numbers. In ASCII, the digits 0 through 9 are represented by the characters '0' through '9' consecutively. The int value of the character '0' is 48 in decimal, character '1' is 49, and so on. Type "man ascii" from a linux prompt to see the full list of ASCII characters.

So to convert a '0' to an int value, subtract '0' from it, and you get 0. Because the characters are consecutive in the ASCII table, this works for all 10 numeric ASCII characters (this is no accident, it was designed this way). Note as David points out: While there are other encodings besides ASCII, all encodings require that the numeric characters be consecutive, so this math to convert to integers always works.

So if you have any char C which is a numeric char from '0' to '9', you can get the numeric value of it using

int i = C - '0'; 

Your values for a and b are wrong, you are converting the ASCII character value to an int using a cast, but you need to convert it by subtracting the value of the character '0':

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

int main()
{
 char x, y, z;
 printf("Enter the calculation: ");
 scanf("%c %c %c", &x, &y, &z);

 // Convert ASCII char to int by subtracting value of char '0'
 int a = x - '0';
 int b = z - '0';

if (y == '+') {
   printf("The answer is %d", a+b);
}  
else if(y == '*') {
    printf("The answer is %d", a*b);
   }
else {
    printf("Use only +, -, /, * signs");
   }
return 0;
}

For converting strings to ints, use the atoi() function mentioned in this post here:

    char *myString = "1234";
    int myIntVal = atoi(myString); 

This is a duplicate of https://stackoverflow.com/a/868508/6693299

Community
  • 1
  • 1
ScottK
  • 1,526
  • 1
  • 16
  • 23
0

The values of x and z are implementation dependent integer values, not necessarily ASCII values. Yet it is required by the Standard that the values of '0' to '9' be in sequence, so one solution is to change to:

int a = x - '0';
int b = z - '0';

Another possibility is to use the atoi() function (or better, the strtol() function, which adds error checking capabilities), and compound literals to form strings from the input characters:

int a = atoi((char []){x, '\0'});
int b = atoi((char []){z, '\0'});
ad absurdum
  • 19,498
  • 5
  • 37
  • 60