Cat's Guide to C++ | Output and Input in C++ (with Visual Studio Code) | Level 0
This tutorial will focus on a C++ project with Visual Studio Code, printing to the standard output using std::cout, and taking input from the standard input device using std::cin.
My Development Environment For This Tutorial
Windows 10
Visual Studio Code with C/C++ Extension from Microsoft(ms-vscode.cpptools) and Code Runner (formulahendry.code-runner)
Note
Read this article from Microsoft to learn how to prepare your Visual Studio Code for development in C++.
Starting a New C++ File with Visual Studio Code
Clicking ‘File > Save As…’ will bring up a pop-up window. In this window, you can choose a location on your computer to save the file. You can also name the file and set the type of the file.
For the file name, write ‘user_profile.’
To set the file type, click on the dropdown menu next to ‘Save as type’. Find and select ‘C++ (*.cpp;*.cc;*.cxx;*.c++;*.hpp;*.hh;*.hxx;*.h++;*.h;*.ii).’
Once you have named the file and selected C++ as your file type, click ‘Save.’
Now, if you look at the file that you created and opened with Visual Studio Code, it will show you the file name (user_profile.cpp) and the location of the file (c: > Users > BatGirl > Documents > Code > C++ > user_profile > code > user_profile.cpp)
Include Directive
The first couple lines of your C++ program will be include directives, which will include the contents of another file.
In line 1, write ‘#include <iostream>.’ This include directive includes a file named iostream in your program. You can utilize the functionalities of the iostream file in your program by including this file.
You will use objects in the iostream file later when you print to the console and take input from it.
In line 2, write ‘#include <string>’ to include a string file in your program. With this include directive, you can store a string in a variable.
A string is a series of characters, such as ‘fantasy.'
A variable is a place in a computer memory where you can store data.
Once you have written two include directives, it is time to write the main function. The main function is a starting point for a C++ program.
A function is a group of code that performs an action and returns a value. A function name must be preceded by its return type and followed by a pair of parentheses.
In line 4, write ‘int main().’ ‘int’ means integers, and it is the return type of the main().
Declaring a String Type Variable
In main(), you need a string type variable to store the name of the user. When creating a variable in C++, you need to specify the type and the name of the variable. Creating a variable is also called declaring a variable.
In line 6, write ‘std::string user_name;.’‘std::string’ means a string object in the standard library. With this object, you can create a string-type variable.
‘::’ is called a scope resolution operator, and it can access objects in another scope. In line 6, '::' is used to access a string object in 'std,' where C++ standard library is stored.
A string object is a part of the string file, a subset of the standard library. It is because of line 6 you wrote ‘#include <string>’ in line 2.
‘user_name’ is the name of the variable.
Note the semicolon ‘;’ at the end of line 6. In C++, a semicolon marks the end of an instruction, often referred to as a statement.
Make line 6 indented. Code within a function in C++ is usually indented to show that it is part of the function.
Printing to the Console
With the new string type user_name variable, you are ready to ask for the user’s name. This tutorial will print a question to the console using the cout object from the iostream file.
In line 8, write ‘std::cout << “What is your name?”;’
‘cout’ is an object in the standard library, accessible by writing ‘std::cout’. It is also a part of the iostream file, which is part of the standard library. ‘cout’ can print to the standard output stream, which usually means the console.
‘<<’ is an output operator, and it can send the data to its right to the object to its left. In line 8, the output operator sends the string object ‘“What is your name?”’ to ‘std::cout’.
A string object must be encased in double quotation marks for C++ to recognize it as a string object.
If you try running your C++ program now, it will print ‘What is your name?’ to your console. To run your program, right-click in the space you are writing, and click ‘Run Code’ in the pop-up menu.
If you do not see an option for ‘Run Code’, you may not have Code Runner (formulahendry.code-runner) installed in your Visual Studio Code.
If all went well, you should see something like the image below in your output window. Make sure you are looking at your program’s output by checking that the dropdown menu at the top right corner of the window says ‘Code’.
Taking an Input
Now that you have asked the user a question, you should get input from the user. To accept the input, you will use the cin object from the iostream file, which is part of the standard library. To store the input, send it to the user_name variable you created earlier.
In line 9, write ‘std::cin >> user_name;’
‘std::cin’ is the cin object from the standard library. This object can take inputs from the standard input device, which is usually the keyboard. ‘>>’ is an extraction operator, and it can send the data to its left to the object to its right. In line 9, the extraction operator takes the input from ‘std::cin’ and sends it to ‘user_name.’
If you run your program now, the output window will wait for the user’s input. However, the output window is read-only; it will not take any input. If you try to write in the output window, Visual Studio Code may show you a pop-up message saying ‘Cannot edit in read-only editor.’
Right-click in your output window to bring up the pop-up menu, and click on the ‘Stop Code Run’ to stop running your program. Otherwise, your program will continue to wait for the user input.
To properly receive input from the user, you want to run your program in the terminal. You can run your code in Visual Studio Code’s terminal by customizing your Visual Studio Code’s settings. Click on ‘File > Preferences > Settings’ to see the list of Visual Studio Code settings.
In the list of settings, find a category named ‘Extensions.’
Expand the ‘Extensions’ category, and find a subcategory named ‘Run Code configuration.’
In ‘Run Code configuration,’ find the toggle for ‘Run In Terminal’ and check the box.
Return to your code and try pressing ‘Run Code.’ Your program should now run in your Visual Studio Code’s terminal.
Try typing your name in the terminal, and pressing enter.
Displaying the Input
The variable named user_name can print its content through the cout object. Let’s confirm the user’s input by printing the user’s name.
In line 10, write ‘std::cout << “User Name: ” << user_name;’. This line will print ‘User Name: ’ first, then print the data stored in user_name. By using two output operators, you can print two different data at once.
Right-click on your code area, and press ‘Run Code.’
Go to your terminal in Visual Studio Code, and enter your name. Your program should now confirm the user’s name.
Comments
Post a Comment