Hello. I'm new to C++. I was just practicing this code and I noticed that even if I input 5 or 1 or 2, the first output result is always wrong in the loop. If I press 5, the output is,
"You've inserted USD 5 note which is not supported."
Why is it doing that?
But when the loop wants user prompts, it works fine. Here's the code.
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
string a = "Coca Cola";
string b = "Pepsi";
string c = "Sprite";
string d = "Nescafe Ice Mocha";
string f;
double e; //The money inserted by the user
cout << "Welcome to the Vending Machine!\n";
cout << "Please Enter only USD 1, 2 & 5 notes: ";
do
{
cin >> e;
cout << endl;
if (e != 1 && e != 2 && e != 5 )
{
cout << "You have inserted " << e << " USD note which is not supported.\n";
cout << "Please Try Again!: \n";
}
}
while (e != 1 && e != 2 && e != 5 );
cout << "You've entered " << e << " !";
cout << endl;
return 0;
}
Thank you, MiiNiPaa. Actually I initially wrote the code with While loop and it worked liked the one CodeWriter wrote. I just wanted to explore a bit since I hardly saw do-while loop in action and that' why I thought I'd give it a go. Thank you for the links :)
And thank you CodeWriter for taking your time to solve the problem. Appreciate it! :)