Introduction to C / C++ Programming
Basic Format

Basic Format:

	1	/* Filename.cpp
	2
3 This program was written by Ima Programmer on some date 4 5 This program does something useful . . . 6 7 */ 8 9 #include <iostream> // For input and output functions 10 11 using namespace std; // For easy reference to cin, cout, and cerr 12 13 int main( void ) { 14 15 // Declare variables to be used 16 17 // Explain to the user what the program does 18 19 // Get user input ( and check it if possible ) 20 11 // Perform necessary calculations 22 23 // Report results ( in a complete and well-formed format. ) 24 25 system( "pause" ); // Only needed with certain environments, e.g. Dev C++ 26 27 return 0; 28 29 } // main

Explanation of Specific Lines

  1. The symbols /* and */ enclose a block comment, which may extend over any number of lines, or which may enclose only part of a line.
  2. The #include statement informs the compiler of what libraries are needed.
  3. Namespaces are complicated issues beyond the scope of this simple introduction. For now just trust that it makes input and output easier to use, and always put it in.
  4. Line 13 says that we are starting a function named main, which is the starting point for all C/C++ programs.
  5. Some IDEs ( Integrated Development Environments ) execute programs in a separate window, and then immediately close the window when the program is completed. In this case, the system( pause ) statement holds the window open so that the results can be seen, until the user presses a key to release it. ( Dev C++ is one of those IDEs. ) Note that CodeLab does not permit the use of system( pause ), so make sure to leave it out when submitting CodeLab solutions.
  6. Every function must have a return statement, which causes the function to end and return control to whoever called it.
  7. It is good practice to comment the closing brace of every function, and any other closing brace that is more than a few lines away from its matching opening brace. This makes it much easier to read more complex programs, which may have MANY sets of nested and consecutive braced blocks.

A Simple Sample:

	1	/* addTwoAndSine.cpp
	2
	3	   This program was written by John Bell on August 2009 for CS 109
	3A	   Modified August 2011 to include use of the math library to calculate a sine.
	4
	5	   This program asks the user for two floating point numbers and reports their total.
	6
	7	*/
	8
	9	#include <iostream>		// For input and output functions
	9A	#include <cmath>		// For use of the math library functions
	10
	11	using namespace std;	// For easy reference to cin, cout, and cerr
	12
	13	int main( void ) {
	14
	15		// Declare variables to be used
	16
	17		double num1(0.0), num2(0.0), total = 0.0;
	18
	19		// Explain to the user what the program does
	20
	21		cout << "This program adds together two floating point numbers.\n";
	21A		cout << "Written August 2009 by John Bell for CS 109.\n\n";
	22
	23		// Get user input ( and check it if possible )
	24
	25		cout << "Please enter the first number > ";
	26		cin >> num1;
	27
	28		cout << "Please enter the second number > ";
	29		cin >> num2;
	30
	31		// Perform necessary calculations
	32	
	33		total = num1 + num2;
	34
	35		// Report results ( in a complete and well-formed format. )
	36
	37		cout << "\nThe total of " << num1 << " and " << num2
	38			<< " is " << total << endl;
	39	
	40		cout << "\nThe sine of " << total << " is " << sin( total ) << endl;
	41
	42		return 0;
	43
	44	} // main

Explanation of Specific Lines

  1. The keyword "double" indicates double-precision floating point numbers, the most commonly used type of real numbers in C/C++.
  2. The keyword cout represents the screen, and is used for output.
  3. The keyword cin represents the keyboard, and is used for reading in input.
  4. The equals symbol, =, as used here is an assignment, which takes the value from the right side and stores it in the left side.
  5. Note that the output of the results is a complete sentence, and includes an echoing back of the user's input.
  6. It is not necessary to store all results in variables before reporting them.

Another Example

Exercises