File Handling, Loops
FILENAME = "notes.txt"
loop do
puts "\n--- Notes App ---"
puts "1. Write Note"
puts "2. View Notes"
puts "3. Exit"
print "Choose option: "
choice = gets.chomp.to_i
case choice
when 1
print "Enter your note: "
note = gets.chomp
File.open(FILENAME, "a") { |f| f.puts(note) }
puts "Note saved!"
when 2
if File.exist?(FILENAME)
puts "\nYour Notes:"
puts File.read(FILENAME)
else
puts "No notes found."
end
when 3
puts "Goodbye!"
break
else
puts "Invalid option."
end
end
Explanation:
"a"
→ appends to file without deleting old notes.File.read
→ reads all notes.- Stores notes in
notes.txt
.
Leave a Reply