How to Create a Number Guessing Game in C
Start by opening Microsoft Visual Studios, or any other coding program that supports C++., Select "New Project" on the left side of the screen to generate this window. , Click the "Visual C++ drop-down menu and select the "Win32 Console...
Step-by-Step Guide
-
Step 1: Start by opening Microsoft Visual Studios
This tutorial will focus on Microsoft Visual Studios Professional 2013, but the code will work on any program in any edition. -
Step 2: or any other coding program that supports C++.
, Then press OK. ,,,,,, int main(void) { srand(time(NULL)); // To not have the same numbers over and over again. while(true) { // Main loop. // Initialize and allocate. int number = rand() % 99 + 2; // System number is stored in here. int guess; // User guess is stored in here. int tries = 0; // Number of tries is stored here. char answer; // User answer to question is stored here. //std::cout << number << "\n"; // Was used for debug... while(true) { // Get user number loop. // Get number. std::cout << "Enter a number between 1 and 100 (" << 20
- tries << " tries left): "; std::cin >> guess; std::cin.ignore(); // Check is tries are taken up. if(tries >= 20) { break; } // Check number. if(guess > number) { std::cout << "Too high! Try again.\n"; } else if(guess < number) { std::cout << "Too low! Try again.\n"; } else { break; } // If not number, increment tries. tries++; } , // Check for tries. if(tries >= 20) { std::cout << "You ran out of tries!\n\n"; } else { // Or, user won. std::cout<<"Congratulations!! " << std::endl; std::cout<<"You got the right number in " << tries << " tries!\n"; } while(true) { // Loop to ask user is he/she would like to play again. // Get user response. std::cout << "Would you like to play again (Y/N)? "; std::cin >> answer; std::cin.ignore(); // Check if proper response. if(answer == 'n' || answer == 'N' || answer == 'y' || answer == 'Y') { break; } else { std::cout << "Please enter \'Y\' or \'N\'...\n"; } } // Check user's input and run again or exit; if(answer == 'n' || answer == 'N') { std::cout << "Thank you for playing!"; break; } else { std::cout << "\n\n\n"; } } // Safely exit. std::cout << "\n\nEnter anything to exit. . . "; std::cin.ignore(); return 0; } ,,, -
Step 3: Select "New Project" on the left side of the screen to generate this window.
-
Step 4: Click the "Visual C++ drop-down menu and select the "Win32 Console Application".
-
Step 5: Press Next to generate this window.
-
Step 6: Under "Additional Options"
-
Step 7: select the "Empty Project" box
-
Step 8: then press finish.
-
Step 9: Press Ctrl+⇧ Shift+A to start a new project once you've reached this screen.
-
Step 10: Click the Visual C++ drop-down on the left side
-
Step 11: select the C++ File
-
Step 12: and press add.
-
Step 13: Type: #include <iostream> #include <cstdlib> #include <ctime> These establish the "libraries" from which actions are stored in the code itself.
-
Step 14: Type the code for generating the number
-
Step 15: and generating 20 tries for the user to guess the code correctly.
-
Step 16: Finish off the code by adding functions that tell the user if they won or lost
-
Step 17: want to play again
-
Step 18: or want to exit the game.
-
Step 19: Press Ctrl+F5 to run your number guessing program.
-
Step 20: Select File--> Save as--> and name your code
-
Step 21: Open the File by clicking on the name of your program
-
Step 22: then opening the source code.
Detailed Guide
This tutorial will focus on Microsoft Visual Studios Professional 2013, but the code will work on any program in any edition.
, Then press OK. ,,,,,, int main(void) { srand(time(NULL)); // To not have the same numbers over and over again. while(true) { // Main loop. // Initialize and allocate. int number = rand() % 99 + 2; // System number is stored in here. int guess; // User guess is stored in here. int tries = 0; // Number of tries is stored here. char answer; // User answer to question is stored here. //std::cout << number << "\n"; // Was used for debug... while(true) { // Get user number loop. // Get number. std::cout << "Enter a number between 1 and 100 (" << 20
- tries << " tries left): "; std::cin >> guess; std::cin.ignore(); // Check is tries are taken up. if(tries >= 20) { break; } // Check number. if(guess > number) { std::cout << "Too high! Try again.\n"; } else if(guess < number) { std::cout << "Too low! Try again.\n"; } else { break; } // If not number, increment tries. tries++; } , // Check for tries. if(tries >= 20) { std::cout << "You ran out of tries!\n\n"; } else { // Or, user won. std::cout<<"Congratulations!! " << std::endl; std::cout<<"You got the right number in " << tries << " tries!\n"; } while(true) { // Loop to ask user is he/she would like to play again. // Get user response. std::cout << "Would you like to play again (Y/N)? "; std::cin >> answer; std::cin.ignore(); // Check if proper response. if(answer == 'n' || answer == 'N' || answer == 'y' || answer == 'Y') { break; } else { std::cout << "Please enter \'Y\' or \'N\'...\n"; } } // Check user's input and run again or exit; if(answer == 'n' || answer == 'N') { std::cout << "Thank you for playing!"; break; } else { std::cout << "\n\n\n"; } } // Safely exit. std::cout << "\n\nEnter anything to exit. . . "; std::cin.ignore(); return 0; } ,,,
About the Author
Jeffrey Murray
Creates helpful guides on lifestyle to inspire and educate readers.
Rate This Guide
How helpful was this guide? Click to rate: