How to Use cout and endl in C++

In this tutorial, we will explore how to use cout and endl in C++ for outputting information in robotic projects. These elements are part of the iostream library and are essential for displaying messages and data in the console.

Prerequisites

Directions

Open a terminal window, and type this:

cd ~/Documents/cpp_tutorial && code .

Let’s create a new C++ file and name it robot_output.cpp.

Type the following code into the editor:

#include <iostream>

using namespace std;

int main() {
    string robot_status = "Active";
    int x_position = 10;
    int y_position = 20;

    cout << "Robot Status: " << robot_status << endl;
    cout << "Robot Position: (" << x_position << ", " << y_position << ")" << endl;

    cout << "Initializing robot..." << endl;
  
     // Simulating robot initialization
    cout << "Robot initialized successfully!" << endl;

    return 0;
}

In this code, we include the iostream header for input/output operations and use the using namespace std statement to bring the std namespace into the current scope.

Inside the main function, we declare variables for robot_status, x_position, and y_position, representing the status and position of the robot.

We use cout to output the robot’s status and position to the console.

 The << operator is used to concatenate strings and variables.

The endl manipulator is used to insert a newline character and flush the output buffer.

Next, we simulate the robot initialization process by outputting messages using cout and endl. 

Run the code.

1-robot-output

You should see the robot’s status, position, and initialization messages printed in the terminal.

That’s it. Thanks, and I’ll see you in the next tutorial.

Keep building!

How to Organize Your C++ Code with std Namespace

In this tutorial, we will explore how to use the std namespace in C++ for robotics. The std namespace helps organize your code and makes it more readable.

Prerequisites

Directions

Open a terminal window, and type this:

cd ~/Documents/cpp_tutorial && code .

Let’s create a new C++ file and name it simple_robot.cpp.

Type the following code into the editor:

#include <iostream>
#include <string>

using namespace std;

int main() {
    string robot_name = "AutomaticAddisonBot";
    int robot_battery = 80;

    cout << "Robot Name: " << robot_name << endl;
    cout << "Battery Level: " << robot_battery << "%" << endl;

    return 0;
}

In this code, we include the iostream and string headers for input/output operations and string manipulation, respectively.

We use the using namespace std; statement to bring the std namespace into the current scope, allowing us to use elements like cout, endl, and string without the std:: prefix.

Inside the main function, we declare variables for robot_name and robot_battery, representing the name and battery level of the robot.

We print the robot’s name and battery level using cout and endl without the std:: prefix since we are using the std namespace.

Run the code.

1-run-the-code

You should see the robot’s name and battery level printed in the terminal.

This example demonstrates how using the std namespace can make your code more concise and readable. 

Thanks, and I’ll see you in the next tutorial. 

Keep building!

How to Run C++ Code That Uses External Libraries

In this tutorial, we will explore how to use external libraries in C++ to enhance your robotics projects. External libraries provide pre-written code that can save you time and effort by offering ready-made solutions for common tasks.

Prerequisites

Directions

Open a terminal window, and type this:

cd ~/Documents/cpp_tutorial
code .

Now let’s install the Eigen library, a linear algebra library widely used in robotics for tasks such as matrix operations and transformations.

Open the integrated terminal in Visual Studio Code by going to ‘Terminal’ in the top menu and clicking on ‘New Terminal’. 

In the terminal, type the following command to install Eigen:

sudo apt install libeigen3-dev

Enter your password if prompted and wait for the installation to complete.

Now, let’s create a new C++ file and name it simple_eigen_example.cpp.

Type the following code into the editor:

#include <iostream>
#include <eigen3/Eigen/Dense>

int main() {
    Eigen::Vector3d v(1, 2, 3);
    std::cout << "v = " << v.transpose() << std::endl;
    return 0;
}

In this code, we include the necessary headers: iostream for input/output operations and Eigen/Dense for the Eigen library. 

Then, in the main function, we create a 3D vector using Eigen’s Vector3d class and initialize it with values (1, 2, 3).

Finally, we print the transposed vector using std::cout.

Run the code.

1-vector-in-the-terminal

You should see the transposed vector printed in the terminal.

We could have also compiled the code this way:

g++ -I /usr/include/eigen3 simple_eigen_example.cpp -o simple_eigen_example

The “-I” flag in g++ tells the compiler where to look for additional header files/includes.

After successful compilation, run the executable with:

./simple_eigen_example

You should see the same transposed vector output as before.

2-alternative-way-of-running

That’s it! Keep building!