Hopefully this can help me to explain:
String age = "";
Console.WriteLine("Enter age:");
age = Console.ReadLine();
Now i want to take that age, and convert it to an integer so as i can manipulate it to work out the users age in Months, weeks, days, hours, minutes and seconds.
As i recall, you can do this with a Console.
New to this site also, hoping it will help me through college, as the course consists of using C#. :)
-Stuart
Carl CioffiPosted Jun 20, 2013, 11:06 AM
int iAge = 61;
int yearofbirth = DateTime.Now.Year - iAge ;
DateTime x = new DateTime(yearofbirth);
TimeSpan xx = DateTime.Now.Subtract(x);
The TimeSpan object with give you almost all of that information with doing all of those calculations and will be more accurate taking into considerations the correct number of days in each year.
Stuart CowenPosted Jun 20, 2013, 8:21 AM
that would explain the large numbers i was getting! thanks
VulpesPosted Jun 20, 2013, 8:16 AM
Stuart CowenPosted Jun 19, 2013, 4:10 PM
VulpesPosted Jun 17, 2013, 7:24 PM
Stuart CowenPosted Jun 17, 2013, 2:12 AM
Convert.ToInt32(age);
I was confusing the Convert, with Console. Thanks though, much help for a beginner such as myself!
However, i can see
int.Parse(age);
being much more easy to remember.
Just to say - this is what I've come up with
String name = "";
String age = "";
int end = 0;
while (end == 0)
{
Console.WriteLine("Enter name:");
name = Console.ReadLine();
Console.WriteLine("Enter Age:");
age = Console.ReadLine();
int iage;
bool isValid = int.TryParse(age, out iage);
if (iage != 0)
{
Console.WriteLine("Hello " + name + ".");
Console.WriteLine("You are " + iage + " years old");
Console.WriteLine("You are " + (iage * 12) + " months old");
Console.WriteLine("You are " + (iage * 12 * 52) + " weeks old");
Console.WriteLine("You are " + (iage * 12 * 52 * 7) + " days old");
Console.WriteLine("You are " + (iage * 12 * 52 * 7 * 24) + " hours old");
Console.WriteLine("You are " + (iage * 12 * 52 * 7 * 24 * 60) + " minutes old");
Console.WriteLine("You are " + (iage * 12 * 52 * 7 * 24 * 60 * 60) + " seconds old");
end = 1;
}
else
{
Console.WriteLine("Please enter an integer e.g.) 9");
}
}
Console.WriteLine("Press enter to exit.");
Console.ReadLine();
VulpesPosted Jun 16, 2013, 7:05 PM
Also if you want to allow a fractional age to be input, then just replace int or Int32 with double or Double:
or:
double dAge;