Menu
Coddy logo textTech

Exit The Program

Part of the Fundamentals section of Coddy's Kotlin journey. Lesson 85 of 93.

challenge icon

Challenge

Medium

In the previous lesson, you created the menu display for the expense tracker. Now, add a loop that continuously shows the menu and reads the user's choice until they select option 5 to exit.

Wrap your menu in a loop that:

  1. Displays the menu
  2. Reads the user's choice as input
  3. If the choice is "5", print Goodbye! and exit the loop
  4. Otherwise, print Invalid option and show the menu again

Input format:

Each line contains a menu choice (a string). The inputs will continue until "5" is entered.

Output format:

For each iteration, print the menu, then either Invalid option (for choices other than "5") or Goodbye! (when "5" is entered).

Example:

If the inputs are 7, 3, and 5, the output should be:

--- Expense Tracker ---
1. Add Expense
2. View All Expenses
3. Total and Average
4. Clear All
5. Exit
Choose an option:
Invalid option
--- Expense Tracker ---
1. Add Expense
2. View All Expenses
3. Total and Average
4. Clear All
5. Exit
Choose an option:
Invalid option
--- Expense Tracker ---
1. Add Expense
2. View All Expenses
3. Total and Average
4. Clear All
5. Exit
Choose an option:
Goodbye!

Note: For now, only option 5 has actual functionality. All other options (including valid ones like 1-4) should print Invalid option. You'll implement the other options in upcoming lessons.

Try it yourself

fun main() {
    // TODO: Write your code below to print the expense tracker menu
    println("--- Expense Tracker ---")
    println("1. Add Expense")
    println("2. View All Expenses")
    println("3. Total and Average")
    println("4. Clear All")
    println("5. Exit")
    println("Choose an option:")
}

All lessons in Fundamentals

Practice on your own: Kotlin playground