Course Websites: CS201 Page Link at My Website
Course Websites: CS201 Page Link at My Website
Lecture slides
Assistants Information
Recitations
Office Hours
Make-up Rules
Plagiarism
Chapter 2
Writing and Reading C++ Programs
A programming language has syntax and semantics like any
natural language
Syntax is the set of rules like spelling and grammar in natural
languages
English: syntax spelled, sentences start with subject
followed by verb
C++: main spelled, programs start with main() followed by {
Semantics is the meaning
English: water means H2O
C++: int means integer, + means add
#include <iostream>
using namespace std;
/* traditional first program */
int main()
{
cout << "Hello world" << endl; // display
return 0;
}
What is a literal?
Reserved words (keywords)
What is an identifier?
Variables and basic types
Symbols and compound symbols
Where to use blanks, line breaks?
Basic Input/Output
Literals
Fixed (constant) values
They cannot be changed during programs execution
They can be output by cout
Different format for different types:
String literals
Sequences of characters
Within double quotes (quotes are not part of the string)
Almost any character is fine (letters, digits, symbols)
"Hello world!"
" 10 > 22 $&*%? "
Numeric literals
Integer
3 454 -43 +34
Real
3.1415 +45.44 -54.6 1.2334e3
1.2334e3 is 1.2334 times 10 to the power 3 (scientific notation)
Identifiers
Names of programmer defined elements in a program
Names of variables, functions and parameters
Examples:
number1 valid
number_1 valid
mySum valid
my_sum_1 valid
1number not valid
Syntax (rules):
1. Sequence of letters (a .. z, A ..Z), digits (0 ..9) or underscore
2. Cannot start with a digit
3. Case sensitive (number1 and Number1 are not the same)
Pick meaningful names to improve readability and
understandability of your program (be consistent)
Hungarian notation
Reserved Words (Keywords)
Special and fixed meanings
built-in in C++ language
no need to have libraries to use them
You cannot use a reserved word as a user-defined identifier
Cannot be changed by programmer
int
return
Full list is Table 2.1 of the textbook
Full list also in MSDN:
http://msdn.microsoft.com/en-us/library/aa245310(VS.60).aspx
#include <iostream>
#include <string>
using namespace std;
int main()
{
int radius;
float area;
string myname;
cout << "Please enter your name: ";
cin >> myname;
cout << "Hello " << myname
<< "! Welcome to my area calculation program" << endl;
cout << "Please enter the radius of your circle: ";
cin >> radius;
area = 3.14 * radius * radius;
cout << "the area is: " << area << endl;
return 0;
}
Issues with the Example
Program
What happens if the user enters a real number for
radius?
wrong result
solution: real radius
Can we combine?
cout << "Hello " << myname
<< "! Welcome to my area calculation program" << endl;
cout << "Please enter the radius of your circle: ";
Can we eliminate the variable area?
area = 3.14 * radius * radius;
cout << "the area is: " << area << endl;
Where to use Blanks
(Newline)
You must have at least one blank
between two words (identifiers or keywords)
e.g. int number;
between a word and numeric literal
e.g. return 0;
You cannot have a blank
within a word (e.g. float)
within a compound symbol (e.g. <<)
within a literal (e.g. 3.145)
except string literals, in string literals blanks are blanks
At all other places
blanks are optional and increases readability
area = 3.14*radius * radius;
Several blanks are functionally same as single blank
except within string literals (e.g. "Hello world")
Newlines can be used whenever blank can be used
Stream Output
Output is necessary for our programs
Standard output stream cout is the monitor (read see-out)
cout is implemented in the iostream library
Output is sent to stream by the << operator
cout << "Hello world! ";
What can be output?
String literals between " ", expressions and variables
More than one output could be sent to the stream
cout << "Hello" << " world!" << endl;
endl means end of line
causes next output to be displayed in next line
cout << "Hello world" << endl << " and universe" << endl;
int sum = 10 + 2; Hello world
cout << "sum = " << sum << endl; and universe
cout << 45 << " km. = ";
cout << 45 * 0.62 << " miles" sum = 12
<< endl; 45 km. = 27.9 miles
Stream Input
Input is also necessary for our programs
Standard input stream cin is the keyboard (read see-in)
cin is also implemented in the iostream library
You can input only to variables
Input is read from the stream by the >> operator
cin >> number;
More than one input could be read from the stream
cin >> variable1 >> variable2 >> variable3 ;
Data will be read into the variables in the same order they
are in the cin statement
int a, b, anynumber;
cin >> b >> anynumber >> a;
first the value for b, then the value for anynumber, then the
value of a must be entered by the user using the keyboard
Stream Input
You have to have at least one blank between any two input
entry
Multiple blanks are OK
You may input values at several lines for a single cin
statement
You cannot display something using cin statement