Skip to content

Instantly share code, notes, and snippets.

@jherr
Created January 27, 2021 00:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jherr/3c23da0d7feddaa21144ebe0d412dad6 to your computer and use it in GitHub Desktop.
Save jherr/3c23da0d7feddaa21144ebe0d412dad6 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <list>
using namespace std;
int getValue(int index) {
int guessValue = 0;
/*
while (guessValue < 1 || guessValue > 100) {
cout << "Guess #" << index + 1 << ": ";
cin >> guessValue;
}
*/
do {
cout << "Guess #" << index + 1 << ": ";
cin >> guessValue;
} while (guessValue < 1 || guessValue > 100);
return guessValue;
}
int main() {
srand(time(0));
int value = ( rand() % 100 ) + 1;
cout << "Chosen value is " << value << endl;
int i = 0;
list<int> guesses;
for(; i < 10; ) {
int myGuess = getValue(i);
guesses.push_back(myGuess);
if (myGuess < value) {
cout << "More than that!" << endl;
} else if (myGuess > value) {
cout << "Less than that!" << endl;
} else {
cout << "You win!!!" << endl;
break;
}
if (rand() % 5 < 2) {
cout << "Free pass" << endl;
continue;
}
i++;
}
cout << "You got it in " << i + 1 << " guesses" << endl;
i = 0;
for (list<int>::iterator it = guesses.begin(); it != guesses.end(); ++it, i++) {
cout << "Guess #" << i + 1 << " was " << *it << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment