Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace MyFirstFunction
- {
- partial class Program
- {
- static void Main()
- {
- 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
- {
- // First thing is to ask the user to input two numbers
- Console.WriteLine("This program uses a function to add two integers");
- Console.WriteLine("Enter The First interger: ");
- string FirstInteger = Console.ReadLine(); //Gets the first integer
- // 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
- int CheckNumber1 = 0;
- if (!int.TryParse(FirstInteger, out CheckNumber1))
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("There was an error converting what you typed to a Integer (Int32) number. <Enter> To exit. ");
- Console.ReadKey();
- return;
- }
- // Now ask the user for a second number
- Console.WriteLine("Enter The Second interger: ");
- string SecondInteger = Console.ReadLine(); //Gets the second integer
- // Again I am checking to see if this is indeed an integer
- // -> 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)
- int CheckNumber2 = 0;
- if (!int.TryParse(SecondInteger, out CheckNumber2))
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("There was an error converting what you typed to a Integer (Int32) number. <Enter> To exit. ");
- Console.ReadKey();
- return;
- }
- // Calls the function to add up the two integers at this stage.
- string IntegerSum = SumOfIntegers(FirstInteger, SecondInteger);
- // Now it outputs the sum to the screen.
- Console.ForegroundColor = ConsoleColor.Yellow;
- Console.WriteLine("That sum of the two integers came to {0} ", IntegerSum);
- Console.ResetColor(); //Reset back to the original color
- Console.WriteLine("Hit any key to exit: ");
- Console.ReadLine();
- break;
- }
- }
- static string SumOfIntegers(string First, string Second)
- {
- string Total = First + Second;
- return Total;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement