+ 1

[SOLVED]Logical error (Char/Int)

Description on code https://code.sololearn.com/cEU7FasOPWvK/?ref=app

15th Dec 2019, 7:01 AM
Rashad Malikov
Rashad Malikov - avatar
14 ответов
+ 4
Why you took char instead of int or double. Fahrenheit value is interger value or Character. Go and review again about char, int, double, float.
15th Dec 2019, 7:07 AM
A͢J
A͢J - avatar
+ 3
`char` type is dedicated for (duh) characters, we don't normally treat them as numbers, although internally they belong to integral types family. Try to use `int` or `short` instead, it may work : ) (Edit) Follow CarrieForle advice if you're concerned about memory usage. You are currently using %d specifier while reading input for a char variable. Use of inappropriate specifier for reading input may cause undesirable result.
15th Dec 2019, 7:08 AM
Ipang
+ 2
Rashad Malikov So you can take any Integer or double value as a Char because it takes less memory where did you read about this. I never heard anywhere. If I take as 1000. Do you know what is size of 1000 and a character value like a. I think you need to learn again.
15th Dec 2019, 7:14 AM
A͢J
A͢J - avatar
+ 2
~ swim ~ nice) all thanks for reply
15th Dec 2019, 8:17 AM
Rashad Malikov
Rashad Malikov - avatar
+ 1
If you do want to use "1 byte" integer. There is inttypes.h which includes 1 byte integer int8_t. Here is how you printf and scanf those int types: https://en.cppreference.com/w/c/types/integer
15th Dec 2019, 7:24 AM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 1
Your code is Ok the error is in (system("pause")) this command is not declared in the stdin.h header you have to include iostream and run it in a live console compiler to work properly
16th Dec 2019, 10:15 AM
$p@rK
$p@rK - avatar
+ 1
The else block will work if enter a value more than 100 or a value less than -100 Try this correction of your code //Fahrenheit to Celsius #include <stdio.h> int main() { int f=0,c=0; //error here printf ("Enter f:"); scanf ("%d",&f); if((f>=-100&&f<=0)||(f<=100&&f>=0)) { c = (f - 32) / 1.8; printf("f=%d\nc=%d", f, c); } else { printf ("Error"); } return 0; }
16th Dec 2019, 6:03 PM
$p@rK
$p@rK - avatar
15th Dec 2019, 7:08 AM
Rashad Malikov
Rashad Malikov - avatar
0
converting char to integer works many times. but it stuck on this code and i can not find why
15th Dec 2019, 7:11 AM
Rashad Malikov
Rashad Malikov - avatar
15th Dec 2019, 7:21 AM
Rashad Malikov
Rashad Malikov - avatar
0
CarrieForle thanks for info
15th Dec 2019, 7:27 AM
Rashad Malikov
Rashad Malikov - avatar
0
I think i found the answer. char uses -128 to 127 and if user input over size ,program changes it to between these numbers. and program never goes to else block 🅰🅹 - ɪ'ᴍ ᴄʀɪᴍɪɴᴀʟʟʏ ɢᴏᴏᴅ! CarrieForle is it true?
15th Dec 2019, 7:38 AM
Rashad Malikov
Rashad Malikov - avatar
0
$p@rK i just wanted to know why else block doesnt work. and found it
16th Dec 2019, 5:42 PM
Rashad Malikov
Rashad Malikov - avatar