
-----------------------------------
Cantharus
Tue Jun 03, 2014 5:42 pm

Vending Machine program problem
-----------------------------------
What is it you are trying to achieve?
Trying to fix an error


What is the problem you are having?
There's a problem in my code where:


elsif paymentchoice = 1 and drink < 6 and creditpayment = drinkprice then
    put "Thank you, enjoy your drink!"
    
elsif paymentchoice = 1 and drink < 6 and creditpayment > drinkprice then
    put "You paid more then enough, here's your change! $"..
    put creditpayment - drinkprice
    put ", come again!"
    
elsif paymentchoice = 1 and drink < 6 and creditpayment < drinkprice then
    put "You didn't pay enough, sorry!"

the message wouldn't show up, even if all the the prerequisites to show the message is true.


Describe what you have tried to solve this problem
I have tried moving the block of code right under the code that asks for the credit card number which has worked but then another part of the code doesn't work.



%-----------------------------------------------------------%-----------------------------------------------------------
%-----------------------------------------------------------%-----------------------------------------------------------
var drink : real
var z : real
var payment : real
var paymentchoice : real
var cash : real
var cardnumber : real                                %Variables.
var bank : real
var answer : real
var cashpayment : real
var creditpayment : real
var creditpayment2 : real
var drinkprice : real
var drinkprice2 : real
%-----------------------------------------------------------%-----------------------------------------------------------

%-----------------------------------------------------------%-----------------------------------------------------------
drinkprice := 1.5
drinkprice2 := 2                                   %Assigning a value to a variable.
bank := 5
%-----------------------------------------------------------%-----------------------------------------------------------

%-----------------------------------------------------------%-----------------------------------------------------------
loop

put "List of drinks available:"
put "-------------------------"
put "1: Orange Soda"
put "2: Coca Cola"
put "3: Sprite"
put "4: Mountain Dew"
put "5: Fanta"                                    %List of drinks the user can choose from.
put "6: Diet Coke"
put "7: Pepsi"
put "8: Dr Pepper"
put "9: Ginger Ale"
put "10: Monster"    
put ""
%-----------------------------------------------------------%-----------------------------------------------------------

%-----------------------------------------------------------%-----------------------------------------------------------
loop

put "Please enter the number associated with the drink that you would like."
    get drink
    
put ""

if drink = 1 then
    put "You have chosen Orange Soda, please pay $"..
    put drinkprice
    
elsif drink = 2 then
    put "You have chosen Coca Cola, please pay $"..
    put drinkprice

elsif drink = 3 then
    put "You have chosen Sprite, please pay $"..
    put drinkprice 

elsif drink = 4 then
    put "You have chosen Mountain Dew, please pay $"..
    put drinkprice 

elsif drink = 5 then
    put "You have chosen Fanta, please pay $"..                      %'if' statements that'll display a different price / error message depending on the number the user chooses.
    put drinkprice

elsif drink = 6 then
    put "You have chosen Diet Coke, please pay $"..
    put drinkprice2

elsif drink = 7 then
    put "You have chosen Pepsi, please pay $"..
    put drinkprice2

elsif drink = 8 then
    put "You have chosen Dr. Pepper, please pay $"..
    put drinkprice2

elsif drink = 9 then
    put "You have chosen Ginger Ale, please pay $"..
    put drinkprice2

elsif drink = 10 then
    put "You have chosen Monster, which costs $"..
    put drinkprice2

elsif drink > 10 then
    put "You have put an invalid number, please try again."

put ""

end if
    exit when drink < 10 or drink = 10
    end loop
%-----------------------------------------------------------%-----------------------------------------------------------

%-----------------------------------------------------------%-----------------------------------------------------------
loop   
    
put ""

put "Please select the number associated with the payment choice from the available  choices!"
put ""

put "1. Credit Card or 2. Cash"
    get paymentchoice
    
put ""

if paymentchoice = 1 and drink > 5 then               
    put "You chose to pay by credit card, please type in your credit card number!"
    
elsif paymentchoice = 1 and drink < 6 then
    put "You have chose to pay by credit card, please type in your credit card number!"     %This part asks the user to choose a way of paying for the drink, and display a message according the set of options he has chosen.
                                                                                            %For example, if he chose the #3 drink and chose to pay by credit card, it'll ask you for your credit card number.
elsif paymentchoice = 2 and drink > 5 then                                                
    put "You chose to pay by cash, please put in $"..
    put drinkprice2
    
elsif paymentchoice = 2 and drink < 6 then
    put "You chose to pay by cash, please put in $"..
    put drinkprice

elsif paymentchoice > 2 then    
    put "You chose an invalid option, please try agian."
    exit when paymentchoice = 1 or paymentchoice = 2

end if
exit when paymentchoice = 1 or paymentchoice = 2
end loop
%-----------------------------------------------------------%-----------------------------------------------------------

loop

if paymentchoice = 1 and drink > 5 then
    get cardnumber
    put "Your remaining balance is $"..
    put bank..
    put ", please pay $"..
    put drinkprice2
    get creditpayment2
    
end if                                                             %This part will display the price of the drink to the user and the remaining balance of their bank account.
                                                                   %This part will also ask the user to pay the amount of money required to recieve the drink they chose.
if paymentchoice = 1 and drink < 6 then
    get cardnumber
    put "Your remaining balance is $"..
    put bank..
    put ", please pay $"..
    put drinkprice
    get creditpayment
%-----------------------------------------------------------%-----------------------------------------------------------

%-----------------------------------------------------------%-----------------------------------------------------------    
elsif paymentchoice = 1 and drink > 5 and creditpayment2 = drinkprice2 then
    put "Thank you, enjoy your drink! Your remaining balance is $"..
    bank := bank - creditpayment2
    put bank..
    put ", do you want to buy another drink? Enter 1 if you do or 2 if you don't."
    get answer
    if answer = 1
    then end if
    
elsif paymentchoice = 1 and drink > 5 and creditpayment > drinkprice2 then
    put "You payed more then enough, here's your change! $"..
    put creditpayment - drinkprice2..
    put ", come again!"
    
elsif paymentchoice = 1 and drink > 5 and creditpayment < drinkprice2 then
    put "You didn't pay enough, sorry!" 
    
elsif paymentchoice = 1 and drink < 6 and creditpayment = drinkprice then      %This part of the code will display appropiate messages according to the amount of money the user paid and what drink he chose.
    put "Thank you, enjoy your drink!"
    
elsif paymentchoice = 1 and drink < 6 and creditpayment > drinkprice then
    put "You paid more then enough, here's your change! $"..
    put creditpayment - drinkprice
    put ", come again!"
    
elsif paymentchoice = 1 and drink < 6 and creditpayment < drinkprice then
    put "You didn't pay enough, sorry!"
    
elsif paymentchoice = 2 then
    get cashpayment    
    
if paymentchoice = 2 and drink > 5 and cashpayment = drinkprice2 then
    put "Thank you, enjoy your drink!"
%-----------------------------------------------------------%-----------------------------------------------------------     
    
end if 
end if
end loop
end loop


Please specify what version of Turing you are using
Turing 4.1.2

Thank youuuuu.

-----------------------------------
Tony
Tue Jun 03, 2014 7:08 pm

Re: Vending Machine program problem
-----------------------------------

the message wouldn't show up, even if all the the prerequisites to show the message is true.
Are you sure that they are true? One way to check is to print all the values before evaluating the condition. E.g.
[code]
put "values are: ", paymentchoice, " | ", drink, " | ", creditpayment, " | ", drinkprice
if paymentchoice = 1 and drink < 6 and creditpayment = drinkprice then
    put "Thank you, enjoy your drink!"
[/code]

Getting to the smallest amount of code that's required to demonstrate the error will make it much easier to identify and fix the problem.

-----------------------------------
Cantharus
Tue Jun 03, 2014 8:05 pm

Re: Vending Machine program problem
-----------------------------------

the message wouldn't show up, even if all the the prerequisites to show the message is true.
Are you sure that they are true? One way to check is to print all the values before evaluating the condition. E.g.


I've fixed the problem, how I did it is beyond me, but I won't complain! Here is the code I have now: 

%-----------------------------------------------------------%-----------------------------------------------------------
var drink : real
var payment : real
var paymentchoice : real
var cash : real
var cardnumber : real                                %Variables.
var bank : real
var answer : real
var cashpayment : real
var cashpayment2 : real
var creditpayment : real
var creditpayment2 : real
var drinkprice : real
var drinkprice2 : real
var cashchange : real
var change : real
%-----------------------------------------------------------%-----------------------------------------------------------

%-----------------------------------------------------------%-----------------------------------------------------------
drinkprice := 1.5
drinkprice2 := 2                                   %Assigning a value to a variable.
bank := 5
change := 1
cashchange := 1
%-----------------------------------------------------------%-----------------------------------------------------------

%-----------------------------------------------------------%-----------------------------------------------------------
loop

put "List of drinks available:"
put "-------------------------"
put "1: Orange Soda"
put "2: Coca Cola"
put "3: Sprite"
put "4: Mountain Dew"
put "5: Fanta"                                    %List of drinks the user can choose from.
put "6: Diet Coke"
put "7: Pepsi"
put "8: Dr Pepper"
put "9: Ginger Ale"
put "10: Monster"    
put ""
%-----------------------------------------------------------%-----------------------------------------------------------

%-----------------------------------------------------------%-----------------------------------------------------------
loop

put "Please enter the number associated with the drink that you would like."
    get drink
    
put ""

if drink = 1 then
    put "You have chosen Orange Soda, please pay $"..
    put drinkprice
    
elsif drink = 2 then
    put "You have chosen Coca Cola, please pay $"..
    put drinkprice

elsif drink = 3 then
    put "You have chosen Sprite, please pay $"..
    put drinkprice 

elsif drink = 4 then
    put "You have chosen Mountain Dew, please pay $"..
    put drinkprice 

elsif drink = 5 then
    put "You have chosen Fanta, please pay $"..                      %'if' statements that'll display a different price / error message depending on the number the user chooses.
    put drinkprice

elsif drink = 6 then
    put "You have chosen Diet Coke, please pay $"..
    put drinkprice2

elsif drink = 7 then
    put "You have chosen Pepsi, please pay $"..
    put drinkprice2

elsif drink = 8 then
    put "You have chosen Dr. Pepper, please pay $"..
    put drinkprice2

elsif drink = 9 then
    put "You have chosen Ginger Ale, please pay $"..
    put drinkprice2

elsif drink = 10 then
    put "You have chosen Monster, which costs $"..
    put drinkprice2

elsif drink > 10 then
    put "You have put an invalid number, please try again."

put ""

end if
    exit when drink < 10 or drink = 10
    end loop
%-----------------------------------------------------------%-----------------------------------------------------------

%-----------------------------------------------------------%-----------------------------------------------------------
loop   
    
put ""

put "Please select the number associated with the payment choice from the available  choices!"
put ""

put "1. Credit Card or 2. Cash"
    get paymentchoice
    
put ""

if paymentchoice = 1 and drink > 5 then               
    put "You chose to pay by credit card, please type in your credit card number!"
    
elsif paymentchoice = 1 and drink < 6 then
    put "You have chose to pay by credit card, please type in your credit card number!"     %This part asks the user to choose a way of paying for the drink, and display a message according the set of options he has chosen.
                                                                                            %For example, if he chose the #3 drink and chose to pay by credit card, it'll ask you for your credit card number.
elsif paymentchoice = 2 and drink > 5 then                                                
    put "You chose to pay by cash, please put in $"..
    put drinkprice2
    
elsif paymentchoice = 2 and drink < 6 then
    put "You chose to pay by cash, please put in $"..
    put drinkprice

elsif paymentchoice > 2 then    
    put "You chose an invalid option, please try agian."
    exit when paymentchoice = 1 or paymentchoice = 2

end if
exit when paymentchoice = 1 or paymentchoice = 2
end loop
%-----------------------------------------------------------%-----------------------------------------------------------

loop

if paymentchoice = 1 and drink > 5 then
    get cardnumber
    put "Your remaining balance is $"..
    put bank..
    put ", please pay $"..
    put drinkprice2
    get creditpayment2
    
end if                                                             %This part will display the price of the drink to the user and the remaining balance of their bank account.
                                                                   %This part will also ask the user to pay the amount of money required to recieve the drink they chose.
if paymentchoice = 1 and drink < 6 then
    get cardnumber
    put "Your remaining balance is $"..
    put bank..
    put ", please pay $"..
    put drinkprice
    get creditpayment

end if
%-----------------------------------------------------------%-----------------------------------------------------------

%-----------------------------------------------------------%-----------------------------------------------------------    
if paymentchoice = 1 and drink > 5 and creditpayment2 = drinkprice2 then
    put "Thank you, enjoy your drink! Your remaining balance is $"..
        bank := bank - creditpayment2
        put bank..
        put ", do you want to buy another drink? Enter 1 if you do or 2 if you don't."   
            get answer
            exit when answer = 1
elsif bank = 0 or bank < 0 then
    put "Sorry, you have insufficent funds."    
end if   
    
if paymentchoice = 1 and drink > 5 and creditpayment2 > drinkprice2 then
    put "You payed more then enough, here's your change $"..
        change := creditpayment2 - drinkprice2
        put change..
        put ", your remaining balance is $"..
        put bank - drinkprice2 
        put ". Do you want to buy another drink? Enter 1 if you do or 2 if you don't."
            get answer
            exit when answer = 1
elsif bank = 0 or bank < 0 then
    put "Sorry, you have insufficent funds." 
end if

if paymentchoice = 1 and drink > 5 and creditpayment2 < drinkprice2 then
    put "You didn't pay enough, sorry! Do you want to try and buy another drink? Enter 1 if you do or 2 if you don't." 
        get answer
        exit when answer = 1
end if
    
if paymentchoice = 1 and drink < 6 and creditpayment = drinkprice then      %This part of the code will display appropiate messages according to the amount of money the user paid and what drink he chose.
    put "Thank you, enjoy your drink! Your remaining balance is $"..
        bank := bank - creditpayment
        put bank..
        put ", do you want to buy another drink? Enter 1 if you do or 2 if you don't."
            get answer
            exit when answer = 1
elsif bank = 0 or bank < 0 then
    put "Sorry, you have insufficent funds." 
end if
    
if paymentchoice = 1 and drink < 6 and creditpayment > drinkprice then
    put "You paid more then enough, here's your change! $"..
        change := creditpayment - drinkprice
        put change..
        put ", your remaining balance is $"..
        put bank - drinkprice
        put ". Do you want to buy another drink? Enter 1 if you do or 2 if you don't."
            get answer
            exit when answer = 1
elsif bank = 0 or bank < 0 then
    put "Sorry, you have insufficent funds."     
end if
    
if paymentchoice = 1 and drink < 6 and creditpayment < drinkprice then
    put "You didn't pay enough, sorry! Do you want to try and buy another drink? Enter 1 if you do or 2 if you don't."
        get answer
        exit when answer = 1
end if    
    
if paymentchoice = 2 and drink > 5 then
    get cashpayment2
    
elsif paymentchoice = 2 and drink < 6 then
    get cashpayment    

end if
    
if paymentchoice = 2 and drink > 5 and cashpayment2 = drinkprice2 then
    put "Thank you, enjoy your drink! Do you want to buy another drink? Enter 1 if you do or 2 if you don't."
    get answer
    exit when answer = 1
    
end if    

if paymentchoice = 2 and drink > 5 and cashpayment2 > drinkprice2 then
    put "You paid more then enough, here's your change $"..
    cashchange := cashpayment - drinkprice2
    put cashchange..
    put ". Do you want to buy another drink?"
    get answer
    exit when answer = 1
    
end if    

if paymentchoice = 2 and drink > 5 and cashpayment2 < drinkprice2 then
    put "You didn't pay enough, sorry! Do you want to try and buy another drink? Enter 1 if you do or 2 if you don't."
    get answer
    exit when answer = 1
    
end if

if paymentchoice = 2 and drink < 6 and cashpayment = drinkprice then
    put "Thank you, enjoy your drink! Do you want to buy another drink? Enter 1 if you do or 2 if you don't."
    get answer
    exit when answer = 1
    
end if

if paymentchoice = 2 and drink < 6 and cashpayment > drinkprice then
    put "You paid more then enough, here's your change $"..
    cashchange := cashpayment - drinkprice
    put cashchange..
    put ". Do you want to buy another drink?"
    get answer
    exit when answer = 1
    
end if

if paymentchoice = 2 and drink < 6 and cashpayment < drinkprice then
    put "You didn't pay enough, sorry! Do you want to try and buy another drink? Enter 1 if you do or 2 if you don't."
    get answer
    exit when answer = 1
    
end if
%-----------------------------------------------------------%-----------------------------------------------------------     
    
end loop
end loop



But what I am trying to do now that I've fixed everything else is that, if the user chooses to pay by credit card, and he has a set $5 in his account, I want to show a message saying "You have insufficient funds" if he only has $1 in his account and attempts to buy a $1.50 drink, got any ideas on how to do that?

-----------------------------------
Srlancelot39
Tue Jun 03, 2014 9:39 pm

Re: Vending Machine program problem
-----------------------------------
I want to show a message saying "You have insufficient funds" if he only has $1 in his account and attempts to buy a $1.50 drink, got any ideas on how to do that?
After they select the drink, compare the drink cost to their credit card balance and only proceed with the transaction if they have sufficient funds.  Else, decline the transaction.
