Skip to content

Instantly share code, notes, and snippets.

@carlstoker
Created February 5, 2021 01:31
Show Gist options
  • Select an option

  • Save carlstoker/9648950ccf243da915f704565f88f231 to your computer and use it in GitHub Desktop.

Select an option

Save carlstoker/9648950ccf243da915f704565f88f231 to your computer and use it in GitHub Desktop.
While loop type comparisons
#include <iostream>
#include <string>
using namespace std;
int main () {
//Good
string input;
do {
cout << "Enter foo: ";
cin >> input;
} while (input.compare("foo"));
//Okay
cout << "Enter bar: ";
cin >> input;
while (input.compare("bar")) {
cout << "Enter bar: ";
cin >> input;
}
//Bad
while (true) {
cout << "Enter baz: ";
cin >> input;
if (!input.compare("bar")) { break; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment