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

string playerName = "Maya Okonkwo";

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

Change the person and you edit the program.

Let Them Tell You

Console.Write("Player name: ");
string playerName = Console.ReadLine();

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

A waiting program is not a frozen program.

Read It Right to Left

Console.Write("Player name: ");
string playerName = Console.ReadLine();

Console.ReadLine hands back what was typed.

string playerName = catches it.

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

Write the Question, Not WriteLine

Console.Write("Player name: ");
string playerName = Console.ReadLine();
Console.WriteLine($"Welcome, {playerName}.");
Terminal window
Player name: Maya Okonkwo [Enter]
Welcome, Maya Okonkwo.

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("Player name: ");
string playerName = Console.ReadLine();
Terminal window
Player name:
Maya Okonkwo [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.

Do not alternate question, answer, question, answer.

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

Now Ask for a Number

Console.Write("Jersey number: ");
int jerseyNumber = 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 jerseyNumber = "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 a jersey number.

Same number. Same message.

Everything Typed In Is Text

Console.ReadLine always hands back a string.

Even when every character the person typed was a digit.

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.

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("Player name: ");
string playerName = Console.ReadLine();
Console.WriteLine($"Welcome, {playerName}.");
Output
Player name: Maya Okonkwo [Enter]
Welcome, Maya Okonkwo.

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.
Console Input
Console.Write("Player name: ");
string playerName = Console.ReadLine();
Console.Write("Team name: ");
string teamName = Console.ReadLine();
Console.Write("Position: ");
string position = Console.ReadLine();
Console.WriteLine();
Console.WriteLine("=== TOURNAMENT REGISTRATION ===");
Console.WriteLine($"Player: {playerName}");
Console.WriteLine($"Team: {teamName}");
Console.WriteLine($"Position: {position}");
Output
Player name: Maya Okonkwo [Enter]
Team name: Ridge Valley [Enter]
Position: Point Guard [Enter]
=== TOURNAMENT REGISTRATION ===
Player: Maya Okonkwo
Team: Ridge Valley
Position: Point Guard

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.WriteLine();
Console.WriteLine("=== TOURNAMENT REGISTRATION ===");
Console.WriteLine($"Player: {playerName}");
Console.WriteLine($"Team: {teamName}");
Console.WriteLine($"Position: {position}");
// Ask for a school
// 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.
Console Input
Console.Write("Jersey number: ");
int jerseyNumber = Console.ReadLine();
Console.WriteLine($"Next season you could be {jerseyNumber + 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
Player name: Maya Okonkwo [Enter]
Team name: Ridge Valley [Enter]
Position: Point Guard [Enter]
School: St. Edward [Enter]
Shirt size: M [Enter]
=== TOURNAMENT REGISTRATION ===
Player: Maya Okonkwo
Team: Ridge Valley
Position: Point Guard
School: St. Edward
Shirt: M
Maya Okonkwo of Ridge Valley plays Point Guard for St. Edward 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