Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I'm trying to create a program based on some problems for beginner c++ online. I came across a problem trying to do this and I need some help

Whenever I go through the do while loop once it doesn't prompt me to input again?
Thanks

C++
#include <iostream>
#include <string>

using namespace std;
int main() {
	string drinkChosen;
	string coke = "coke";
	string fanta = "fanta";
	string pepsi = "pepsi";
	string sprite = "sprite";
	string drpepper = "drpepper";
	string water = "water";
	int x;

	do {
	std::cout << "choose which beverage you would like" <<std::endl << "Coke, Fanta, Pepsi, Sprite or drpepper" << std::endl;
	

	getline(std::cin, drinkChosen);

		if (drinkChosen == coke) {

			std::cout << "you have chosen coke" << std::endl;

		}

		else if (drinkChosen == fanta) {

			std::cout << "you have chosen fanta" << std::endl;

		}

		else if (drinkChosen == pepsi) {

			std::cout << "you have chosen pepsi" << std::endl;

		}
		else if (drinkChosen == sprite) {

			std::cout << "you have chosen sprite" << std::endl;

		}
		else if (drinkChosen == drpepper) {

			std::cout << "you have chosen pepper" << std::endl;

		}
		else if (drinkChosen == water) {

			std::cout << "Please, you know you want a nice drink" << std::endl;

		}
		else {

			std::cout << "please choose again" << std::endl;
		}
		
		std::cout << "Please press 1 to confirm or zero to start again" << std::endl;
		std::cin >> x;
		
		
	} while (x == 0);

	
	
	

	system("pause");
	return 0;
}


What I have tried:

I have tried to move around the "string drinkChosen"
I have also messed around with the while loop's conditions
I have also searched this website and stack overflow
Posted
Updated 12-Jul-16 22:00pm
v2

1 solution

That is because there is still a line end character(s) in the input stream after you enter 0 or 1. Modify your code as follows:
C++
std::cout << "Please press 1 to confirm or zero to start again" << std::endl;
std::cin >> x;
cin.get();

You should also add code to convert the input strings to lower case for your equality tests.
 
Share this answer
 
Comments
CPallini 13-Jul-16 4:25am    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900