Your First C++ Lab

C++C++Beginner
Practice Now

Introduction

Welcome to LabEx! This is your first C++ lab, where you'll take your initial steps into the world of C++ programming. Don't worry if you're completely new to this - we'll guide you through every step.

In this lab, you'll learn how to:

  1. Write and run your first C++ program
  2. Use basic output in C++
  3. Work with a simple variable

These fundamental skills will start your journey into C++ programming. Let's begin!

Click the Continue button below to begin the lab.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL cpp(("`C++`")) -.-> cpp/IOandFileHandlingGroup(["`I/O and File Handling`"]) cpp(("`C++`")) -.-> cpp/SyntaxandStyleGroup(["`Syntax and Style`"]) cpp(("`C++`")) -.-> cpp/BasicsGroup(["`Basics`"]) cpp/IOandFileHandlingGroup -.-> cpp/output("`Output`") cpp/SyntaxandStyleGroup -.-> cpp/comments("`Comments`") cpp/BasicsGroup -.-> cpp/variables("`Variables`") cpp/BasicsGroup -.-> cpp/data_types("`Data Types`") cpp/BasicsGroup -.-> cpp/strings("`Strings`") cpp/SyntaxandStyleGroup -.-> cpp/code_formatting("`Code Formatting`") subgraph Lab Skills cpp/output -.-> lab-391803{{"`Your First C++ Lab`"}} cpp/comments -.-> lab-391803{{"`Your First C++ Lab`"}} cpp/variables -.-> lab-391803{{"`Your First C++ Lab`"}} cpp/data_types -.-> lab-391803{{"`Your First C++ Lab`"}} cpp/strings -.-> lab-391803{{"`Your First C++ Lab`"}} cpp/code_formatting -.-> lab-391803{{"`Your First C++ Lab`"}} end

Your First C++ Program

Let's start with the traditional first program for any programmer - "Hello, World!". This simple program will help you understand the basic structure of a C++ program.

First, we need to open our WebIDE. Look for the "WebIDE" button on your LabEx interface and click it. This will open a VS Code-like environment where we'll write our C++ code.

Once the WebIDE is open, follow these steps:

  1. In the left sidebar, you'll see a file explorer. Right-click in this area and select "New File".
    • If you don't see the option to create a new file, look for a "..." or "More actions" button in the file explorer.
  2. Name the file hello.cpp. The .cpp extension tells the IDE that this is a C++ file.
    • Make sure to include the .cpp extension. If you accidentally name it just hello, you can rename it by right-clicking on the file.
  3. Click on the new hello.cpp file to open it in the editor.
alt text

Now, let's write our first C++ program. Copy and paste the following code into your hello.cpp file:

#include <iostream>

int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

Let's break down this code:

  • #include <iostream>: This line tells the computer to use a pre-written code that allows us to input and output information. Think of it like importing a tool we need to use.
  • int main() { ... }: This is where our program starts. Everything inside the curly braces { } is what our program will do. Every C++ program needs a main function.
  • std::cout << "Hello, World!" << std::endl;: This line tells the computer to print "Hello, World!" to the screen.
    • std::cout is like a funnel that sends text to the screen.
    • The << symbols are like arrows pointing what we want to send into the funnel.
    • std::endl moves to a new line after printing, like pressing Enter on your keyboard.
  • return 0;: This tells the computer our program has finished successfully. It's like saying "We're done here!"

To run this program:

  1. Open the terminal in your WebIDE (usually at the bottom of the screen).
    • If you don't see the terminal, look for a menu option like "Terminal" -> "New Terminal" or a terminal icon.
  2. In the terminal, type the following commands:
g++ hello.cpp -o hello
./hello
alt text

The first command (g++ hello.cpp -o hello) prepares your program to run. It's called "compiling" and it translates your C++ code into a language the computer can understand. The -o hello part names the resulting program "hello".

The second command (./hello) actually runs your program. The ./ tells the computer to look in the current folder for the program.

You should see "Hello, World!" printed in the terminal. If you don't, don't worry! Check these common issues:

  • Make sure you saved your hello.cpp file after pasting the code.
  • Check that you typed the commands exactly as shown, including the ./ before hello.
  • If you see any error messages, read them carefully. They often give clues about what went wrong.

Congratulations! You've just written and run your first C++ program.

Adding More Output

Now that we've got our first program running, let's expand it a bit. We'll modify our program to print multiple lines. This will show you how easy it is to add more output to your programs.

Open your hello.cpp file in the WebIDE. You can do this by clicking on the file name in the file explorer on the left side of the screen.

Replace the contents of hello.cpp with this new code:

#include <iostream>

int main() {
    std::cout << "Hello, World!" << std::endl;
    std::cout << "Welcome to C++ programming!" << std::endl;
    std::cout << "This is your first C++ lab." << std::endl;
    return 0;
}

This program uses std::cout three times to print three different lines. Let's break down what's new:

  • We've added two more std::cout lines, each followed by << std::endl;.
  • Each std::cout line ends with std::endl;, which moves to a new line. This is why each message will appear on a separate line when we run the program.
  • Notice how each line ends with a semicolon (;). In C++, we use semicolons to show where each instruction ends.

Compile and run the program using the same commands as before:

g++ hello.cpp -o hello
./hello
alt text

You should now see three lines of text printed in the terminal. If you don't see all three lines, here are some things to check:

  • Make sure you saved the file after making changes.
  • Check that each line ends with << std::endl;.
  • Look for any missing semicolons at the end of the std::cout lines.

This demonstrates how you can use multiple std::cout statements to print several lines of text. You can add as many lines as you want using this method!

Using a Simple Variable

For our final step, let's introduce the concept of variables. A variable is like a labeled box where we can store information. In this case, we'll use a variable to store a name, which we'll then use in our greeting.

Create a new file in your WebIDE and name it greeting.cpp. Here's how:

  1. Right-click in the file explorer (left sidebar) and select "New File".
  2. Name the new file greeting.cpp.
  3. Click on greeting.cpp to open it in the editor.

Then, copy and paste the following code:

#include <iostream>
#include <string>

int main() {
    std::string name = "LabEx";
    std::cout << "Hello, " << name << "!" << std::endl;
    std::cout << "Welcome to C++ programming, " << name << "!" << std::endl;
    return 0;
}

This program introduces a few new concepts:

  • #include <string>: This allows us to use text variables. Think of it as importing another tool, this time for working with text.
  • std::string name = "LabEx";: This creates a variable called name and stores the text "LabEx" in it.
    • std::string tells C++ that name will hold text.
    • You can change "LabEx" to any name you like!
  • We use the name variable in our cout statements to personalize our messages.
    • Notice how we use << name << to insert the value of name into our output.

Compile and run the program:

g++ greeting.cpp -o greeting
./greeting
alt text

You should see personalized greeting messages in the terminal. If you changed "LabEx" to a different name, you'll see that name in the output.

If you're not seeing the expected output, here are some things to check:

  • Make sure you've saved the greeting.cpp file after pasting the code.
  • Check that you've typed the compilation command correctly: g++ greeting.cpp -o greeting (note that we're using greeting.cpp and greeting here, not hello.cpp and hello).
  • Ensure that the name variable is defined before it's used in the cout statements.

Try changing the name in the name = "LabEx"; line to your own name, then compile and run the program again. You'll see how variables allow us to easily change parts of our program!

Summary

Congratulations! You've completed your first C++ Lab and taken your first steps into the world of C++ programming. Let's recap what you've learned:

  1. How to write and run a basic C++ program

    • You learned about the structure of a C++ program, including the main() function.
    • You used g++ to compile your code and ./ to run the resulting program.
  2. How to use cout to display output

    • You used std::cout to print messages to the screen.
    • You learned about std::endl to move to a new line after printing.
  3. How to use multiple cout statements to print several lines

    • You saw how easy it is to add more output to your program.
  4. How to use a simple string variable to store and display text

    • You learned how to create a string variable and give it a value.
    • You saw how to use a variable in your cout statements to personalize output.

These are the building blocks of C++ programming. You've made an excellent start!

Remember, learning to program is a journey, and you've just taken your first steps. Here are some tips as you continue learning:

  • Don't be afraid to experiment! Try changing the text in your cout statements or the value of your name variable.
  • If you encounter errors, read the error messages carefully. They often provide clues about what went wrong.
  • Practice is key. Try writing small programs that use what you've learned here.

Your next adventure in C++ is just around the corner. Keep coding, and have fun exploring the world of C++!

Other C++ Tutorials you may like