Handout 4 (Remarks)
#include <iostream>
using namespace std;
Sometimes we may need to include other stuff also. That depends on our needs.
int main()
If you declare the function main this way, then you must include the statement
return 0;
at the end of the function. Notice that returning 0 tells the computer that everything went ok. When that statement is executed control returns to the computer's operating system. If the return value is different than 0, then that means something went wrong. Notice that if you don't include the return statement, you do not get any error messages and you end up with the same desired output, because the compiler will assume that you wanted the function to be of type void. So, you should include the return statement.
Note: You can declare the function main to be of type void. In that case, there should be no return statement.
Notice that we usually prefer to write { and } on separate lines. It is not wrong to write other statements on the same line, but people don't do that.
cout << endl;
But you can also use
cout << "\n";
Or
cout<<'\n';
\n is the newline character in C++.
Notice also that << is called the insertion operator
cout << "Welcome to";
cout << endl;
cout << endl;
cout << "New York.";
is the same as
cout << "Welcome to" << endl << endl << "New York.";
and is the same as
cout << "Welcome to"
<< endl
<< endl
<< "New York.";
Notice that the read value must be stored in a variable (a variable is a location in memory refrenced by an identifier that contains a data value that can be changed). We'll explain what we mean by
The format of the InputStatement is
cin >>Variable >>Variable >>….;
For example, suppose you want to read two real values and store the results in two variables whose names are a and b. Then, you can do this
cin >> a >> b;
OR you can do this
cin >> a;
cin >> b;
During program execution, you can enter the value of a, then space (one space or more it makes no difference, but people usually leave a single space), then the value of b (on the same line). Or, you can enter the value of a on a line, then the value of b on the next line.
Notice that what follows the extraction operator >> must be a variable. So, it is not quite the same as the insertion operator <<.
14. What follows the insertion operator << can be a variable, it can be a constant identifier (we'll explain it later), it can be an expression, it can be a sentence included within double quotes, it can be a single character included within single or double quotes, etc.