+ 3

Help with loop statements.

Hello. My program doesn't loop accordingly as I'd want it to. I'm trying to make it loop whenever the burger input is greater than 5, asking the same question if the input is such. ", we can only make a maximum of 5 burgers at the moment. How many would you like?" cin >> burger; The line above to be exact, I wish to put it in a loop whenever the input is >5. The program: #include<iostream> #include <string> using namespace std; int main() { int burger = 0; string name; cout << "What's your name?"; cin >> name; cout << "Hello, " << name << "! How many burgers would you like?" << endl; cin >> burger; if (burger > 5) { cout << name << ", we can only make a maximum of 5 burgers at the moment. How many would you like?" << endl; cin >> burger; } else { cout << "Your food is coming." << endl; } system("pause"); }

20th Oct 2017, 2:01 AM
Melonie Cardiff
Melonie Cardiff - avatar
2 ответов
+ 6
Here is some slight modification that does what you need , let me know if you need something else. =: #include<iostream> #include <string> using namespace std; int main() { unsigned int burger = 0; // I made this unsigned, since you // cannot take -3 burgers. cout << "What's your name?"; string name; cin >> name; cout << "Hello, " << name << "! How \ many burgers would you like?" << endl; cin >> burger; // The loop. This will continue taking //input till you give a proper input //(Input in the range needed). while (burger > 5) { cout<<name<< ", we can only make a \ maximum of 5 burgers at the \ moment. How many would \ you like?\n"; cin >> burger; } // The else statement. Will execute only // when the number in the required // range is given. The loop then stops. cout << "Your food is coming." << endl; system("pause"); }
20th Oct 2017, 2:41 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
0
and u can use 'for'loop
27th Oct 2017, 12:46 AM
Chrowder