Menu
Coddy logo textTech

Exit The Program

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

challenge icon

Challenge

Easy

In the previous lesson, you printed the menu for the expense tracker. Now, add a loop that continuously displays the menu and reads user input until the user chooses to exit.

Modify your code to:

  1. Create an empty array expenses of type [Double] to store expenses
  2. Use a while true loop to keep the program running
  3. Inside the loop, print the menu (as before)
  4. Read the user's choice using readLine()
  5. If the user enters "5", print Goodbye! and use break to exit the loop
  6. For any other input, print Coming soon... (we'll implement other options in later lessons)

Expected behavior:

  • The menu displays repeatedly until the user enters "5"
  • When "5" is entered, print Goodbye! and exit the loop
  • For choices "1", "2", "3", or "4" (or any other input), print Coming soon... and show the menu again

Example interaction:

1. Add expense
2. View all expenses
3. Show total and average
4. Clear all expenses
5. Exit
1
Coming soon...
1. Add expense
2. View all expenses
3. Show total and average
4. Clear all expenses
5. Exit
5
Goodbye!

Try it yourself

// Daily Expense Tracker - Menu Display

// TODO: Write your code below to print the menu options
// Each option should be on its own line with the format: "number. option text"
print("1. Add expense")
print("2. View all expenses")
print("3. Show total and average")
print("4. Clear all expenses")
print("5. Exit")

All lessons in Fundamentals