Hey /g/uys, I'm working on a little C# console app that will calculate age in days after the user enters their birthday in MM/DD/YYYY format. I am trying to put a while loop with try/catch to, for example, stop people from spamming an invalid entry.
The problem however is that I'm a total fucking newb with this and when I run the exe and try it myself, I will enter an incorrect value and it will prompt me to try again. When I try again and hit enter the console window just closes. This is driving me mad, can anyone help?
Fuck I forgot code:
namespace faggots
{
class Program
{
static void Main(string[] args)
{
try {
Console.WriteLine("This code is written by Your asshole!");
Console.WriteLine("Please type in your birthday in MM/DD/YYYY format, and press the Enter key.");
while (true)
{
string userValue;
userValue = Console.ReadLine();
DateTime myValue = DateTime.Now;
DateTime myBirthday = DateTime.Parse(userValue);
TimeSpan myAge = DateTime.Now.Subtract(myBirthday);
Console.WriteLine("Your age in days is:");
Console.WriteLine(myAge.TotalDays);
}
}
catch(Exception e)
{
Console.WriteLine("Please enter valid date in MM/DD/YYYY format. {0}", e.Message);
}
finally
{
Console.WriteLine();
}
Console.ReadLine();
}
}
}
shiggy diggy I can't even post the code in here the right way
#!/usr/bin/env bash
DATEfirstnum = `date -d "2014/5/14" +"%j"`
DATElastnum = `date -d "12/31/14" +"%j"`
DAYSdif=$(($DATElastnum - $DATEfirstnum))
echo $DAYSdir
>>50812995
I'm not really sure what this means, I mean I understand what it is doing
>>50812871
If the user enters an invalid entry the method that throws an exception is DateTime.Parse. Put that method in a try/catch block.
https://msdn.microsoft.com/en-us/library/1k1skd40%28v=vs.110%29.aspx#Anchor_1
>>50813288
Thank you, I could not figure out what exception to use so I just used the general one. How do I get it to run the while loop again once the user enters a new value?
>>50813288
Here is the updated code
>>50813448
Because I'm boredvoid Main()
{
Console.WriteLine("Licensed under Wizard License v1.0");
DateTime bday = DateTime.MinValue;
bool invalidInput = true;
do
{
Console.WriteLine("Type your b/day");
string userValue = Console.ReadLine();
invalidInput = !DateTime.TryParse(userValue, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AssumeUniversal,out bday);
} while(invalidInput);
Console.WriteLine("Your age in days is {0}", DateTime.Today.Subtract(bday).Days);
}
>>50813699
If you have the time, would you mind explaining to me how this works?
>>50812871
if dumb_frogposter
then kill_yourself;
>>50813944
What if it was only to get your attention?
>>50813960
you could have gotten our attention without frogposting, but frogposting will only give you negative attention.
>>50813979
never again ;-;
>>50813865
What is there to explain? Read up on do-while loop. TryParse returns true if the parse was successful, and if so, stores the parsed value in out bday.
I used invariant culture so you could use your "DD/MM/YYYY" string and it would work like that everywhere.
At work so not reading the thread, but try adding a sleep at the end of the program. Windows closes console programs when execution is complete
>>50814054
Yeah never mind, I realize what you wrote now. Thank you very much for you help, I feel as if there is A LOT left for me to learn.
>>50813979
>frogposting will only give you negative attention
Just like animeposting