"Hello World" :D I am ashamed to admit, but I cannot figure this out, I haven't found any info on the net, so I decided to come here. My problem here is that, it doesn't recognize any number except 1 and 100. When I put something like 4 or 5 or 50 whatever, it doesn't put the line on the screen I have intended. I removed bits of code to spare you the reading time, left the part that doesn't work...
The problem is that you're using a string instead of a number. The letter '1' is not less than the number '2' (or any other numeral with the exception of zero). The numerical value of '1' is 0x31 and the numerical value of '2' is 0x32.
You probably meant to use a type int for age not a string.
string comparison works but think about how you would sort strings.
abc
ab
bcd
bcx
bc
would be sorted to
ab
abc
bc
bcd
bcx
and what that means here is that
"23" is < "45" sure
but think about 1, 10, and 100:
1 < 10 < 100, ok so far.
now think about
2, 20, and 100
that sorts to
100 <-- first one sorted lexographically
2
20
so as you can see, most inputs for real people's ages are going to be > 100 in a string compare. 2 is > 100, 30 is > 100, ...
using int on "age" doesn't work for some reason, I tried putting a lower number on age, like 50, and it all work as intended, anyone knows what's up with that?
When instead of 50 I put 100, it works only if the age will be 1. But, there is a problem also with 50, if I input anything above 100 and 100 included, it will give output "Still young". I am really at a loss here, I am using Microsoft compiler, if that matters.
Edit: When I use "int age" the code stops working. Here are the errors:
Error (active) E0042 operand types are incompatible ("int" and "const char *")
Error C2446 '<': no conversion from 'const char [3]' to 'int'
Most people would reasonably write an if statement like this:
if (age <= 100)
Nothing inherently wrong with that. It is when you are testing for equality (==) that it can result in very hard to detect errors. Someone meant to write:
if (age == 100) wrote if (age = 100)
That will always be true, and now the value in age has changed.
if (100 = age) will be a compile-time error, so the mistake is caught easily.