Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use C++ to realize the guessing game on the terminal

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "how to use C++ to achieve a guessing game on the terminal". The content is simple and clear. I hope it can help you solve your doubts. Next, let the editor lead you to study and learn the article "how to use C++ to realize the guessing game on the terminal".

Installation dependency

To keep up with this article, you need C++ and a compiler.

On Linux, you can get everything you need by installing Qt Creator IDE from your distribution repository.

On Fedora, CentOS, or RHEL:

$sudo dnf install qt-creator

On Debian, Ubuntu, Chromebook, or similar systems:

$sudo apt install qtcreator

Qt Creator IDE is not used in this article, but it is an easy way to install everything you need and is an essential tool for complex C++ projects, including those with GUI. On macOS or Windows, follow the installation instructions on the Qt website.

Set inclusion and namespaces

The core language of C++ is concise. Even a simple application needs to use additional libraries. This application uses iostream to get access to the cout and cin keywords.

Also, make sure that the program uses the std namespace:

# include using namespace std

This is not absolutely necessary, but if you do not set the namespace to std, all keywords from the iostream library need a namespace prefix. For example, I can't write cout, I want to write std::cout.

Sentences in C++ end with a semicolon.

Create a function

Every C++ application needs at least one function. The main function of a C++ application must be called main, and it must return an integer (int), which is in line with POSIX's expectation that a process returns 0 on success and other values on failure. You can create a new function by providing it with a return type and name.

Int main () {/ / code goes here} implements program logic

The game code must first generate a random number for players to guess. In C++, you can do this by creating a seed for generating pseudo-random numbers. A simple seed is the current time. Once you have the seed, you can get a number between 1 and 100. A random number from 0 to 99 is generated by calling the rand function and setting an upper limit of 100, so whatever number you choose is incremented by 1 and the result is assigned to a variable named number. You must also declare a variable to hold the player's guess. For clarity, I call this variable guess.

The sample code also includes a debug statement that tells you what a random number is. This is not very good for guessing games, but it makes testing much faster. Later, you can delete this line or comment it with / / directly before the line:

Srand (time (NULL)); int number = rand ()% 100% 1; int guess = 0; cout number) {cout

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report