Follow us on Twitter!
Never in the field of human conflict was so much owed by so many to so few. - Winston Churchill
Wednesday, April 23, 2014
Navigation
Home
HellBoundHackers Main:
HellBoundHackers Find:
HellBoundHackers Information:
Learn
Communicate
Submit
Shop
Challenges
HellBoundHackers Exploit:
HellBoundHackers Programming:
HellBoundHackers Think:
HellBoundHackers Track:
HellBoundHackers Patch:
HellBoundHackers Other:
HellBoundHackers Need Help?
Other
Members Online
Total Online: 23
Guests Online: 22
Members Online: 1

Registered Members: 82885
Newest Member: ConiBE
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

Need help with this program please

TROY
Member



Posts: 83
Location: Sheffield, UK
Joined: 17.06.08
Rank:
Moderate
Posted on 19-12-08 22:03
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 (Pfft 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 < 13) {
cout<<"Your really young\n";
}
else if (age > 100) {
cout<<" Your very old, keep up the good work Grin\n";
}
else if (13 < age < 19) {
cout<<"Your a teenager\n";
}
else if (20 < age < 35) {
cout<<"You are middle aged\n";
}
else if (35 < age < 40) {
cout<<"You are starting to get older\n";
}
else if (40 < age < 60) {
cout<<" Your slightly more than middle aged\n";
}
else if (60 < age < 80) {
cout<<" Your quite old\n";
}
else if (80 < age < 100) {
cout<<" Yor old\n";
}

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. Grin


Author

RE: Need help with this program please


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 19-12-08 22:28
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 Wink.
Author

RE: Need help with this program please


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 19-12-08 22:44
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.
Author

RE: Need help with this program please

TROY
Member



Posts: 83
Location: Sheffield, UK
Joined: 17.06.08
Rank:
Moderate
Posted on 19-12-08 22:53
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 Wink.


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.





Author

RE: Need help with this program please


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 19-12-08 23:02
TROY wrote:
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 Wink.


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.



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.


Author

RE: Need help with this program please

TROY
Member



Posts: 83
Location: Sheffield, UK
Joined: 17.06.08
Rank:
Moderate
Posted on 19-12-08 23:04
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.


Author

RE: Need help with this program please

TROY
Member



Posts: 83
Location: Sheffield, UK
Joined: 17.06.08
Rank:
Moderate
Posted on 19-12-08 23:07
COM wrote:
TROY wrote:
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 Wink.


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.



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.


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


Author

RE: Need help with this program please

TROY
Member



Posts: 83
Location: Sheffield, UK
Joined: 17.06.08
Rank:
Moderate
Posted on 19-12-08 23:12

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


Author

RE: Need help with this program please

bl4ckc4t
Member

Your avatar

Posts: 591
Location: /etc/
Joined: 07.03.06
Rank:
Wiseman
Posted on 19-12-08 23:13
Should be if(age >= 13 && < 19) It will collide and depending on the compiler, the other way could prevent it from compiling.

-Bl4ckC4t


Author

RE: Need help with this program please

TROY
Member



Posts: 83
Location: Sheffield, UK
Joined: 17.06.08
Rank:
Moderate
Posted on 19-12-08 23:24
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 Wink


Author

RE: Need help with this program please


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 19-12-08 23:35
bl4ckc4t wrote:
Should be if(age >= 13 && < 19)

What's less than 19? Not a mathematical question.


Author

RE: Need help with this program please


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 20-12-08 01:04
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.


Author

RE: Need help with this program please

bl4ckc4t
Member

Your avatar

Posts: 591
Location: /etc/
Joined: 07.03.06
Rank:
Wiseman
Posted on 20-12-08 01:39
COM wrote:
bl4ckc4t wrote:
Should be if(age >= 13 && age < 19)

What's less than 19? Not a mathematical question.