Jump to content

  • Log In with Google      Sign In   
  • Create Account


Can't figure out why same results appear


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 Phil_93   Members   -  Reputation: 146

Like
0Likes
Like

Posted 16 August 2012 - 06:59 AM

Hey I'm very new to programming and I've been doing some tutorials to get me used to programming. I've made a program that displays different text boxes depending on the age entered. The age criteria and messages are as follows:

Less than 16: "You're still a youngster."
Over 16 but under 25: "Fame beckons!"
Over 25 but under 40: "There's still time."
Over 40: "Oh dear, you've probably missed it!"

I have made a windows form with one button and a text box and this is the code I have writen:

private void button1_Click(object sender, EventArgs e)
{
double age;
age = double.Parse(textBox1.Text);
if (age < 17)
{
MessageBox.Show("Still a youngster.");
}
else if (age >= 17)
{
MessageBox.Show("Fame beckons!");
}
else if (age < 26)
{
MessageBox.Show("Fame beckons!");
}
else if (age >= 26)
{
MessageBox.Show("There's still time!");
}
else if (age < 41)
{
MessageBox.Show("There's still time!");
}
else if (age >= 41)
{
MessageBox.Show("Oh dear, you've probably missed it!");
}

However when the age entered gets to figures above 25 the message "Fame beckons!" still appears. I really cant see why the message isn't changing any help would be appreciated as this is really bugging me. Also if anyone could recommend any online tutorials to help me understand programming with C# it would be a big help. Thanks in advance.

Sponsor:

#2 brx   Members   -  Reputation: 672

Like
1Likes
Like

Posted 16 August 2012 - 07:03 AM

The problem is that the if statements are all executed in exactly the order you stated them. So when, for example, you enter 40, the condition if (age >= 17) will be executed and return true. Therefore all the other if statements are actually dead code (i.e. unreachable code). What you need to do is put both conditions age >= 17 and age < 26 into one statement using the '&&;' operator:
else if (age >= 17 && age <26) {
MessageBox.Show("Fame beckons!");
}
This way the "Fame beckons!" message box will only appear when the age is between 17 and 26. Same applies for the "There's still time!" case.


EDIT: Damn, editing messes up the '&' showing up correctly.

Edited by brx, 16 August 2012 - 07:06 AM.


#3 Phil_93   Members   -  Reputation: 146

Like
0Likes
Like

Posted 16 August 2012 - 07:08 AM

Ah now it makes sense, thank you very much :) Do you know any good online tutorials for me to follow? I feel stupid asking but I've just literally just scraped a pass in my first year of uni, unfortunately this is due to me missing half of the year being put in hospital so my programming is really shaky at the moment and I want to try and get some practice in before I go back for my second year.

#4 jrh2365   Members   -  Reputation: 579

Like
1Likes
Like

Posted 16 August 2012 - 11:07 AM

else if (age >= 17 && age <26) {


Technically you don't need the "age >= 17 &&" there, because that else if will only be reached if the age is >= 17.

You could just write it as:
[source lang="csharp"]if (age < 17){ MessageBox.Show("Still a youngster.");}else if (age < 26){ MessageBox.Show("Fame beckons!");}else if (age < 41){ MessageBox.Show("There's still time!");}else{ MessageBox.Show("Oh dear, you've probably missed it!");}[/source]




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS