Step 2 - Read the user's name
Best first step is to build something small, then we can grow the program our from that start. Let’s just get the first two messages handled - reading the user’s name and displaying it. So we want to see this at the end:
What is your name: Andrew
Hi Andrew.
-
Open your terminal, and setup your project
Terminal window # Move you your code projects foldercd ~/Documents/Code # or cd /c/Users/andrew/Documents/Code# Create and move into a folder for the projectmkdir TravelCalculatorcd TravelCalculator# Create a blank dotnet projectdotnet new console# Add the SplashKit librarydotnet add package splashkit# Open in VS Codecode . -
Add the using directives to access the SplashKit and Convert code.
-
At this point you should just have these two lines of code.
using static System.Convert;using static SplashKitSDK.SplashKit;
As you look at this you want to see if there are any values that will change. We will need to create variables to store these.
Looking at the first lines, I imagine that we may have different users run this. So their name may change.
-
-
Add the code to create a
name
variable - then we can think about how we use this. -
Review the messages we want to output. To start we want to output the message
"What is your name: "
, read the text and user enters, then output a blank line and the greeting text"Hi "
followed by the user’s name.Looking back at the terminal methods, we have methods to output text (
Write
andWriteLine
), and read text from the user (ReadLine
). Use these to display the prompt, and output the required messages.- using static System.Convert;using static SplashKitSDK.SplashKit;string name;Write("What is your name: ");name = ReadLine();WriteLine();WriteLine($"Hi {name}.");WriteLine();
-
-
Now compile and run the program to see the output. Check it against what we were aiming to achieve.