Question Data Type conversion

Reza1970

New member
Joined
Jun 29, 2019
Messages
2
Programming Experience
Beginner
C#:
using System;

namespace ConsoleUI
{
    class Program
    {
        static void Main()
        {
             Console.WriteLine("Please enter the year you were born");
            string yearBorn = Console.ReadLine();
            int age = 0;
            int millenium = 2000;

            bool isValid = int.TryParse(yearBorn, out age);

            if (isValid == true)
            {
                millenium -= age;

                Console.WriteLine($"You were {millenium} years old in the year 2000");

            }
            else
            {
                
                Console.WriteLine($" you were not born");
            }
              Console.ReadLine();


        }
    }
}

Hi all
I am trying to display to the user when they enter the year they were born how old they were in the year 2000, However, when the user types a year after 2000, I want it to show that they were not born yet
 
Do you have a specific question?

Right now, with the code you have above, it looks like you have a potential logic problem, rather than a data conversion problem.
 
Please don't do a code dump. Explain your problem or ask a question.

As an aside, your use of if (isValid == true) is not correct for determining the year someone was born against the millennium age variable. If I type 2002 I will be -2 years old which technically means I wasn't born, and this thus make your else condition useless as it will only execute if your number conversion from the tryparse fails. You can use relational operators < > <= >= against your age and millennium variable to ensure the correct conditions of an if statement are met. Check the spoiler in my signature for the operators link.
 
You're welcome.

You can't expect people to know the following unless you tell us
  1. What you're trying to do
  2. What is or is not working
  3. What your desired outcome is
  4. Where in your code a problem may reside
  5. If there is in fact a problem or if you are looking on ways to improve what you already wrote.
You need to explain what your code is about or why you're posting it in the first place.
 
Try to add conditional logic into your tryparse boolean statement to check the conditions of the values. Perhaps something like this ::
C#:
private static void Main(string[] args)
        {
            Console.WriteLine("Please enter the year you were born");
            string yearBorn = Console.ReadLine();
            int age = 0;
            const int millenium = 2000;

            bool isValid = int.TryParse(yearBorn, out age);

            if (isValid == true)
            {
                if (age > millenium)
                {
                    Console.WriteLine("You were not born yet");
                }
                else if (age.Equals(millenium))
                {
                    age = 0;
                    Console.WriteLine("You were {age} years old and only born in the year {millenium}");
                }
                else
                {
                    age -= millenium;
                    Console.WriteLine($"You were {age} years old in the year {millenium}");
                }
            }
            else
            {
                Console.WriteLine("You didn't type in a proper year. Format Example 1972");
            }
            Console.ReadLine();
        }
 
Back
Top Bottom