Back

Activity 1.8: Console Input

divider

Activity 1.8

Console Input

Key Concepts

Asking a Question

Storing the Answer

What Comes Back

You Have Been Writing the Data Yourself

Every value in every program so far was typed in by you.

You Have Been Writing the Data Yourself

string studentName = "Priya Shah";
Console.WriteLine($"Welcome, {studentName}.");
Every single time
Welcome, Priya Shah.

To welcome somebody else, you edit the program.

Let Them Tell You

Console.ReadLine waits for one line to be typed and Enter to be pressed.

Let Them Tell You

Console.Write("Student name: ");
string studentName = Console.ReadLine();
Run it and this is all you get
Student name:

The cursor sits after the colon and nothing else happens.

A waiting program is not a frozen program.

Read It Right to Left

Console.Write("Student name: ");
string studentName = Console.ReadLine();

Console.ReadLine hands back what was typed.

string studentName = catches it.

Same right-to-left reading as every assignment since session 6.

Write the Question, Not WriteLine

Console.Write("Student name: ");
string studentName = Console.ReadLine();
Console.WriteLine($"Welcome, {studentName}.");
Terminal window
Student name: Priya Shah [Enter]
Welcome, Priya Shah.

Console.Write leaves the cursor on the same line, so they type right after the question.

WriteLine Puts Them on the Next Line

Console.WriteLine("Student name: ");
string studentName = Console.ReadLine();
Terminal window
Student name:
Priya Shah [Enter]

Neither is wrong. One of them looks like a form.

That is the third job the Write pair has done for you.

Ask Everything First

Collect all the answers, then print the summary.

Ask Everything First

Question, answer, question, answer
Student name: Priya Shah [Enter]
Student: Priya Shah
Trip: Science Center [Enter]
Trip: Science Center
Every question, then the summary
Student name: Priya Shah [Enter]
Trip: Science Center [Enter]
=== FIELD TRIP SIGN-UP ===
Student: Priya Shah
Trip: Science Center

A form that interrupts itself is hard to read and harder to fix.

Now Ask for a Number

Console.Write("Grade level: ");
int gradeLevel = Console.ReadLine();
Terminal window
1-8-registration.cs(2,20): error CS0029: Cannot implicitly convert type 'string' to 'int'
The build failed. Fix the build errors and run again.

It will not even build.

You Have Met This Exact Error

int level = "24";
Terminal window
1-5-card.cs(1,20): error CS0029: Cannot implicitly convert type 'string' to 'int'
The build failed. Fix the build errors and run again.

Activity 1.5, when you put quotes around your character's level.

Same number. Same message.

Everything Typed In Is Text

Console.ReadLine always hands back a string.

Everything Typed In Is Text

Session 5
1-5-card.cs(1,20): error CS0029: Cannot implicitly convert type 'string' to 'int'
The build failed. Fix the build errors and run again.
Today
1-8-registration.cs(2,20): error CS0029: Cannot implicitly convert type 'string' to 'int'
The build failed. Fix the build errors and run again.

Different file, different line, same sentence.

24 and "24" are still different things, exactly as they were in session 5.

We Are Leaving This Broken

You can collect a number. You cannot do arithmetic with it yet.

The tool that fixes it is the whole of next session.

Write down what you think it will be.

Today's Objectives

  • Reading a typed line with Console.ReadLine
  • Storing input in a variable and reusing it
  • Separating the prompt, the input, and the output
  • Explaining why input arrives as text

Key Terms

Input
Data a program receives while it is running.
Prompt
The message that tells the user what to type.
Console.ReadLine
Waits for one typed line and hands it back as a string.

'F' → Fullscreen

divider

Build

Create a new C# program named 1-8-registration, starting empty.

Keep this file inside the workspace folder. Input code saved anywhere else prints warnings numbered CS8600 and CS8602 that this course never explains. If you see either one, your program is fine — it is in the wrong folder.

How to read the code blocks. Each task's code block shows your whole program, from line 1, as it should look when that task is finished.

You never start a new file and you never delete your work. The green lines marked with a + are the ones you add or change for that task — everything else is already in your file. Do not type the + sign.


Task 1: One Question

  • Write the code below and run it.
  • Answer the question when the cursor stops.
  • Run it a second time with a different answer. The output changes without you editing anything, and that is the whole point of input.
Console Input
Console.Write("Student name: ");
string studentName = Console.ReadLine();
Console.WriteLine($"Welcome, {studentName}.");

Task 2: A Registration Form

  • Add two more questions, each with its own variable.
  • Ask all three questions first, then print the summary. Do not alternate question, answer, question, answer.
  • Line up the labels using spaces inside the strings.
  • Note: the green lines marked with a + sign are new. Do not type the + sign.
  • Task 1's welcome line goes away. The summary block replaces it, so delete that one line as you go — it is the only thing you remove today.
Console Input
Console.Write("Student name: ");
string studentName = Console.ReadLine();
Console.Write("Trip: ");
string destination = Console.ReadLine();
Console.Write("Homeroom: ");
string homeroom = Console.ReadLine();
Console.WriteLine();
Console.WriteLine("=== FIELD TRIP SIGN-UP ===");
Console.WriteLine($"Student: {studentName}");
Console.WriteLine($"Trip: {destination}");
Console.WriteLine($"Homeroom: {homeroom}");

Task 3: Plan, Then Add Two More

  • The three comments are a plan. Write the code for them.
  • The two new questions have to be asked before the summary prints. Think about where they go, not just where the comments are.
  • The last line should read as a sentence using all five answers, not as another list.
Console Input
Console.Write("Student name: ");
string studentName = Console.ReadLine();
Console.Write("Trip: ");
string destination = Console.ReadLine();
Console.Write("Homeroom: ");
string homeroom = Console.ReadLine();
Console.WriteLine();
Console.WriteLine("=== FIELD TRIP SIGN-UP ===");
Console.WriteLine($"Student: {studentName}");
Console.WriteLine($"Trip: {destination}");
Console.WriteLine($"Homeroom: {homeroom}");
// Ask for a bus
// Ask for a shirt size
// Print a one-line sentence using all five answers

Task 4: Ask for a Number

  • At the bottom, add the two lines below and run it.
  • Copy the error message exactly, including the numbers in the parentheses.
  • Then find where you have seen it before. It is in your Activity 1.5 work, with the same number.
  • Leave it broken. Comment the two lines out so the rest of your program still runs, and hand it in that way.
Add to the end of 1-8-registration
Console.Write("Grade level: ");
int gradeLevel = Console.ReadLine();
Console.WriteLine($"Next year you'll be in grade {gradeLevel + 1}.");

Challenge (Optional): Guess the Fix

  • Write a comment at the bottom of your program saying what you think would make Task 4 work.
  • You are not expected to know. Guessing carefully is the exercise, and next session opens on these guesses.
  • While you are there, change one prompt from Console.Write to Console.WriteLine and describe what moved.
divider

Same Idea, Exam Notation

C#
Console.Write("Name? ");
string name = Console.ReadLine();
Console.WriteLine($"Hello, {name}");
Exam Notation
DISPLAY("Name? ")
name ← INPUT()
DISPLAY("Hello, ")
DISPLAY(name)
  • INPUT() is the reference sheet's Console.ReadLine. It hands back what the user typed, and the arrow catches it.
  • The sheet has no types, so it never has to say what kind of value comes back. That is why Task 4 has no equivalent there.
  • DISPLAY takes one value at a time and adds a space after it, so the sheet needs two lines where interpolation needs one.
divider

Checkpoint

Your finished program should run like this.

Example Output
Student name: Priya Shah [Enter]
Trip: Science Center [Enter]
Homeroom: Room 214 [Enter]
Bus: Bus 4 [Enter]
Shirt size: M [Enter]
=== FIELD TRIP SIGN-UP ===
Student: Priya Shah
Trip: Science Center
Homeroom: Room 214
Bus: Bus 4
Shirt: M
Priya Shah of Room 214 is going to Science Center on Bus 4 in a size M shirt.
  • All five questions are asked before anything is summarized.
  • Every answer is stored in a variable named for what it holds.
  • The summary sentence uses all five, and reads as a sentence.
  • Task 4's error is written down, and its two lines are commented out.
divider

Reflection

Answer the following questions before submitting your work.

  1. Your program produces different output every time it runs, without you editing it. Explain what changed and what did not.
  2. Task 4 gave you the same error number as a mistake you made in Activity 1.5, where no user typed anything. What do those two situations have in common?
  3. What do you think Console.ReadLine hands back if the user presses Enter without typing anything? Give a reason for your answer.
divider

Submit

Submit four things to the dropbox:

  1. Your 1-8-registration program file.
  2. A copy of one full run, questions and answers included.
  3. Task 4's error message, copied exactly.
  4. Your three reflection answers.

Activity Complete