C++ Program Structure and Basic I/O

There are two basic parts to a C++ program:
1. Instructions to the C++ preprocessor and compiler;
2. Instructions that describe the processing to be done.
However, before we can describe these instructions, we must have a way of naming things so that we can tell the compiler about them and describe what we want to have done to them. We name things (data types, data objects, and actions) by giving them an identifier. An identifier is made up of letters, numbers, and underscores, but must begin with a letter or an underscore. We use the words identifier and name interchangeably.

Beware: C++ is case sensitive. That means that Value, VALUE, value, and a vaLue could be four different identifiers.

C++ Program Structure

Let's examine the following C++ program. We have numbered the lines so that we can discuss them easily.

1	/******************************************************
2	*
3	*	FileName: sampleio.cpp
4	*	Author:	  Ada Lovelace
5	*	Purpose:  Demonstrate Basic I/O operation 
6	*		  by inputting a unmber and outputting 
7	*		  the number just entered.
8	*
9	******************************************************/
10	#include <iostream>
11	using namespace std;
12	int main()
13	{
14	   int number;
15	   cout << "Please enter a number.";
16	   cin >> number;
17	   cout << "You entered: " << number << endl;
18	   return 0;
19	}
20	// end program

Semicolons terminate statements in the C++ program. There are 6 semicolons in the preceding program, so there are 6 statements in the program.

Here is a clean version of the above program for your convenience.

	*	FileName: sampleio.cpp
	*	Author:	  Ada Lovelace
	*	Purpose:  Demonstrate Basic I/O operation 
	*		  by inputting a unmber and outputting 
	*		  the number just entered.
	#include <iostream>
	using namespace std;
	int main()
	   int number;
	   cout << "Please enter a number.";
	   cin >> number;
	   cout << "You entered: " << number << endl;
	   return 0;
	// end program

Copyright: Department of Computer Science, University of Regina.