Code Structure

  • Basic sequential code structure in C++ looks like this:
flowchart TD
	start(["Start"])
	init{{"Initialization\nDeclaration"}}
	input[/"Input"/]
	process["Process"]
	output[/"Output"/]
	stop(["End"])
	
	start --> init
	init --> input
	input --> process
	process --> output
	output --> stop
  • When making our C++ code, we often use this structure to build on our code.

1. Starting the Program

  • When starting the program, we set up the necessary files required in processing code.
  • This include setting up the necessary compiler directives or global variables.
  • To begin, we can simply start our code as:
#include <iostream>
using namespace std;
 
int main () {
 
}
  • Here:
    • The first line, #include <iostream> asks the compiler to include the header file iostream.
      • This header file contains code that allows certain commands to work.
      • More specifically, it should allow us to manage input/output streams.
    • The second line using namespace std;, simplifies writing commands.
      • We’ll explain this later in Setting User Inputs or Displaying Outputs.
    • Lines 3-5 is what’s called the main() function.
      • Any line of code inside it will be executed first.

2. Declaring and Initializing Variables

  • When we do code, we often need names to represent a specific value or line of text.
  • This is what variables are for.
    • They serve as identifiers if we want to represent the same value throughout the program.
  • To initialize, we simply state the data type and the name of the variable inside the main() function.
#include <iostream>
using namespace std;
 
int main () {
	int myNumber; //Declaring the variable myNumber
}
  • For example, we use the data type int to represent that it can only contain integers.

    • We will learn more about data types in a later section.
  • The variable name myNumber simply assigns the name to be used throughout the program.


  • This process of defining a variable is called declaration.

    • When declaring variables, we are simply letting the computer know about it’s existence.
  • We can also assign an initial value for our variable.

    • In this case, if we assign a value to a variable, it keeps that value when we use that variable we assigned it to throughout the program.
#include <iostream>
using namespace std;
 
int main () {
	int myNumber; //Declaring the variable myNumber
	myNumber = 25; //Initialization
}
  • This process of assigning an initial value is called initialization.
    • We can define then initialize such as the one above.
    • We can also define and initialize a variable in a single line.
#include <iostream>
using namespace std;
 
int main () {
	int myNumber = 25; //Declaration + Initialization 
}
  • We can also define and initialize multiple variables at once.
    • To do this, we separate each declaration with a comma.
    • If we do this, all variables will have the same data type too.
#include <iostream>
using namespace std;
 
int main () {
	int weight = 0, height = 0, bmi = 0;
}

3. Setting up User Inputs

  • When managing user inputs, we use the cin command.

    • This pauses code execution and waits for the user’s input.
  • For example, in this line of code:

#include <iostream>
using namespace std;
 
int main () {
	int weight = 0, height = 0, bmi = 0;
	cin >> weight;
}
  • Anything that the user inputs in the terminal will be extracted then assigned to the weight variable.

    • This is why the >> is what’s called an extraction operator.
  • Often when we use cin we want to display something first using cout to notify the user to insert the weight first.

#include <iostream>
using namespace std;
 
int main () {
	int weight = 0, height = 0, bmi = 0;
	cout << "Please enter the weight: ";
	cin >> weight;
}
  • We will discuss how cout works in a moment.

4. Processing Inputs

  • Suppose that in the code below, the user inputs two numbers.
#include <iostream>
using namespace std;
 
int main () {
	int num1 = 0, num2 = 0, sum = 0;
	cin >> num1;
	cin >> num2;
}
  • We want to add those two numbers that the user inputs.
    • We can use the variablesum to assign the sum of num1 and num2.
#include <iostream>
using namespace std;
 
int main () {
	int num1 = 0, num2 = 0, sum = 0;
	cin >> num1;
	cin >> num2;
	
	sum = num1 + num2;
}
  • In the line, sum = num1 + num2, we assign the result of adding whatever is in the variable num1 and num2.
  • When we process data like this, we often assign values to a variable