Back

Activity 1.4: Asking a Question

divider

Activity 1.4

Asking a Question

Key Concepts

Getting Text Input From a User

Prompt, Wait, Store, Convert, and Respond

You Have Seen This Program

Console Template
Console.Write("What is your name? ");
string name = Console.ReadLine();
Console.WriteLine($"Nice to meet you, {name}!");

You ran it in Activity 1.1. Today you will understand every line.

A Program Can Have a Conversation

  1. Prompt the user with a question.
  2. Wait for the user to type and press Enter.
  3. Store the answer in a variable.
  4. Use the answer in a response.

1. Prompt the User

Demo
Console.Write("What is your name? ");

Console.Write displays text and leaves the cursor on the same line.

2. Wait for an Answer

Demo
string name = Console.ReadLine();

Console.ReadLine() pauses the program until the user presses Enter.

3. Store the Answer

Demo
string name = Console.ReadLine();

The text the user entered is assigned to the string variable named name.

4. Use the Answer

Demo
Console.WriteLine($"Nice to meet you, {name}!");

The variable's value becomes part of the program's response.

The Complete Conversation

Demo
Console.Write("What is your name? ");
string name = Console.ReadLine();
Console.WriteLine($"Nice to meet you, {name}!");
Demo
What is your name? Sam [Enter]
Nice to meet you, Sam!

Write or WriteLine?

Write

Leaves the cursor on the same line.

WriteLine

Moves the cursor to the next line.

Write or WriteLine?

Demo
Console.Write("First");
Console.Write("Second");
Console.WriteLine();
Console.WriteLine("Third");
Console.WriteLine("Fourth");
Demo
FirstSecond
Third
Fourth

Keyboard Input Starts as Text

In this activity, each line read from the keyboard is stored in a string variable.

Even if the user types digits, Console.ReadLine() gives the program text.

Keyboard Input Starts as Text

Demo
Console.Write("How old are you? ");
string age = Console.ReadLine();
Console.WriteLine(age);
Demo
How old are you? 16 [Enter]
16

Convert Number Text

int.Parse converts text containing a whole number into an int value.

Demo
Console.Write("How many minutes? ");
string minutesText = Console.ReadLine();
int minutes = int.Parse(minutesText);
Console.WriteLine(minutes);

Convert Number Text

Demo
Console.Write("How many minutes? ");
string minutesText = Console.ReadLine();
int minutes = int.Parse(minutesText);
Console.WriteLine(minutes);
Demo
How many minutes? 30 [Enter]
30

Parsing Can Fail

int.Parse expects text that represents a whole number.

Entering "thirty" produces an exception because C# cannot convert that word to an int.

Parsing Can Fail

Terminal
How many minutes? thirty [Enter]
Unhandled exception. System.FormatException:
The input string 'thirty' was not in a correct format.

Compiler Error or Exception?

Compiler Error

The code could not be translated, so it never started.

Exception

The program started, then encountered a problem while running.

Insert a Variable Into Text

Demo
string name = "Sam";
Console.WriteLine($"Hello, {name}!");

The $ before the opening quote creates an interpolated string.

C# replaces {name} with the value stored in name.

Do Not Forget the Dollar Sign

Missing $
string name = "Sam";
Console.WriteLine("Hello, {name}!");

Prints: Hello, {name}!

With $
string name = "Sam";
Console.WriteLine($"Hello, {name}!");

Prints: Hello, Sam!

Order Matters

Display the prompt before reading input so the user knows what to type.

Store the answer before trying to use its variable.

Today's Objectives

  • Prompting a user with Console.Write
  • Reading a line of text with Console.ReadLine()
  • Storing input in a string variable
  • Converting whole-number text with int.Parse
  • Recognizing a runtime exception

Key Terms

Input
Information a program receives from a user or another source.
Prompt
A message that tells the user what information to enter.
Console.ReadLine()
Waits for the user to enter a line of text and press Enter.
Interpolated String
A string beginning with $ that can insert variable values inside braces.
Parse
Convert text into another data type, such as an integer.
Exception
A problem encountered after a program has started running.

'F' → Fullscreen

divider

Build

Copy template-console and rename the copy 1-04-conversation. Open its Program.cs file.


Task 1: Ask and Respond

  1. Replace the starter code with the program below.
  2. Click the triangular Run Code play button.
  3. Type your name in the terminal and press Enter.
  4. Confirm that the same name appears in the response.
ASKING A QUESTION
Console.Write("What is your name? ");
string name = Console.ReadLine();
Console.WriteLine($"Nice to meet you, {name}!");

Task 2: Continue the Conversation

  1. Add a second prompt asking for a favorite game.
  2. Store the answer in favoriteGame.
  3. Add both response statements shown below.
  4. Run and answer both questions.
ASKING A QUESTION
Console.Write("What is your name? ");
string name = Console.ReadLine();
Console.Write("What is your favorite game? ");
string favoriteGame = Console.ReadLine();
Console.WriteLine();
Console.WriteLine($"Nice to meet you, {name}!");
Console.WriteLine($"I have heard good things about {favoriteGame}.");

Task 3: Ask for a Whole Number

  1. Ask how many minutes the user plays in a typical week.
  2. Store the keyboard input in minutesText.
  3. Parse that text and store the result in the int variable minutes.
  4. Display the number using an interpolated string.
  5. Run and enter a whole number.
ASKING A QUESTION
Console.Write("What is your name? ");
string name = Console.ReadLine();
Console.Write("What is your favorite game? ");
string favoriteGame = Console.ReadLine();
Console.Write("How many minutes do you play in a typical week? ");
string minutesText = Console.ReadLine();
int minutes = int.Parse(minutesText);
Console.WriteLine();
Console.WriteLine($"Nice to meet you, {name}!");
Console.WriteLine($"I have heard good things about {favoriteGame}.");
Console.WriteLine($"You entered {minutes} minutes.");

Task 4: Cause and Read an Exception

  1. Run the program again.
  2. When asked for minutes, enter thirty.
  3. Find the words System.FormatException in the terminal.
  4. Explain why the program compiled successfully but stopped while running.
  5. Run again with a whole number and confirm that the program recovers.

Challenge (Optional): Add One More Question

  • Ask one more school-appropriate question of your choice.
  • Store the answer in a descriptive camel-case variable.
  • Work that answer into a new interpolated response.
divider

Same Idea, Exam Notation

The AP exam's INPUT() accepts a value directly. It does not show C#'s separate text-reading and parsing steps.

C#
Console.WriteLine("How many minutes?");
string minutesText = Console.ReadLine();
int minutes = int.Parse(minutesText);
Console.WriteLine(minutes);
AP Exam Notation
DISPLAY("How many minutes?")
minutes ← INPUT()
DISPLAY(minutes)
divider

Checkpoint

Run the finished program and enter the example answers below. Your output should follow the same conversation pattern.

Example Output
What is your name? Sam [Enter]
What is your favorite game? Minecraft [Enter]
How many minutes do you play in a typical week? 120 [Enter]
Nice to meet you, Sam!
I have heard good things about Minecraft.
You entered 120 minutes.
  • The program asks three questions and waits after each one.
  • The two text answers are stored in string variables.
  • The minutes answer is read as text and parsed into an int.
  • The response uses all three stored values.
  • The program compiles without an error.
divider

Reflection

Answer the following questions before submitting your work.

  1. What happens when C# reaches Console.ReadLine()?
  2. Why does the program store the minutes answer in minutesText before assigning a value to minutes?
  3. Why did entering thirty cause an exception rather than a compiler error?
divider

Submit

Submit your completed activity sheet. Keep 1-04-conversation in your workspace.

Activity Complete