Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 4 Next »

When you create your code - ensure you have the following:

  1. Comment section:

    • Chapter and Lab Number

    • Your Name

    • Created/Modified date

    • Purpose of program

/*
    Chapter and Lab Number: Module 1, Lab 2
    Author: John Doe
    Date: July 12, 2023
    Description: This file contains an example C++ program that demonstrates the usage of functions and arithmetic operations to calculate the sum of two numbers and display the result.
*/
  1. Preprocessor directives - These are statements that begin with a # symbol and are used to include header files

    #include <iostream>
  2. Namespace declaration - Namespaces are used to organize code and avoid naming conflicts between different libraries or modules. The using namespace directive allows you to use elements from a specified namespace without having to provide their fully-qualified names.

    using namespace std; // Allows you to use 'std' namespace elements without the 'std::' prefix
    
  3. The main function: This is the entry point of your C++ program. The execution of your program starts from the main function. It should have a return type of int (it can take command-line arguments)

    int main() {
        // Your program logic goes here
    
        return 0; // Indicates successful execution
    }

//  Demo1
//  Author: Sam Student
//  Created on 1-9-2023.
//  Demonstrate output and create new lines
//  Code uses \n and endl

#include <iostream> // required library
using namespace std;

int main()
{    
    //output to the console
    cout << "This is my first line" << endl;
    cout << "This is my second line\n";
    cout << "This is my third line" << endl;
  
    return 0;
} // end of main

Output

Code Breakdown:

Let's break down the code piece by piece:

// Demo1
// Author: Sam Student
// Created on 1-9-2023.
// Demonstrate output and create new lines
// Code uses \n and endl

These lines are comments in the code. In C++, comments that begin with // are single-line comments. They're not executed as part of the code, but they provide information or context to readers. Here, the comments are giving information about the code's purpose, the author, the creation date, and the specific features demonstrated.

#include <iostream> // required library

This line includes the iostream library, which provides facilities for input-output operations. In this code, it's used to output text to the console.

using namespace std;

This line tells the compiler to use the std (standard) namespace for the functions and objects used in this code. This is why we can use cout and endl without prefixing them with std::.

int main()

This is the declaration of the main function, which is the starting point for execution in a C++ program. Every C++ program needs a main function.

{    
    cout << "This is my first line" << endl; //output to the console

Here, we're using cout (short for "character output") to print a string to the console. The string is followed by endl, which stands for "end line". This will both print a new line and flush the output buffer, ensuring that the text is immediately displayed on the console.

    cout << "This is my second line\n";

Again, we're using cout to print a string. This time, instead of endl, the string itself contains a newline escape sequence (\n), which will move the cursor to the next line after printing.

    cout << "This is my third line" << endl;

Similar to the first line of output, we print a string followed by endl to create a new line.

    return 0;

This line signals the successful completion of the main function. In C++, returning 0 from main typically indicates that the program finished without any errors.

} // end of main

This closing brace marks the end of the main function's body. The comment simply provides additional clarity about what this brace corresponds to.

Each string is printed on a separate line, thanks to the use of endl and \n.

  • No labels