← Back

Activity 1.7 Prompting for Numerical Input


Introduction

In the last activity, we learned how to prompt the user for input and store their response in a variable using Console.ReadLine(). However, there’s an important detail to remember: Console.ReadLine() method always returns the user input as a string. This means if you ask the user to enter a number, you’ll get that number as text, not as something you can use for arithmetic.

Examples

string num1 = "5";
string num2 = "3";
Console.WriteLine(num1 + num2); // Outputs "53", not 8!
Console.WRiteLine(num1 * num2); // Error, cannot multiply strings

Converting Strings to Numbers

There are a few methods in C# that help us convert strings that contain numbers into numeric types, such as integers and doubles.

These methods will attempt to convert the string into its corresponding number, and if successful, it will return that number. Remember, some methods can return data. We can then save that returned value to a variable or use it in some other expression.

Examples

// Converting to an integer (whole number)
int number = Convert.ToInt32("32"); // Converts the string "32" to the number 32
Console.WriteLine(number + 10); // Outputs 42

// Converting to a double (number with a decimal)
double decimalNumber = Convert.ToDouble("100.4"); // Converts the string "100.4" to 100.4
Console.WriteLine(decimalNumber + 10.6); // Outputs 111.0

Example Video

Handling Errors with Invalid Input

What happens if a user enters something unexpected, like trying to convert “abc” into a number? Or what if we try to convert a string that contains a decimal into an integer, like “32.5”?

int bad = Convert.ToInt32("32.5"); // This will cause an error!

In this case, we’ll get an error because the Convert.ToInt32() method cannot handle a number with a decimal—it only works with whole numbers.

Skills to Practice


Instructions

Setup

  1. Create a new C# console application. Name the project 1.7 Numerical Input.
  2. Click Create.

Code

Console.WriteLine("--- Demo 1 - Age to Months Calculator ---");
// Steps involved in the statements below:
// 1) User types in age
// 2) Convert string input to integer and save it to a new variable
Console.Write("Enter your age: ");
string ageString = Console.ReadLine();
int age = Convert.ToInt32(ageString);

Console.WriteLine($"You are {age} years old. That's {age * 12} months old, you old fart!\n");


Console.WriteLine("\nPress enter to continue...");
Console.ReadLine();


Console.WriteLine("--- Demo 2 - Square Area Calculator ---");
// Shorter, concise way to get input, convert to number, and save to variable
Console.Write("Enter the length of the square: ");
double length = Convert.ToDouble(Console.ReadLine());
double area = length * length;

Console.WriteLine($"The area of the square is {area}");

Debug

--- Demo 1 - Age to Months Calculator ---
Enter your age: 30
You are 30 years old. That's 360 months old, you old fart!


Press enter to continue...

--- Demo 2 - Square Area Calculator ---
Enter the length of the square: 45
The area of the square is 2025

Tips, Tricks, and Reflection

If writing input, conversion, and variable assignment all in one line feels confusing, it’s completely okay to break it up into multiple steps. In fact, it’s often helpful to write each step on its own line when you’re learning, so you can better understand what’s happening at each stage.

Remember, there’s no rush to write code in fewer lines—what’s important is that the code makes sense to you! Breaking things down into smaller steps can make it easier to spot errors and understand how each part of the program works.