Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

Need help with this program please


TROY's Avatar
The Krazy Bastard
0 0

I've been making this program tonight, just practising what i've learnt and i have a problem with it, its like an age teller thing (:P lol pritty lame but i got it from some video on youtube and then coded it myself), This is the code:

#include <iostream> using namespace std;

int main() { int age; cout<<"Hello, and welcome to the Age teller, designed and programmed by Matt Sutton!\n"; cout<<"Please enter your age to continue: "; cin>> age; cin.ignore();

if (age &lt; 13) {
        cout&lt;&lt;&quot;Your really young&#92;n&quot;;
        }
else if (age &gt; 100) {
        cout&lt;&lt;&quot; Your very old, keep up the good work :D&#92;n&quot;;
        }        
        else if (13 &lt; age &lt; 19) {
             cout&lt;&lt;&quot;Your a teenager&#92;n&quot;;
             }
        else if (20 &lt; age &lt; 35) {
             cout&lt;&lt;&quot;You are middle aged&#92;n&quot;;
             }      
        else if (35 &lt; age &lt; 40) {
        cout&lt;&lt;&quot;You are starting to get older&#92;n&quot;;
        }
        else if (40 &lt; age &lt; 60) {
        cout&lt;&lt;&quot; Your slightly more than middle aged&#92;n&quot;;
        }
        else if (60 &lt; age &lt; 80) {
        cout&lt;&lt;&quot; Your quite old&#92;n&quot;;
        }
        else if (80 &lt; age &lt; 100) {
        cout&lt;&lt;&quot; Yor old&#92;n&quot;;
        }
                    
        cin.get();

}
It works fine when you say that your under 13 and over 100, but any other age you put like 35 or 84 etc. it just says what it should say for Between the age of 13 and 19 (Your a teenager), and im not sure how to solve this, i supect that its just because its layed out wrong or im missing some code. Any help would be massiveley appreciated, Thanks. :D


ghost's Avatar
0 0

Take a look at the difference between the IF condition for under 13 and over 100 and compare it to the rest of the ages and see if you can see why it doesn't work ;).


ghost's Avatar
0 0

I would suggest that you first start with C because you'll get a much better "feeling" for the code. Don't get me wrong, C++ can do anything that C can, but in a way… C is a bit simpler if you're starting with programing. Remove the "else" in the list and just put it on the end (not the one you have now, but add a >100) I would like if you looked into the switch char commands more, you'll get it faster and better.


TROY's Avatar
The Krazy Bastard
0 0

slpctrl wrote: Take a look at the difference between the IF condition for under 13 and over 100 and compare it to the rest of the ages and see if you can see why it doesn't work ;).

Yeah, i know what you mean, but thats why i put the syntax's for the other ages as else if (13 < age < 19) instead of else if ( age < 19). I did this because surely if i put

if (age < 13) { cout<<"Your really young\n"; }

and then put

else if (age < 19) { cout<<"Your a teenager\n"; } SURELY they would collide and just mess up?? because the first one is saying: for everyone under 13 print "Your really young! and the second one would be saying: for everyone under the age of 19 print "Your a teenager", because if it did do this then wouldnt it just cancel each other out, and then surely the last one i enterred would then cancel out the previous ones and then wouldnt it just say your old for all of them??, lol. Im really sorry because i bet this sounds really confusing and like giberish sorry about that.


ghost's Avatar
0 0

TROY wrote: [quote]slpctrl wrote: Take a look at the difference between the IF condition for under 13 and over 100 and compare it to the rest of the ages and see if you can see why it doesn't work ;).

Yeah, i know what you mean, but thats why i put the syntax's for the other ages as else if (13 < age < 19) instead of else if ( age < 19). I did this because surely if i put

if (age < 13) { cout<<"Your really young\n"; }

and then put

else if (age < 19) { cout<<"Your a teenager\n"; } SURELY they would collide and just mess up?? because the first one is saying: for everyone under 13 print "Your really young! and the second one would be saying: for everyone under the age of 19 print "Your a teenager", because if it did do this then wouldnt it just cancel each other out, and then surely the last one i enterred would then cancel out the previous ones and then wouldnt it just say your old for all of them??, lol. Im really sorry because i bet this sounds really confusing and like giberish sorry about that.

[/quote] Else is there for a reason, if statement is true do something ELSE you do something else, etc. If a statement is true then there's no need to do something else.

What he meant though was learn about operators.


TROY's Avatar
The Krazy Bastard
0 0

Stoney wrote: I would suggest that you first start with C because you'll get a much better "feeling" for the code. Don't get me wrong, C++ can do anything that C can, but in a way… C is a bit simpler if you're starting with programing. Remove the "else" in the list and just put it on the end (not the one you have now, but add a >100) I would like if you looked into the switch char commands more, you'll get it faster and better.

I read into that but most guides i read advised against it, some actually even said (Including Learn C++ in 21 days) that it might be an advantage n NOT to learn it.


TROY's Avatar
The Krazy Bastard
0 0

COM wrote: [quote]TROY wrote: [quote]slpctrl wrote: Take a look at the difference between the IF condition for under 13 and over 100 and compare it to the rest of the ages and see if you can see why it doesn't work ;).

Yeah, i know what you mean, but thats why i put the syntax's for the other ages as else if (13 < age < 19) instead of else if ( age < 19). I did this because surely if i put

if (age < 13) { cout<<"Your really young\n"; }

and then put

else if (age < 19) { cout<<"Your a teenager\n"; } SURELY they would collide and just mess up?? because the first one is saying: for everyone under 13 print "Your really young! and the second one would be saying: for everyone under the age of 19 print "Your a teenager", because if it did do this then wouldnt it just cancel each other out, and then surely the last one i enterred would then cancel out the previous ones and then wouldnt it just say your old for all of them??, lol. Im really sorry because i bet this sounds really confusing and like giberish sorry about that.

[/quote] Else is there for a reason, if statement is true do something ELSE you do something else, etc. If a statement is true then there's no need to do something else.

What he meant though was learn about operators.[/quote]

Ohhhh, so i dont really need the else if's???, can i just put if for all of them?


TROY's Avatar
The Krazy Bastard
0 0

Ohhhh, so i dont really need the else if's???, can i just put if for all of them?

Actually, forget that, i tried it and i got this:

Hello, and welcome to the age teller, designed and programmed by Matt Sutton! Please enter your age to continue: 34 Your a teenager You are middle aged You are starting to get older Your slightly more than middle aged Your quite old Your old

So basically it displayed all ot the answers to the IF's lol


bl4ckc4t's Avatar
Banned
0 0

Should be if(age >= 13 && < 19) It will collide and depending on the compiler, the other way could prevent it from compiling.

-Bl4ckC4t


TROY's Avatar
The Krazy Bastard
0 0

bl4ckc4t wrote: Should be if(age >= 13 && < 19) It will collide and depending on the compiler, the other way could prevent it from compiling.

-Bl4ckC4t

I tried that too, but it gave me an error for all the lines which i changed that to. I dont suppose someone else could compile and run it if they have time, and then see what results they get. Thanks very much ;)


ghost's Avatar
0 0

bl4ckc4t wrote: Should be if(age >= 13 && < 19) What's less than 19? Not a mathematical question.


ghost's Avatar
0 0

The reason why they recommend that you don't use multiple if…else statements in a single set is because it's worse practice (and slower) than a switch() statement. However, if you can't use switch for whatever reason, use the if…else's; it's a whole lot less stupid than using multiple solitary if's.


bl4ckc4t's Avatar
Banned
0 0

COM wrote: [quote]bl4ckc4t wrote: Should be if(age >= 13 && age < 19) What's less than 19? Not a mathematical question.[/quote]