Advertisement
kyrathasoft

age

Jul 21st, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.63 KB | None | 0 0
  1. using System;
  2.  /*
  3. from Lesson 4 of C# console programming tutorial series at following URL:
  4. http://williammillerservices.com/windows-c-console-programming/
  5. demonstrates input data validation and parsing to int
  6. GitHub gist -> https://gist.github.com/kyrathasoft/a6ead63e6832361086dc69a6487de81d
  7. Pastebin.com -> https://pastebin.com/ySY7crrg
  8. */
  9. namespace MyNamespace
  10. {
  11.     class MyClass
  12.     {
  13.         static void Main()
  14.         {
  15.             //get user's name
  16.             Console.Write("What is your name? ");
  17.             string sName = Console.ReadLine();
  18.            
  19.             //get user's location
  20.             Console.Write("And where are you from? ");
  21.             string sFrom = Console.ReadLine();
  22.  
  23.            
  24.             bool blnValid = false; //a true/false flag for use in the while loop
  25.             int iAge = 0; //initialize age to zero; user will change this value
  26.            
  27.             //loop until user enters a valid integer
  28.             while(!blnValid){
  29.                 try{
  30.                     Console.Write("What is your age? ");
  31.                     string sAge = Console.ReadLine();
  32.                     iAge = Int32.Parse(sAge);
  33.                     if(iAge < 4){
  34.                         Console.Write(iAge.ToString() + "? I don't think so. ");                       
  35.                         blnValid = false;
  36.                     }else{
  37.                         blnValid = true;
  38.                         if(iAge >= 80){
  39.                             Console.WriteLine("You're {0} and learning C# at that age? Respect!", iAge.ToString());
  40.                         }
  41.                     }
  42.                 }catch{
  43.                     Console.WriteLine("Uh, that's not your age...");                   
  44.                 }              
  45.             }
  46.  
  47.             //finally, report data back to user by printing to console
  48.             Console.Write("Summary: You are {0}. You are from {1}, and are ", sName, sFrom);
  49.             Console.WriteLine("{0} years old.", iAge.ToString());
  50.         }
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement