Introduction

Up until now, our programs have likely been “hard-coded” meaning the numbers and values were set it stone inside the code itself. To create a program with actual value it needs to be interactive.

we will bridge the gap between the computer and the human. You’ll learn how to Listen to what a user types and more importantly how to translate that text into math-ready number. by the end, we’ll combine these skills to build a functional calculator that determines a Triangle Area based on any dimensions a user provides.

  • We’ll learn how to get input from the user through the console window.
  • then convert it to the right data type.
Understanding Console.ReadLine() The Listener

Think of Console.ReadLine() as a waiter at a restaurant standing by your table with a notepad.

The wait ( The Program Pauses) When the computer reaches the line Console.ReadLine() it doesn’t move to the next line. It stops. It is “Listening” for the keyboard.

  • if your console window is open but nothing is happening the program is likely stuck at a ReadLine waiting for you to do something
  • The capture: Everything the user types is captured as String* even if they type a number

The String Trap This is the most important part of Console.ReadLine() always returns Text.

If a user types 25, the computer doesn’t see the number 25. It sees the text “25” you cannot do math with text!

// this will cause error 
int num = console.ReadLine();

The Translator When a user types on a keyboard they aren’t typing math. They are typing characters to the computer, there is no difference between the letter “A” and the character “5”. they are both just symbols.

The “Text” vs “Value” problem Think of it like:

  • The String (“5”) : This is like a Picture of a 5. You can look at it, but you can’t add it to anything
  • The Integer (5) : This is the actual value of 5. you can use this for math.
string input = "5" 
Console.WriteLine(input + input); 
// output: "55": it just glues the pictures together!

To use the input for math we must translate the text into a number we use Convert command.

int input = Convert.ToInt32(Console.ReadLine()); // explain 16,32,64 int versions.
Console.WriteLine( input + input);
If you want this Nickname…Use this Official Convert NameWhy?
boolToBooleanThe full name of the logic type.
charToCharShort for “Character.”
doubleToDoubleThe names actually match here!
floatToSingleThe Tricky One! (Single precision).
intToInt3232-bit integer.
shortToInt1616-bit integer.
longToInt6464-bit integer.

Examples

Console.WriteLine("Rectangle Area Calcualtor");

Console.WriteLine("Enter the width: ");
string widthInput = Console.ReadLine();
int width  = Convert.ToInt32(widthInput);


Console.WriteLine("Enter the height: ");
int height = Convert.ToInt32(Console.ReadLine());


int area = width * height;
Console.WriteLine("Total Area: " + area + " square units");
Console.WriteLine("Triangle Area Calculator"); 


float triangleBase = Convert.ToSingle(Console.ReadLine()); 

Console.Write("Enter the height: "); 
double triangleHeight = Convert.ToDouble(Console.ReadLine()); 

triangleArea = (triangleBase * triangleHeight) / 2.0; 
Console.WriteLine("The Triangle Area is: " + triangleArea);