Noob C++ loops help
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
int yourGuess, randomNumber;
string playAgain;
srand(time(NULL));
cout << "Let's play a game. I will come up with a number and you will have to guess it." << endl;
do {
randomNumber = (rand() % 20) + 1;
do {
cout << "Guess a number: ";
cin >> yourGuess;
if(yourGuess > randomNumber)
cout << "Nope! You were too high." << endl;
else if(yourGuess < randomNumber)
cout << "Nope! You were too low." << endl;
} while(yourGuess != randomNumber);
cout << "Winner Winner!!! You got my number!" << endl;
cout << "Play again?[y/n]: ";
cin >> playAgain;
} while(playAgain == "y");
return 0;
}
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now