+ 1
[SOLVED]Logical error (Char/Int)
Description on code https://code.sololearn.com/cEU7FasOPWvK/?ref=app
14 Réponses
+ 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.
+ 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.
+ 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.
+ 2
~ swim ~ nice) all thanks for reply
+ 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
+ 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
+ 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;
}
0
🅰🅹 - ɪ'ᴍ ᴄʀɪᴍɪɴᴀʟʟʏ ɢᴏᴏᴅ! because char uses less memory
0
converting char to integer works many times. but it stuck on this code and i can not find why
0
🅰🅹 - ɪ'ᴍ ᴄʀɪᴍɪɴᴀʟʟʏ ɢᴏᴏᴅ! conversion specifier
0
CarrieForle thanks for info
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?
0
$p@rK i just wanted to know why else block doesnt work. and found it