[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Extra juicy! | Home]

Hey /g/uys, I'm working on a little C# console app that

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 18
Thread images: 2

File: rarepepe2dontsave.gif (607KB, 800x792px) Image search: [iqdb] [SauceNao] [Google]
rarepepe2dontsave.gif
607KB, 800x792px
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?
>>
File: updatecode.png (22KB, 929x646px) Image search: [iqdb] [SauceNao] [Google]
updatecode.png
22KB, 929x646px
>>50813288
Here is the updated code
>>
>>50813448
Because I'm bored
void 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
Thread posts: 18
Thread images: 2


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]
Please support this website by donating Bitcoins to 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
If a post contains copyrighted or illegal content, please click on that post's [Report] button and fill out a post removal request
All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site. This means that 4Archive shows an archive of their content. If you need information for a Poster - contact them.