Last active
July 21, 2018 14:37
-
-
Save kyrathasoft/a6ead63e6832361086dc69a6487de81d to your computer and use it in GitHub Desktop.
A program written in Lesson 4 of the following introductory C# console programming series:http://williammillerservices.com/windows-c-console-programming/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
/* | |
from Lesson 4 of C# console programming tutorial series at following URL: | |
http://williammillerservices.com/windows-c-console-programming/ | |
demonstrates input data validation and parsing to int | |
GitHub gist -> https://gist.github.com/kyrathasoft/a6ead63e6832361086dc69a6487de81d | |
Pastebin.com -> https://pastebin.com/ySY7crrg | |
*/ | |
namespace MyNamespace | |
{ | |
class MyClass | |
{ | |
static void Main() | |
{ | |
//get user's name | |
Console.Write("What is your name? "); | |
string sName = Console.ReadLine(); | |
//get user's location | |
Console.Write("And where are you from? "); | |
string sFrom = Console.ReadLine(); | |
bool blnValid = false; //a true/false flag for use in the while loop | |
int iAge = 0; //initialize age to zero; user will change this value | |
//loop until user enters a valid integer | |
while(!blnValid){ | |
try{ | |
Console.Write("What is your age? "); | |
string sAge = Console.ReadLine(); | |
iAge = Int32.Parse(sAge); | |
if(iAge < 4){ | |
Console.Write(iAge.ToString() + "? I don't think so. "); | |
blnValid = false; | |
}else{ | |
blnValid = true; | |
if(iAge >= 80){ | |
Console.WriteLine("You're {0} and learning C# at that age? Respect!", iAge.ToString()); | |
} | |
} | |
}catch{ | |
Console.WriteLine("Uh, that's not your age..."); | |
} | |
} | |
//finally, report data back to user by printing to console | |
Console.Write("Summary: You are {0}. You are from {1}, and are ", sName, sFrom); | |
Console.WriteLine("{0} years old.", iAge.ToString()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment