13.6 C
Jaipur
Sunday, January 23, 2022

Beginner C++ Projects

Must read

Students might find it challenging to get started with long and expert-level projects when learning a new language. Students used to derive help from books, online programming tutorials, and guides. However, these sources are not enough when you want to become an expert at a certain language. Hence, they try to search for projects which are less complicated, short, and simple to accomplish within the initial stage of practice. Within this guide, we will let you know about some C++ beginner projects along with their codes that are short and easy. Now, let’s start with Ubuntu 20.04 system.

Project 01: Calculator

A beginner can start their first project by creating a simple calculator application in C++. For this, you need to create a simple c++ file in your shell with the “touch” command of Ubuntu. To open it, you have to use a built-in editor, such as GNU Nano, Vim, or Text editor:

Students might find it challenging to get started with long and expert-level projects when learning a new language. Students used to derive help from books, online programming tutorials, and guides. However, these sources are not enough when you want to become an expert at a certain language. Hence, they try to search for projects which are less complicated, short, and simple to accomplish within the initial stage of practice. Within this guide, we will let you know about some C++ beginner projects along with their codes that are short and easy. Now, let’s start with Ubuntu 20.04 system.

Project 01: Calculator

A beginner can start their first project by creating a simple calculator application in C++. For this, you need to create a simple c++ file in your shell with the “touch” command of Ubuntu. To open it, you have to use a built-in editor, such as GNU Nano, Vim, or Text editor:

Then, the empty file will be opened in the editor. We started the code of the application by adding a simple “iostream” header file followed by the namespaced “std”. After this, the code starts from the main() function. The main() function has 1 character type variable, “op” stands for operator, and two float type variables “n1” and “n2”. The first cout statement is asking a user to add the operator as input. The cin statement is saving it in the variable “op”. The second cout statement is asking the numbers to be used further from the user as input. The cin statement is again utilized to save the numbers added by a user in the variables “n1” and “n2”:

The switch statement is started. It is using the “op” variable as a case value. Within the switch statement, different cases have been defined for each operator, i.e., +, -, *, and /. The cout statement after each case works according to that operator entered by the user. The calculated value will be displayed on the shell with the help of an operator and its operands in the cout statement shown in the attached image. The overall code is provided below:

After saving the file with Ctrl+S, we have compiled it using the “g++” compiler of Ubuntu 20.04. This was successful, and we have executed the file with the “./a.out” command. Within the first time of execution, we have used the * operator to multiply two integer values. During the second time, we used “/” to divide, “-” to subtract, and “+” to sum the values. Here, your calculator works in C++:

Project 2: Login System

So, in the second project you can try creating in C++ is a simple login system or panel on the Ubuntu 20.04 system. So, let’s see a simple example of it. We have created a new login.cc file, and to edit it in the editor, we have used the Nano editor:

The code started from the mainstream header “iostream” and the standard “std” namespace. Within the main function, two string-type variables, “username” and “Password”, are declared. The integer variable “Attempt” has been initialized to 0. The “while” statement is utilized to let a user add their username and password provided the attempts are less than 3. The cout and cin statements are used to get the input from users and save it to the variables. The if-else statements are used here to match the username and password entered by the user with the attempt already in the database or mentioned in the code.

Upon a match, the cout statements will show a welcome message to the user. Otherwise, it will display that this attempt was invalid. The “Attempt” variable will be incremented each time when a user puts the wrong username or password. When the invalid attempts reach “3”, it will quit the program, showing the message that you have done too many login attempts. However, if the attempt is successful, it will display a simple message to thank the user:

Let’s just save and compile this newly made code first. The same g++ and “./a.out” command can be utilized here. Within the first attempt, we have added the wrong username and password. In return, we received an invalid login attempt alert message. While in the second attempt, we have added the correct username and password that are matched with the record and received the welcome and thanks message on the screen:

Project 03: Tic-Tac-Toe Game

Another simple but not a brief project a student can create while in the beginning stage of learning C++ can be a tic-tac-toe game. Let’s look at the code to create a game. Create a new file and open it in the editor, as we have done earlier:

So, we have started this code again with some header files ad initialized a character type array having 10 characters in it. Two functions, “Win()” and “board()”, are prototypes at the start. The main() function has been started within initializing an integer type variable player with value 1. The integer variables “I” and “choice” are also declared. The character type variable “mark” will be used to mark the square by a player. The do statement has been called the “board” function to show the initial board of the game. It will ask a first player to enter a number you want to choose from a square array, i.e., 0 to 9. The mark “X” is for player 1 and “O” for player 2:

Whatever the number can be added by player 1 or player 2, that square will be replaced by the specific player marks, i.e., “X” or “O”. The if-else statement has been utilized here for this purpose. If a user chooses anything other than 1 to 9, it will show you an alert of an invalid move. The player variable will be decremented and “cin” added by this specific user will be ignored. The cin will be refreshed again with the “get()” function:

The variable “I” will be using the value, i.e., Boolean (true or false) stored from the function “win()”. The player has been incremented. While the value of “I” is “-1” or false, the board() will be called again. If the value of “I” is true or “1”, it will display which player is the winner; otherwise, the match will be a draw:

Here, the “Win()” function is checking if three of the squares are placed at consecutive positions or in diagonal, i.e., horizontal and vertical, it will return true otherwise false:

Here, the board function is showing simple outputting of the board pattern in the shell:

Here comes the illustration of playing a whole game in the terminal:

Conclusion:

We have discussed several beginner projects for C++ users. You can also create a reservation system, library system, registration system, online banking system, online selling purchasing system or an eCommerce system, and many more options. These are simple suggestions one can utilize during their early days of practice. You can achieve a good level of understanding C++ by simply practicing such projects. We hope, you like this article. Check out Linux Hint for more tips and information.

Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisement -

Latest article