Currently I am learning a new program language which is F#. Now, I am working a project about the food ordering system. And, I am currently running into a problem is when I done all the ordering, the system will prompt out a statement says "Do you want to continue pruchase?". Then I input small y and big Y which related to the condition is true, but the system straight away give me "Thank you for your purchase" and press any key to continue statement. Is anyone got any ideas to deal with this issue? Thank you, Thank you.
open System
let menu()=
printfn "---------------------------------------"
printfn " Food ordering system"
printfn "---------------------------------------"
printfn "1. ORDER FOOD "
printfn "2. ORDER DRINK"
Console.Write("Enter your selection : ")
let loop()=
Console.Write("Do you want to continue pruchase?:")
let choice=Console.ReadLine()
if(choice="y"||choice="Y")then
menu()
let food()=
printfn "\n1.MCD CHICKEN RM 10 \n2.FILET-O-FISH RM 13 \n3.FRENCH FRIES RM 5 \n4.APPLE PIE RM 4"
Console.Write("\nEnter food code that you want :")
let option=Console.ReadLine()
Console.Write("How many you want :")
let qty=Console.ReadLine()
if(option = "1")then
let price = 10
let total = int qty*int price
Console.Write("\nThe price for {0} set MCD CHICKEN ",qty)
Console.Write("is RM {0} \n",total)
loop()
Console.Write("\nThank you for your purchase ")
elif(option = "2")then
let price = 13
let total = int qty*int price
Console.Write("\nThe price for {0} set FILET-O-FISH ",qty)
Console.Write("is RM {0} \n",total)
loop()
Console.Write("\nThank you for your purchase ")
elif(option = "3")then
let price = 5
let total = int qty*int price
Console.Write("\nThe price for {0} FRENCH FRIES ",qty)
Console.Write("is RM {0} \n",total)
loop()
Console.Write("\nThank you for your purchase ")
elif(option = "4")then
let price = 4
let total = int qty*int price
Console.Write("\nThe price for {0} APPLE PIE ",qty)
Console.Write("is RM {0} \n",total)
loop()
Console.Write("\nThank you for your purchase ")
else
printfn "Invalid input"
menu()
let drink()=
printfn "\n1.ICE BLENDED CHOCOLATE MEDIUM RM 8 \n2.MANGO PEACH RM 11 \n3.PEPSI RM 4 \n4.ICE LEMON TEA RM 4"
Console.Write("\nEnter beverage code that you want :")
let option=Console.ReadLine()
Console.Write("How many you want :")
let qty=Console.ReadLine()
if(option = "1")then
let price = 8
let total = int qty*int price
Console.Write("\nThe price for {0} ICE BLENDED CHOCOLATE MEDIUM ",qty)
Console.Write("is RM {0} \n",total)
loop()
elif(option = "2")then
let price = 11
let total = int qty*int price
Console.Write("\nThe price for {0} MANGO PEACH ",qty)
Console.Write("is RM {0} \n",total)
loop()
Console.Write("\nThank you for your purchase ")
elif(option = "3")then
let price = 4
let total = int qty*int price
Console.Write("\nThe price for {0} PEPSI ",qty)
Console.Write("is RM {0} \n",total)
loop()
Console.Write("\nThank you for your purchase ")
elif(option = "4")then
let price = 4
let total = int qty*int price
Console.Write("\nThe price for {0} ICE LEMON TEA ",qty)
Console.Write("is RM {0} \n",total)
loop()
Console.Write("\nThank you for your purchase ")
else
printfn "Invalid input"
menu()
let main()=
menu()
let selection=Console.ReadLine()
if(selection = "1") then
food()
else
drink()
main()
CodePudding user response:
This isn't a problem with flushing a buffer. You're calling menu
from loop
, but not waiting for the user's input before continuing.
One way to fix this is to move the ReadLine
call into menu
, but then you'll end up recursively calling food
and drink
from menu
, which is already called from loop
inside food
and drink
. This creates nested control flow that probably isn't what you really want.
I suggest instead to rethink the design so the dependencies between the different levels are clean. Each function should return a value that is used by its calling function, instead of containing loops back to the menu.
CodePudding user response:
To be honest this code is far too imperative and non-idiomatic F# in many ways.
I humbly suggest you post this code to https://codereview.stackexchange.com/ and get a code review there from which to learn.