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 fileiostream.- 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.
- The first line,
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.
- 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
intto represent that it can only contain integers.- We will learn more about data types in a later section.
-
The variable name
myNumbersimply 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
cincommand.- 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
weightvariable.- This is why the
>>is what’s called an extraction operator.
- This is why the
-
Often when we use
cinwe want to display something first usingcoutto notify the user to insert theweightfirst.
#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
coutworks 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 variable
sumto assign the sum ofnum1andnum2.
- We can use the variable
#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 variablenum1andnum2.
- When we process data like this, we often assign values to a variable