Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C#

age test c#

i am trying to make a age test programme when i run i only get the out put of you are "age" and a adult any ideas.

code:

https://w.trhou.se/f31b2byvwj

4 Answers

Robert Lyon
Robert Lyon
7,551 Points

Yeah sorry i see the problem now, i should have tested it before i put in my answer. You are hard coding the variable age to the value of 18 at the top of the program. Then within your conditional statements you are comparing the hard coded variable age against the other values.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace agetest.console
{
    class Program
    {
        static void Main(string[] args)
        {


            Console.WriteLine("please enter age\n");
            int entry = Int32.Parse(Console.ReadLine());


            //notice below the comparison to entry instead of  age.
            if (entry >= 18)
            {
                Console.WriteLine("You are " + entry + " and a adult");
            }
            else if(entry >= 13)
            {
                Console.WriteLine("You are " + entry + " and a Teenager");
            }
            else if (entry > 0)
            {
                Console.WriteLine("You are " + entry + " and a child");
            }
            else
            {
                Console.WriteLine("something's wrong");
            }
        }
    }
}

hope this fixes the problem

Robert Lyon
Robert Lyon
7,551 Points

int age = 18;

        Console.WriteLine("please enter age\n");

        /*As you can see below i have changed the type of the variable entry from string to int
        and that i have added an explicit conversion to the ReadLine() method.
        This is because the value that is obtained from ReadLine() is returned as a string.
        This means that we have to convert the string value obtained by ReadLine() to an integer value and store the result in a variable of type int.
        */
        int entry = Int32.Parse(Console.ReadLine());
        if (age >= 18)
        {
            Console.WriteLine("You are " + entry + " and a adult");
        }
        else if(age >= 13)
        {
            Console.WriteLine("You are " + entry + " and a Teenager");
        }
        else if (age > 0)
        {
            Console.WriteLine("You are " + entry + " and a child");
        }
        else
        {
            Console.WriteLine("something's wrong");
        }

I hope that this helps and makes sense to you.

initially thought i'd have to do that but am sill having the same problem that with every age i input it says adult ive have juggled around with different ways of using the if else but then it just gives me all of string with entry adult, teen and child.

ahha thank you as it was age test thought i was appropriate to rename all entry's to age and it now all works perfect great job initially i hard coded the age at top as thought i may need it to compare to but didn't and although short program well it now smaller thank you for taking time to answer my question.