Advertisement
Guest User

My first attempt at coding

a guest
Mar 3rd, 2014
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace MyFirstFunction
  8. {
  9.  
  10. partial class Program
  11. {
  12.  
  13. static void Main()
  14. {
  15. while (true) //This is the main part of the program, it will keep on looping until the user decides to exit by entering escape or q
  16. {
  17. // First thing is to ask the user to input two numbers
  18. Console.WriteLine("This program uses a function to add two integers");
  19. Console.WriteLine("Enter The First interger: ");
  20. string FirstInteger = Console.ReadLine(); //Gets the first integer
  21.  
  22. // now perform a check to see if this indeed is an integer (here i will just copy/adapt the code used by Lou Maresca during episode 2 I think
  23. int CheckNumber1 = 0;
  24. if (!int.TryParse(FirstInteger, out CheckNumber1))
  25. {
  26. Console.ForegroundColor = ConsoleColor.Red;
  27. Console.WriteLine("There was an error converting what you typed to a Integer (Int32) number. <Enter> To exit. ");
  28. Console.ReadKey();
  29. return;
  30. }
  31.  
  32. // Now ask the user for a second number
  33. Console.WriteLine("Enter The Second interger: ");
  34. string SecondInteger = Console.ReadLine(); //Gets the second integer
  35. // Again I am checking to see if this is indeed an integer
  36. // -> The very fact that I am reusing this code and a new variable means to me that I need to make this a function (something to work on next)
  37. int CheckNumber2 = 0;
  38. if (!int.TryParse(SecondInteger, out CheckNumber2))
  39. {
  40. Console.ForegroundColor = ConsoleColor.Red;
  41. Console.WriteLine("There was an error converting what you typed to a Integer (Int32) number. <Enter> To exit. ");
  42. Console.ReadKey();
  43. return;
  44. }
  45.  
  46. // Calls the function to add up the two integers at this stage.
  47. string IntegerSum = SumOfIntegers(FirstInteger, SecondInteger);
  48.  
  49. // Now it outputs the sum to the screen.
  50. Console.ForegroundColor = ConsoleColor.Yellow;
  51. Console.WriteLine("That sum of the two integers came to {0} ", IntegerSum);
  52. Console.ResetColor(); //Reset back to the original color
  53. Console.WriteLine("Hit any key to exit: ");
  54. Console.ReadLine();
  55. break;
  56. }
  57. }
  58.  
  59.  
  60. static string SumOfIntegers(string First, string Second)
  61. {
  62. string Total = First + Second;
  63. return Total;
  64. }
  65. }
  66.  
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement