Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <sstream>
- #include <ctime>
- #include <cstdlib>
- using namespace std;
- int main (int argc, char** argv)
- {
- // the upper and lower limits of which numbers we can choose from.
- int numLower = 1;
- int numHigher = 10;
- // generate our random number
- srand(time(NULL));
- int secret = rand() % numHigher + numLower;
- // the variables we will use to hold our answer, split among three.
- stringstream answerBuffer;
- int answerNumber;
- string answerString;
- // useless statistical knickknacks
- int winCount = 0;
- int tryCount = 0;
- // flags whether we are still playing the game or not.
- bool playing = true;
- do
- {
- cout << "Guess a number between 1 and 10, enter q to quit." << endl;
- // get our input from cin
- getline(cin, answerString);
- if (answerString.compare("q") == 0 || answerString.compare("Q") == 0)
- {
- // if Q is found, skip the game and end the loop.
- playing = false;
- }
- else
- {
- // otherwise, play a round.
- tryCount++;
- // insert our answer into the stringstream buffer
- answerBuffer << answerString;
- // extract our answer into the answerNumber integer
- answerBuffer >> answerNumber;
- if (answerNumber == secret)
- {
- cout << "You picked the right number!" << endl;
- // generate a new number so we can play again
- secret = rand() % numHigher + numLower;
- winCount++;
- }
- else if (answerNumber > numHigher || answerNumber < numLower)
- cout << "You picked an out of bounds number!" << endl;
- else
- cout << "You picked the wrong number." << endl;
- //clear the stringstream so we can use it again.
- answerBuffer.clear();
- }
- } while (playing);
- cout << "Goodbye! You guessed the right number " << winCount << " times out of " << tryCount << " attempts." << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement