Here is a simple C++ program containing some I/O (input/output) statements.
#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
Let's breakdown this program, and see what it all means.
#include <iostream>
and using namespace std;
inform the compiler of the location of the namespace,
and to include them into our program.
The first line is what's known as a preprocessor command.
The pound(#) sign followed by include begins every
preprocessor command. Any built-in or user-defined header files you
wish to use in your program must follow this same syntax.
int main()
is the start of the statements which will become the executable
part of our program.
The function name is main, and it returns a value that is of type
integer.
The function has no parameters, as indicated by the empty parentheses.
{ }
are marks which indicate the
beginning and end of a function. In this case, they enclose our main program.
int number;
sets "number" as a variable of type integer.
cout << "Please enter a number: ";
prints text or data on the screen.
Anything that is enclosed in quotation marks is to be printed.
cin >> number;
receives data from the keyboard
and puts it into the variable specified.
cout << "You entered: " << number <<
endl;
prints out both a literal string and a variable.
endl
- Specifies the end of the line indicator.
The next thing printed will begin on the next line.
return 0;
- this line is there because the return value
in our main program was designated as an integer.
// end program
- a single line comment.
This page has been accessed
times.
Last modified: Friday, 21-Aug-2020 15:28:13 CST
Copyright 2000 Department of Computer Science, University of Regina.