Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement