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...

22 Steps 1 min read Advanced

Step-by-Step Guide

  1. 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.
  2. 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; } ,,,
  3. Step 3: Select "New Project" on the left side of the screen to generate this window.

  4. Step 4: Click the "Visual C++ drop-down menu and select the "Win32 Console Application".

  5. Step 5: Press Next to generate this window.

  6. Step 6: Under "Additional Options"

  7. Step 7: select the "Empty Project" box

  8. Step 8: then press finish.

  9. Step 9: Press Ctrl+⇧ Shift+A to start a new project once you've reached this screen.

  10. Step 10: Click the Visual C++ drop-down on the left side

  11. Step 11: select the C++ File

  12. Step 12: and press add.

  13. Step 13: Type: #include <iostream> #include <cstdlib> #include <ctime> These establish the "libraries" from which actions are stored in the code itself.

  14. Step 14: Type the code for generating the number

  15. Step 15: and generating 20 tries for the user to guess the code correctly.

  16. Step 16: Finish off the code by adding functions that tell the user if they won or lost

  17. Step 17: want to play again

  18. Step 18: or want to exit the game.

  19. Step 19: Press Ctrl+F5 to run your number guessing program.

  20. Step 20: Select File--> Save as--> and name your code

  21. Step 21: Open the File by clicking on the name of your program

  22. 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

J

Jeffrey Murray

Creates helpful guides on lifestyle to inspire and educate readers.

124 articles
View all articles

Rate This Guide

--
Loading...
5
0
4
0
3
0
2
0
1
0

How helpful was this guide? Click to rate: