Menu
Coddy logo textTech

Getting Input

Part of the Fundamentals section of Coddy's Swift journey — lesson 38 of 86.

challenge icon

Challenge

Easy

In the previous lesson, you printed a welcome message. Now, extend your calculator to gather the necessary information from the user.

After the welcome message, read three inputs from the user:

  1. The total bill amount (a decimal number)
  2. The tip percentage (an integer)
  3. The number of people splitting the bill (an integer)

You will receive the following inputs:

  • First line: The bill total as a string (e.g., "85.50")
  • Second line: The tip percentage as a string (e.g., "20")
  • Third line: The number of people as a string (e.g., "4")

Use readLine() to read each input and safely unwrap them using if let. Convert the bill total to a Double and the other two values to Int.

Print the values in this exact format:

Welcome to the Bill Split Calculator!
Bill: [billTotal]
Tip: [tipPercentage]%
People: [numberOfPeople]

For example, if the inputs are 85.5, 20, and 4, the output should be:

Welcome to the Bill Split Calculator!
Bill: 85.5
Tip: 20%
People: 4

Try it yourself

print("Welcome to the Bill Split Calculator!")

All lessons in Fundamentals