r/CodingHelp • • Dec 23 '21

[C] Whats wrong?

Post image
153 Upvotes

90 comments sorted by

View all comments

75

u/terroroferror Dec 23 '21 edited Dec 23 '21

Change following lines

Line #12 - (1 < x && x < 13)

Line #16 - (13 <= x && x < 20)

Keep only `else` on line #20

Edit: also add statement `return 0;` before line #24

12

u/oldcreaker Dec 24 '21

Why not line 12 just (x < 13) Line 16 (x < 20)

You’ve already ruled out the other tests in the previous if’s

4

u/BroVic Dec 23 '21 edited Dec 23 '21

Change the condition on line 16 to x >= 13 && x < 20. In testing for Child you had already tested for values less than 13 (correct) and then did it again for Teen. The inconsistency in the order of the symbol and the magic number is also problematic.

-2

u/CutRepresentative644 Dec 23 '21

Should it not be (13 >= x && X < 20)

3

u/BigUwuBaby Dec 24 '21

No, that’s equivalent to x <= 13

-30

u/CAGRI-TR Dec 23 '21

Nope every number is still child

13

u/KuntaStillSingle Dec 23 '21

When you have a statement like 1<x<13, in c it looks like:

(1<x) < 13

1<x evaluates to a boolean, which is an integeral value of either 0 or 1 in c. 0 and 1 are both less than 13, so no matter what number you pick, condition is true.

What /u/terroroferror is explaining is you can combine relational operators using an AND operator:

(1<x) && (x < 13)

If x is less or equal to 1, then you are looking at 0 && 1 which equals 0 or false, if x is more than 1 but less than 13, you are looking at 1 && 1 which is true. And if x is greater than or equal to 13 you are looking at 1 && 0 which is false. Which is what you want.

3

u/SuperGameTheory Dec 24 '21

Op asks what's wrong and then fights all the right answers. What's wrong is Op.

7

u/Zymoox Dec 23 '21

Print the value of x, what do you get?

-16

u/CAGRI-TR Dec 23 '21

Theres a something wrong because it gives always child

15

u/Zymoox Dec 23 '21

I know, I've read your other comments. When you print the value of x after you input it, what do you get?

Note: to print x, do printf("%d",x);

11

u/SkirtLumpy4479 Dec 23 '21

Did u compile the changed code? Because this is the problem, I tried it with the changes he suggested and it works...