Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

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

  1. Comment section:

    • Chapter and , Lab Number, Programming Language

    • Your Name

    • Created/Modified date

    • Purpose of program

Code Block
/*
    Chapter and Lab Number: Module 1, Lab 2, C++
    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

    Code Block
    languagecpp
    #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.

    Code Block
    languagecpp
    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)

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

Code Block
languagecpp
//  Demo1Module 1 Demo
//  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

...

Let's break down the code piece by piece:

Code Block
languagecpp
// Demo1 Module 1 Demo, C++
// 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 as well as the language the file is written in (it is also acceptable to use the file name and extension - Example main.cpp)

Code Block
languagecpp
#include <iostream> // required library

...