2. What Do C++ Programs Look Like?

A C++ program (i.e., the contents of a source code file) has the following general form or structure:

// Source code file comments

#include <iostream>

int main()
{
    Your code goes here

    return 0;
}

The parts of a C++ program are described below, then a small program is presented and dissected to provide a concrete example of the topics discussed. We will be adding to this structure as we cover future C++ topics.

Comments

Every source code file begins with information such as the name of the source code file, the name of the programmer who wrote the program, a date, and a description of the program. In a working environment this information is used to document the creator of a program and to provide an overview of the program. It will be used for the same purpose in this class.

The compiler, as you know, will be translating all of the steps in this file to machine language. However, the information at the top of a source code file describes the contents of the file but does not contain any steps for the CPU to execute. We therefore need to tell the compiler to ignore this information as it translates the file and not include it in the object file. There are two ways to do this:

  1. Begin a line with // (i.e., two forward slashes).

    The two slashes and everything on the line which follows them will not be translated to machine language by the compiler.

  2. Place the information to be ignored between /* and */ symbols.

    Everything between the starting /* and the ending */, including the symbols themselves, will be ignored by the compiler. If the start (/*) and end (*/) are placed on different lines, the information on all lines between the symbols will be ignored.

Text we add to a source code file but want the compiler to ignore are called comments. Comments are used solely to provide information to humans.

The comments placed at the top of a file are called “source code file comments” because they describe the file as a whole. It is also common to add a comment after the code on a line to describe the step being taken or to place a comment on a line of its own to describe the purpose of the code in the next section of the program.

In a working environment, an employer often sets standards for formatting source code file comments and for the use of comments in general. Commenting standards are also often established in programming classes.

#include <iostream>

The compiler needs to be told when a program is using tools from the C++ Standard Library. An include statement is added to a program to allow it to use library tools for such things as reading data typed by the user on a keyboard and for displaying data to the screen.

main()

Our pseudocode and flowchart designs begin and end with “Start” and “End”, respectively. Between these two words are the steps in our algorithm. What is the C++ equivalent?

PseudocodeFlowchartC++ Program
StartFlowchart symbol for a terminator (an oval), labeled 'Start'
int main()
{
EndFlowchart symbol for a terminator (an oval), labeled 'End'
    return 0;
}

Every C++ program has a function named main(). The figure to the rightbelow shows how the start and end of main() correspond to the beginning and end of a pseudocode or flowchart design. The steps between Start and End in our algorithm will be placed between main()’s opening and closing braces.

PseudocodeFlowchartC++ Program
StartFlowchart symbol for a terminator (an oval), labeled 'Start'
int main()
{
EndFlowchart symbol for a terminator (an oval), labeled 'End'
    return 0;
}

If main() is omitted, translation of the program will fail (i.e., the program will not compile).

Some Additional Points

C++ is case sensitive: Whether a letter is uppercase (e.g., ‘A’) or lowercase (e.g., ‘a’) matters. For example, main() is written using all lowercase letters. If it was typed, for example, Main() or MAIN() it would not be seen as the same word by the compiler and there would be translation problems.

Keywords: Many programming languages, including C++, reserve words for their own purposes. If a programmer attempts to use one of these words for his/her own purpose, the compiler will object. Examples of keywords we have seen so far are return and int. Keywords are also sometimes called reserved words.

Most C++ statements end in a semicolon: It is important to note where semicolons are and are not used. The location of semicolons is determined by C++’s syntax rules and if they are not used in the right locations, the compiler will again object.

A Sample Program

Now that we have a feel for the general structure of a C++ program, let’s take a look at an example. The figure below provides both a pseudocode and flowchart design for the program. Note that the program simply reads in an integer and displays it on the screen.

PseudocodeFlowchart
Start
Declare number As integer
Prompt user to enter a number
Read number
Display number
End
Flowchart corresponding to pseudocode

Here is the C++ program corresponding to the designs given in the figure:

// Program    : first.cpp
// Author     : A. Student
// Due Date   : September 9, 2025
// Description: This program reads in a number and
//              displays it on the screen afterwards.

#include <iostream>

int main()
{
    int number;  // stores the number entered by the user

    std::cout << "Enter a number: ";
    std::cin >> number;
    std::cout << "Your number is " << number << std::endl;

    return 0;
}

Comments

The comments in the example include four pieces of information: 1) source code file name, 2) programmer name, 3) due date, and 4) program description. Please see your instructor for your class’ standards.

int number; // stores the number entered by the user

When a program needs to store a value, a variable is required. The statement above is a C++ variable declaration requesting RAM be allocated for the storage of an integer. A comment describing the use of the variable follows the declaration. Additional information about variables is provided in the Variables section.

std::cout << "Enter a number: ";

When information needs to be displayed on the screen, std::cout (pronounced “standard see-out”) is used. Follow std::cout with the stream insertion operator (<<) and then the value to be displayed. If text is to be displayed, place the text in double quotes. End the statement with a semicolon. This cout statement is prompting the user to enter a number.

std::cin >> number;

A cin (pronounced “see-in”) statement is used to read data typed by the user into a variable. Follow std::cin with the stream extraction operator (>>) and then the variable into which the value is to be stored (in this case, number). A semicolon also needs to end a cin statement. This cin statement is reading in the value typed by the user and storing it into the number variable.

std::cout << "Your number is " << number << std::endl;

The second cout statement in the program displays the text “Your number is” and the value stored in the variable number by the cin statement. At the end of the statement a newline (std::endl) is used to move the cursor down to the next line on the screen. Note that a stream insertion operator is used between each value to be displayed.

Additional Points

Here is a sample program run:

Enter a number: 23
Your number is 23

The value entered by the user is bold. As a result of the newline in the second cout statement, the cursor would be left at the start of the line following “Your number is 23”.

Input and output will be described in greater detail in the Screen Output and Keyboard Input section.