I hope you're having a great day. Firstly, I would like to state that I am still a beginner to C++ and the coding world. I am working on a program that simulates a vending machine. This is done using while loops and if and else statements for the conditions and checks. I DO UNDERSTAND that my code is not optimized! I want to UNDERSTAND what I am reading in order for me to learn.
The vending machine program is roughly split into 4 parts. 1- Header 2-money loop 3-drink loop 4-exit loop
I am having a very hard time trying to piece it all together! When I fix one problem I run into another!
I am going to annotate my code in order to explain my thinking and then hopefully criticism is more effective.
`
#include <iostream>
using namespace std;
/*
What I plan on using for conditions, etc.
*/
char orderCharacter;
char orderNumber;
float fundsAvailable = 0;
char moreMoney;
char moreDrinks;
bool quit = false;
bool quitTwo = false;
int main()
{
/*
simple header.
*/
cout<<"========================================"<<endl;
cout<<"Welcome to the Vending Machine"<<endl;
cout<<"========================================"<<endl;
/*
While loop for inserting coins and bills. After user input, if and else output
*/
while (quit == false){
cout<<"Please insert your coins/bills"<<endl;
cout<<"(1)$1, (2)$5, (3)$10, (4)$20 :"<<endl;
cin>>orderNumber;
if (orderNumber == '1'){
fundsAvailable = fundsAvailable + 1;
cout<<"You've inserted: $1"<<endl;
cout<<"Funds available: $"<<fundsAvailable<<endl;
quit = true;
}else if (orderNumber == '2'){
fundsAvailable = fundsAvailable + 5;
cout<<"You've inserted: $5"<<endl;
cout<<"Funds available: $"<<fundsAvailable<<endl;
quit = true;
}else if (orderNumber == '3'){
fundsAvailable = fundsAvailable + 10;
cout<<"You've inserted: $10"<<endl;
cout<<"Funds available: $"<<fundsAvailable<<endl;
quit = true;
}else if (orderNumber == '4'){
fundsAvailable = fundsAvailable + 20;
cout<<"You've inserted: $20"<<endl;
cout<<"Funds available: $"<<fundsAvailable<<endl;
quit = true;
}else{
cout<<"Invalid Selection"<<endl;
quit = false;
}
//same process but for money...
cout<<"Add more coins/bills? (Y/N): ";
cin>>moreMoney;
if ((moreMoney == 'N' || moreMoney == 'n') && (fundsAvailable>=1.50)){
quit = true;
}else if ((moreMoney == 'Y') || (moreMoney == 'y')){
quit = false;
}else if ((moreMoney == 'N' || moreMoney == 'n') && (fundsAvailable<=1.49)){
cout<<"Insufficient funds to make a purchase."<<endl;
cout<<"Please take your change."<<endl;
quit = true;
quitTwo = true;
cout<<"Thank you for using our vending machine!"<<endl;
}else{
cout<<"Your answer is invalid. Please answer Y or N"<<endl;
cout<<"Add more coins/bills? (Y/N): ";
cin>>moreMoney;
}
}
//second while loop drink loop
while (quitTwo == false){
cout<<"Please make a selection:"<<endl;
cout<<"(A)quaVeena $1.50, (B)epsi $2.00, (C)ool Cola $2.00, (G)atorade $2.25"<<endl;
cin>>orderCharacter;
if ((fundsAvailable <= 1.49) && (quitTwo == false)){
cout<<"Insufficient funds to make a purchase."<<endl;
cout<<"Please take your change."<<endl;
cout<<"Thank you for using our vending machine!"<<endl;
}
//calculations
switch (orderCharacter){
case 'A':{
fundsAvailable = fundsAvailable - 1.50;
break;
}
case 'a':{
fundsAvailable = fundsAvailable - 1.50;
break;
}
case 'B':{
fundsAvailable = fundsAvailable - 2.00;
break;
}
case 'b':{
fundsAvailable = fundsAvailable - 2.00;
break;
}
case 'C':{
fundsAvailable = fundsAvailable - 2.00;
break;
}
case 'c':{
fundsAvailable = fundsAvailable - 2.00;
break;
}
case 'G':{
fundsAvailable = fundsAvailable - 2.25;
break;
}
case 'g':{
fundsAvailable = fundsAvailable - 2.25;
break;
}
}
cout<<"Add more drinks (Y/N): ";
cin>>moreDrinks;
//trying to checking available funds before selection, if lower than 1.50, automatically end and other options.
if ((fundsAvailable >=1.50) && (moreDrinks == 'Y' || moreDrinks =='y')){
quitTwo = false;
}else if ((fundsAvailable <=1.49) && (moreDrinks == 'Y' || moreDrinks =='y')){
quitTwo = true;
cout<<"Funds available: $"<<fundsAvailable<<endl;
cout<<"Please take your change."<<endl;
cout<<"Thank you for using our vending machine!"<<endl;
}else if ((fundsAvailable >=1.50) && (moreDrinks == 'N' || moreDrinks =='n')){
quitTwo = true;
cout<<"Funds available: $"<<fundsAvailable<<endl;
cout<<"Please take your change."<<endl;
cout<<"Thank you for using our vending machine!"<<endl;
}else{
cout<<"Your answer is invalid. Please answer Y or N"<<endl;
cout<<"Add more drinks (Y/N): ";
cin>>moreDrinks;
quitTwo = false;
}
}
return 0;
}
`
I am having trouble mostly with drink loop as well as the exit loops. It is simple to have one loop with a condition or two. however I am having a hard time implanting all in sync. For example, if I click '2' for the first user input I have 5 dollars and purchase myself a gatorade for 2.25. the program will work as intended. however now if I make an invalid selection I will bypass the check for available funds. So even if I have 1 dollar, the vending machine will ask the user to make the selection (even though minimum is 1.50).
I know that my fundamentals of my code are lacking. I feel as if my loops aren't correctly stated or formed.
Known issues: Cant make invalid input more than once or code just skips ahead to next loop, fails to check conditions (this is an error of mine of course)