Drummerpete Posted January 11, 2011 Author Share Posted January 11, 2011 I wrote a little C++ program.It generates a random number between 1 and 10 and the user has 3 attempts to guess it.OLD CODE:```// Guess the number game// 11/01/11// Pete Murray#include #include #include using namespace std;int main(){ int randomnumber,guess,x; //generated number, user's guess, loop counter int endgame; //so user can end game //generates random number srand(time(0)); randomnumber = rand()%10+1; //prompts user to guess cout << "I am thinking of a number between 1 and 10, can you guess what it is? \n" << "You have 3 guesses. \n\n"; //loop for 3 guesses for (x=1; x<4; x++) { cout << "Guess " << x << ":\n"; cin >> guess; //if guess is correct if (guess == randomnumber) { cout << "Correct, the number was " << randomnumber << "! \n\n"; cout << "Enter any number to end the game \n"; cin >> endgame; return 0; } //if guess is wrong else { //guess was too high if (guess > randomnumber) { cout << guess << " is too high. \n\n"; } //guess was too low else { cout << guess << " is too low. \n\n"; } } } cout << "Enter any number to end the game \n"; cin >> endgame; return 0;} ```NEW CODE:```// Guess the number game II// 11/01/11// Pete Murray#include #include #include using namespace std;int main(){ int randomnumber,guess,x; //generated number, user's guess, loop counter int endgame; //so user can end game //generates random number srand(time(0)); randomnumber = rand()%10+1; //prompts user to guess cout << "I am thinking of a number between 1 and 10, can you guess what it is? \n" << "You have 3 guesses. \n\n"; //loop for 3 guesses for(x=1; x<4 && guess != randomnumber; x++) { //makes sure the user inputs a number between 1 and 10 do { cout << "Please enter a number between 1 and 10: "; cin >> guess; cout << "\n"; } while(!guess || guess > 10); if(guess > randomnumber) cout << guess << " is too high.\n\n"; else if(guess < randomnumber) cout << guess << " is too low.\n\n"; else cout << "Correct!\n\n"; } cout << "The number was " << randomnumber << "!\n\n"; //prompts user to end the game cout << "Enter any number to end the game: "; cin >> endgame; return 0;} ```NEWEST CODE:```// Guess the number game II// 11/01/11// Pete Murray#include #include #include using namespace std;int main(){ int randomnumber,guess,x,i; //generated number, user's guess, loop counters char endchar; //so user can end game //infinite loop for(;;) { //generates random number srand(time(0)); randomnumber = rand()%10+1; //prompts user to guess cout << "I am thinking of a number between 1 and 10, can you guess what it is? \n" << "You have 3 guesses. \n\n"; //loop for 3 guesses for(x=1; x<4 && guess != randomnumber; x++) { //makes sure the user inputs a number between 1 and 10 do { cout << "Please enter a number between 1 and 10: "; cin >> guess; } while(!guess || guess > 10); if(guess > randomnumber) cout << guess << " is too high.\n\n"; else if(guess < randomnumber) cout << guess << " is too low.\n\n"; else cout << "Correct!\n\n"; } cout << "The number was " << randomnumber << "!\n\n"; //prompts the user to restart game cout << "Would you like to play again? (y/n): "; cin >> endchar; //"clears" screen for(i=1; i<100; i++) { cout << "\n"; } //if user chose to, exit program if(endchar == 'n') break; } return 0;} ```Edit:Now you can play as many games as you want!I'd still love a programmer's comments.Game is attached. Link to comment Share on other sites More sharing options...
JSchilli1 Posted January 11, 2011 Share Posted January 11, 2011 Why is your username Roberto if your real name is Pete? :/Cool game.-JS1 Link to comment Share on other sites More sharing options...
Chief Posted January 11, 2011 Share Posted January 11, 2011 Fun, got it on my 3rd try. 'twas 2! Link to comment Share on other sites More sharing options...
Drummerpete Posted January 11, 2011 Author Share Posted January 11, 2011 @JS1:> Why is your username Roberto if your real name is Pete? :/Because Robin.@Chief:> Fun, got it on my 3rd try. 'twas 2!Glad you liked it :3Edit:Updated so it tells you what the number was, even if you were wrong. Link to comment Share on other sites More sharing options...
Drummerpete Posted January 11, 2011 Author Share Posted January 11, 2011 Updated. Link to comment Share on other sites More sharing options...
Homicidal Monkey Posted January 11, 2011 Share Posted January 11, 2011 this is the best game on this site Link to comment Share on other sites More sharing options...
Drummerpete Posted January 11, 2011 Author Share Posted January 11, 2011 @Fabio:> this is the best game on this site♥ Link to comment Share on other sites More sharing options...
Robin Posted January 11, 2011 Share Posted January 11, 2011 Why would you post this? xD Link to comment Share on other sites More sharing options...
Drummerpete Posted January 11, 2011 Author Share Posted January 11, 2011 @Robin:> Why would you post this? xDI wanna know… amidoinitrite?Edit:I'm just trying to get the hang of ifs, do while, while, etc. Link to comment Share on other sites More sharing options...
Skillzalot Posted January 12, 2011 Share Posted January 12, 2011 Nice little game you got going here. Link to comment Share on other sites More sharing options...
Drummerpete Posted January 14, 2011 Author Share Posted January 14, 2011 This version accepts negative numbers. xDAlso loops infinitely if you input a letter. Link to comment Share on other sites More sharing options...
Recommended Posts
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