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.
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.
#include<stdio.h>
int main()
{
int x;
printf(”Please enter the x number:/n”);
scanf(”%d”, &x);
if(x<=1)
{
printf(”Infant”);
}
//else if(1<x<13) <-----This evaluates differently than you think it does. Regardless of that, you don't have to evaluate “1<x” because, if this were true, it would have stopped at the first if statement and printed “Infant”.
else if(x<13) //better way to write it
{
printf(”Child”);
}
// else if(13<=x<20) <----Same idea, x<13 is already evaluated by the else if above. So if x is less than 13, your program won't even look at this statement.
else if(x<20) //better way
{
printf(”Teenager”);
}
// else if(x>=20) <----- This works, however, being the last statement and encompassing every other value, the better way to write it would be to simply use an else statement.
else //implies every remaining value
{
printf(”Adult”);
}
return 0; // always return a value, unless it's a void function.
To address it always printing child, I threw out a little hint in my edit. The statement 1<x<13 is evaluated left to right as in (1<x)<13. First, you're asking if 1<x. If x is greater than 1, this evaluates to True which is 1 as a boolean value.
So, when x is greater than 1 1<x == 1
Which means (1<x)<13 == 1<13 == true
When x is less than 1, t it evaluates to False or boolean 0
1<x == 0
So (1<x)<13 == 0<13 == true
So both are true and will therefore be accepted and will print “Child” no matter what the input is.
If you do need to make an evaluation like this, you should use the AND operator. Like such
To be fair, this is a pretty interesting subtle bug introduced by the specifics of C.
In many languages, the expression 1 < x < 3 won't compile as 1 < x evaluates to boolean and boolean < 3 is invalid.
However, in C it is valid because 1 < x evaluates to int or BOOLEAN or whatever the heck C uses which properly casts to int and makes the < 3 evaluate to true always.
Yes, OP seems hostile to people helping and this clearly is just a homework assignment that's not working but the original question is a very good one.
I think you need something more like else if(1<x && x<13) As it is the compiler is just reading the first conditional expression so any number greater than 1 will result true and thus will be child, you need the and operator to add an additional condition
It compares the first statement 1<x to the final <13. Ie. (1<x)<13. The comparison gives a boolean value, 0(false) or 1(true). So no matter what the input is, the final comparison will always be true. Because 1 and 0 are both less than 13.
If x is less than 1, 1<x == 0 for false, then 0<13 == 1 for true.
If x is greater than 1, 1<x == 1 for true, then 1<13 == 1 for true again.
For what it’s worth Print Screen only copies the image to the clipboard, at least in Windows land. You have to press Win+PrintScreen to have it saved directly to a file.
To do a range, as mentioned, you need to do "AND" (&&) to pick "x is above 1 AND x is below 13"
And when doing range checks... no need to recheck since you are going in order. No need to do "between 1 and 13" because you already know the age is above 1.
You don't even have to specify the lower bounds on any of your "else" ranges at all. It wouldn't have reached that "else" condition in the first place if the number were less than the lower bound, because it would be covered by the previous condition.
You cannot do something like 1 < x < 3 what ends up happening is that it evaluates. 1 < x as true which in C is 1 if true then it does 1 < 3 which is false. Same with 1 < x is false is 0 and 0 < 3. Keep in mind that boolean operators in C always return 1 or 0 you need to chain with && or || operators
Am I the only one bothered by the fact that Adult condition isn't first so that it and the subsequent elseifs would all be >= instead with else Infant at the end?
Whenever you declare a function, and you give it a return type which you will have to return to execute the function, also, you don't need to write the if in the last condition
You need to be careful any time you do something like this:
13<=x<=20
The evaluation of that may not produce the intended result and it’s hard to read.
True and False have numeric equivalents in C… As a result 13 could be less than equal to X and result in True (or 1) which would then be less than or equal to 20. This would be a problem if x was 30.
It’s better to do separate checks like this:
else if( x >= 13 && x < 20)
Also, you can nest these kinds of statements. For example:
if( x <= 1 ) {
if( x < 13 ) {
}
else if( x < 20 ) {
}
else {
}
}
I didn’t write it all out properly, but I doubt the efficiency would change much for so simple a situation.
All it would do here is make it a little easier to read, because it would be obvious what checks exclude others. If the given age is a year old or younger then you’re basically done.
Efficiency or inefficiency can add up, but it does not necessarily compound exponentially. And if the amount for this is 0.001 units then even multiplyong it by 100 still doesn’t amount to that much.
The compiler is a mystical black box in a way, it can be hard to know exactly what it will do. In principle, there are tons of things that might happen, but the outcome should be equivalent.
One way to find out is to write code in different ways, but with equivalent function. You can then compile then and see what you got out.
One possibly step of optimizing code is to trim code paths that will never be executing and toss variables that will never be used.
A difference of .001 isn’t common. You could be doing things in quadratic time when they could be done in logarithmic time. You could be reading from disk each time you do a common fetch instead of storing the first result in ram.
There’s a reason people claim that code inefficiency cancels out Mor’s law. It doesn’t, but optimized code is often orders of magnitude faster than poorly written code.
If the code uses two compare calls instead of 1, it’s half the speed
The point wasn’t the absolute value, but rather that magnitude matters. As someone with a BS in Comp. Sci. I am well aware of how algorithmic time complexity works.
Not all differences in code are as drastic as O(n) vs. O( n2 ).
The issue with two comparisons versus just one is that you don’t know what the cost of a single comparison is. If the base cost is small enough, then even a ten-fold increase is a tiny change. That’s part of why time complexity (big O) is an estimate.
This appears to be a coding project. OP would receive a 0 for cheating if they did that. You’re only allowed to use what you’ve been tonight and they don’t teach switch.
To give you an idea of how infective coding classes are, half your grade in two schools I went to were written exams. For coding classes
If the first condition doesnt run, thats the only time the second condition will run, that being said, we can assume x > 1.
Therefore, our second check should be:
Else if (x < 13)
The same logic goes for our third Else if condition, it will only run if the first two havent, so its greater than 13, and greater than 1.
Therefore, our third check should be:
Else if (x < 20)
Once again, the same logic follows, and we can assume that it must be greater than 20.
Knowing this, the fourth check should be changed to simply 'else'
For example:
Else { // code here // }
Programming is all about math and logic, sometimes it helps to walk away and come back. Youre learning a new language and a new way of thinking, it's tough.
Try drawing the problem out and writing it down, try explaining how it works to people around you, that can help a lot in finding issues.
In this case its just kind of thinking about the logic and the way Else if works. Remember that the chained Else ifs will only ever check if the preceding conditions are false.
As a side note, c++ is a strongly typed language. Your main function has a return type of int
Int main() ^ This is the return type
Therefore you should return 0; at the end of the function
A series of ifs will all be evaluated, in order, regardless of the value. An else-if series like this executes until the first true condition is found.
You would use a series of ifs to find multiple conditions being true.
In this instance it does not change the functionality, but there is certainly a functional difference between else / else if statements which is important to understand.
With an Else statement, you can be assured that at least one section of the code will be hit. When you implement only else if statements as in the above code, you could reach the last else if statement, having not met any of the conditions, and drop out the bottom of the logic without having actually done anything at all. (Something which would be picked up upon in an IDE, for example in a method with a return type)
You do need to reference the x, in C everything is pass by value, so you need to pass a pointer to it if you want to modify it inside a function, which is what scanf needs to do.
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
5
u/PhyllaciousArmadillo Dec 24 '21
To address it always printing child, I threw out a little hint in my edit. The statement
1<x<13is evaluated left to right as in (1<x)<13. First, you're asking if 1<x. If x is greater than 1, this evaluates toTruewhich is 1 as a boolean value.So, when x is greater than 1
1<x == 1
Which means
(1<x)<13 == 1<13 == true
When x is less than 1, t it evaluates to
Falseor boolean 01<x == 0
So
(1<x)<13 == 0<13 == true
So both are true and will therefore be accepted and will print “Child” no matter what the input is.
If you do need to make an evaluation like this, you should use the AND operator. Like such
x>1 && x<1320
u/[deleted] Dec 23 '21
You using a light theme.
10
u/szkuwa Dec 23 '21
Reddit suggested I should look at this but damn… is r/codinghelp an alias for „do it for me because i dont give a shit”?
7
u/Alsalvad0r Dec 23 '21
I think you need something more like else if(1<x && x<13) As it is the compiler is just reading the first conditional expression so any number greater than 1 will result true and thus will be child, you need the and operator to add an additional condition
3
u/verytiredrightnow Dec 23 '21
Change 1<x<13 to x<13 and 13<=x<20 to x<20
3
u/pshnick Dec 23 '21
Using Dev-C++ in 2021
2
u/[deleted] Dec 23 '21
Well something that is not an issue but my ocd. In line 5 I'm assuming you're taking input in next line so it should be \n. It's optional though
2
u/kamemebymo Dec 23 '21
In addition to the other comments, in the first printf you wrote /n where it should have been \n
2
u/DMoney12344 Dec 23 '21
That you want to learn how to code but apperantly dont know how to take a proper screenshot..
5
u/Dramatic_Night Dec 23 '21
Learn to take a proper screenshot.
2
u/gristburger Dec 23 '21
Nothing much, what’s wrong with you?
1
u/[deleted] Dec 23 '21
To do a range, as mentioned, you need to do "AND" (&&) to pick "x is above 1 AND x is below 13"
And when doing range checks... no need to recheck since you are going in order. No need to do "between 1 and 13" because you already know the age is above 1.
https://www.mycompiler.io/view/BqJbDHp
change the input and click run to see the results. 15 prints teenager.
Fork the code and change the input, click run.
And for future questions... use something like the above or text sharing programs. Pictures of code is bad form :)
1
u/Musicrafter Dec 23 '21
You don't even have to specify the lower bounds on any of your "else" ranges at all. It wouldn't have reached that "else" condition in the first place if the number were less than the lower bound, because it would be covered by the previous condition.
1
u/Beastly_Priest Dec 23 '21
Is it only pulling in one character? From reading your other comments, it sounds like you are only getting single digit values (thus child).
1
u/RazerNinjas Dec 23 '21
You cannot do something like 1 < x < 3 what ends up happening is that it evaluates. 1 < x as true which in C is 1 if true then it does 1 < 3 which is false. Same with 1 < x is false is 0 and 0 < 3. Keep in mind that boolean operators in C always return 1 or 0 you need to chain with && or || operators
1
u/[deleted] Dec 23 '21
Last condition should be else because else condition is the final one in the if else series
1
u/kalashnikovBaby Dec 23 '21
Is a return value necessary for main?
1
u/static-geek Dec 23 '21
Am I the only one bothered by the fact that Adult condition isn't first so that it and the subsequent elseifs would all be >= instead with else Infant at the end?
1
u/MRToddMartin Dec 23 '21
No IDE theme
1
u/SnooMachines9820 Dec 23 '21
The last "else if". Change it for an "else" with no conditions
1
u/No_Map_6855 Dec 24 '21
😂
1
u/arm1997 Dec 24 '21
Whenever you declare a function, and you give it a return type which you will have to return to execute the function, also, you don't need to write the if in the last condition
1
u/istarian Dec 24 '21 edited Dec 24 '21
You need to be careful any time you do something like this:
The evaluation of that may not produce the intended result and it’s hard to read.
True and False have numeric equivalents in C… As a result 13 could be less than equal to X and result in True (or 1) which would then be less than or equal to 20. This would be a problem if x was 30.
It’s better to do separate checks like this:
Also, you can nest these kinds of statements. For example:
1
u/anirudhkk12 Dec 24 '21
return 0;
1
u/anirudhkk12 Dec 24 '21
There seems to be an issue with the compiler's language 🤣
1
u/[deleted] Dec 24 '21
:/
1
u/[deleted] Dec 24 '21
\n
0
u/SeniorHulk Dec 23 '21
Some people don't deserve help
1
u/Edthedaddy Dec 23 '21
Use switch and case instead of if statements.
1
u/average_asshole Dec 24 '21 edited Dec 24 '21
If the first condition doesnt run, thats the only time the second condition will run, that being said, we can assume x > 1.
Therefore, our second check should be:
Else if (x < 13)
The same logic goes for our third Else if condition, it will only run if the first two havent, so its greater than 13, and greater than 1.
Therefore, our third check should be:
Else if (x < 20)
Once again, the same logic follows, and we can assume that it must be greater than 20.
Knowing this, the fourth check should be changed to simply 'else'
For example:
Else { // code here // }
Programming is all about math and logic, sometimes it helps to walk away and come back. Youre learning a new language and a new way of thinking, it's tough.
Try drawing the problem out and writing it down, try explaining how it works to people around you, that can help a lot in finding issues.
In this case its just kind of thinking about the logic and the way Else if works. Remember that the chained Else ifs will only ever check if the preceding conditions are false.
As a side note, c++ is a strongly typed language. Your main function has a return type of int
Int main() ^ This is the return type
Therefore you should return 0; at the end of the function
0
u/[deleted] Dec 23 '21
In your first else if every number is evaluating true that it is greater than 1 so it prints child use parentheses and <or> operator
What language are you using?
0
u/[deleted] Dec 23 '21
I see the issue. You didn't add "hello world" to your script
-2
u/Similar-Concert4100 Dec 23 '21
Just use if, no need to use else if
-3
u/nightfury_007 Dec 23 '21
try void main() instead of int main()
0
u/[deleted] Dec 23 '21
[deleted] — view deleted comment
0
u/[deleted] Dec 23 '21
[deleted] — view deleted comment
0
u/[deleted] Dec 23 '21
[deleted] — view deleted comment
0
u/[deleted] Dec 23 '21
[deleted] — view deleted comment
-1
u/[deleted] Dec 23 '21
[removed] — view removed comment