ECE151 - Lecture 2
ECE151 - Lecture 2
Programming
Parts of the C++ program
● You use the stream insertion operator << to send output to cout:
● You can use the endl manipulator to start a new line of output. This will
produce two lines of output:
Programming is
fun!
The endl Manipulator
● You can also use the \n escape sequence to start a new line of output. This
will produce two lines of output:
Programming is
fun!
The cin Object
#include <iostream>
using namespace std;
int main()
{
string firstName;
cout << "Type your first name: \n";
cin >> firstName; // get user input from the keyboard
cout << "Your name is: " << firstName;
return 0;
}
The cin Object
The #include Directive
Feature of Variable
• Int • 10, 20
• Float • 1.2, 9.3 Example:
• Double • 3.40 int X= 20;
• String • “Nile University” float Y= 3.3;
• Char • “N” double W= 606;
string Name= “Nile University”
char O= “B”
Variables and Literals
Type of Variable
Variable Definition
Literals
20 is an integer literal
String Literals in Program 2-9
You cannot use any of the C++ key words as an identifier. These words have
reserved meaning.
Variable Names
● A variable name should represent the purpose of the variable. For example:
itemsOrdered
totalSales Yes
total_Sales Yes
● An integer literal is an integer value that is typed into a program’s code. For
example:
itemsOrdered = 15;
Integer Literals
Integer Literals
CODE: MEMORY:
char letter; letter
letter = 'C';
67
Character Literals
'A'
Character Literals in Program 2-13
Character Strings
H e l l o \0
The C++ string Class
● Can be represented in
○ Fixed point (decimal) notation:
31.4159 0.0000625
○ E notation:
3.14159E1 6.25e-5
● Are double by default
● Can be forced to be float (3.14159f) or long double (0.0000625L)
Floating-Point Data Types in Program
2-16
The bool Data Type
allDone finished
1 0
The bool Data Type
● Example:
#include <iostream>
using namespace std;
int main() {
bool X = true;
bool Y = false;
cout << X << "\n";
cout << Y;
return 0;
}
allDone finished
1 0
The bool Data Type
Boolean Variables in Program 2-17
Determining the Size of a Data Type
The sizeof operator gives the size of any data type or variable:
double amount;
cout << "A double is stored in "
<< sizeof(double) << "bytes\n";
cout << "Variable amount is stored in "
<< sizeof(amount)
<< "bytes\n";
Variable Assignments and Initialization
item = 12;
● The variable receiving the value must appear on the left side of the = operator.
● This will NOT work:
// ERROR!
12 = item;
Variable Initialization
● The scope of a variable: the part of the program in which the variable can be
accessed
● A variable cannot be used before it is defined
Variable Out of Scope in Program 2-20
Arithmetic Operators
+ addition ans = 7 + 3; 10
- subtraction ans = 7 - 3; 4
* multiplication ans = 7 * 3; 21
/ division ans = 7 / 3; 2
% modulus ans = 7 % 3; 1
Arithmetic Operators in Program 2-21
A Closer Look at the / Operator
/* this is a multi-line
comment
*/
● Can begin and end on the same line:
○ #include <iostream.h>