Here is a simple drinks machine programm. It basically consists in the user selecting a drink that it's presented, if the user selects correctly the number representing a certain drink, the machine shows the user what was the drink he selected, after his choise the machine asks for money insertion, if the user inserts the incorrect amount of money, the machine shows an error saying that user need to insert the correct amount of money.
My problem is that, if the user selects a different number other than those presentend, the machine displays an error saying that he needs to choose a valid drink, but i can't find a way to reprompt the user again after the error has been displayed. here's the code i hope it isn't that confusing.
// ***Drinkins Machine*** by Roxmate
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
// initializes the variables coca, fanta, iced and drink to be used in switch function,so that the user can choose what drink he wants.
int coca = 1;
int fanta = 2;
int iced = 3;
int drink;
//initializes the vars drink_1, drink_2, drink_3 and money to be used in the if-else-if ladder, in order to check if the input from the user matches the prices of the drinks.
double drink_1 = 1.50;
double drink_2 = 1.30;
double drink_3 = 1.20;
double money;
//outputs what choices the user has and the prices
cout << "::DRINKS MACHINE::" <<"\n \n";
cout << "1 Coca-Cola 1.50$" << "\n";
cout << "2 Fanta 1.30$" << "\n";
cout << "3 Iced tea 1.20$" << "\n \n";
cout << "Select your drink: ";
cin >> drink;
cout << "\n";
// here, i want to reprompt the user to select the drink again if the error occurs
switch(drink) {
case 1:
cout << "You have selected Coca-Cola \n \n";
break;
case 2:
cout << "You have selected Fanta \n \n";
break;
case 3:
cout << "You have selected Iced Tea \n \n";
break;
default:
cout << "::Error:: You must select a valid drink \n";
exit(0); // if i remove this exit(0) the error will occur and the program will continue to execute.
}
// prompts the user for an input
cout << "Insert Money: ";
cin >> money;
cout << "\n";
// if-else-if ladder
if(money == drink_1){
cout << "Thank you here's your drink";
}
else if(money == drink_2)
cout << "Thank you here's your drink";
else if( money == drink_3)
cout << "Thank you here's your drink";
else
cout << "::ERROR::Insert the correct amount of money";
// here, i want to reprompt the user to insert the money again if this error occurs
return 0;
}