what is the best way to prompt the user again after some conditions have met

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;
}
Last edited on
Use a loop. F.e. do <statements> while (condition).
thank you, but i think i have figured out how to do it, i used a flag on the line of code i wanted the program to return. like this:
a: // the flag
cout << "Select a drink: "; // prompts the user for an input
cin >> drink;
cout << "\n";

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 \n";
goto a;
break;
You should NEVER EVER USE GOTO!!!

This codeing style of the 1950th or 1960th. You'll definitely lose control over your program flow! Especially beginners should never be told that this control structure exists. Modern C uses it only for historical and for compatibility reasons to very old software.

One rule of software design declared in the late 1960th is to never jump from elsewhere into a block of statements nor jump out of it except at the end say at well designed entry and exit points.

So please forget those GOTO as soon as possible and use well defined loops instead.
closed account (Dy7SLyTq)
side note: look up comefrom. i agree with tcs. it should only be used in batch. if you use goto in c, there is always a better way to do it
oh... well i thought that might have been a good idea, yeh but i see the point of not using goto in a c programm :{ anyways, i guess i will have to find a better way, maybe using a do while loop as tcs mentioned earlier. Thank you.
Topic archived. No new replies allowed.