Topic2 20234 Part2
Topic2 20234 Part2
STRUCTURE
PROBLEM SOLVING
(PART 2)
COLLEGE OF COMPUTING, INFORMATICS &
MATHEMATICS
2024
2.16 The cin Object
THE cin OBJECT
• Standard input object
• Like cout, requires iostream file
• Used to read input from keyboard
• Information retrieved from cin with >>
• Input is stored in one or more variables
system(“Pause”);
THE cin OBJECT
• cin converts data to the type that matches the variable:
int height;
cout << "How tall is the room? ";
cin >> height;
DISPLAYING A PROMPT
• A prompt is a message that instructs the user to enter data.
• You should always use cout to display a prompt before each cin
statement.
H e l l o \0
CHARACTER STRINGS
q To read an entire line of input, use cin.getline():
const int SIZE = 81;
char address[SIZE]; //or char address[81];
cout << "Enter your address: ";
cin.getline(address, SIZE);
area = 2 * PI * radius;
cout << "border is: " << 2*(l+w);
ORDER OF OPERATIONS
• In an expression with more than one operator, evaluate in this order:
• In the expression 2 + 2 * 2 – 2
2 + 2 * 2 – 2 = 4
(2 + 2) * 2 – 2 = 6
2 + 2 * (2 – 2) = 2
(2 + 2) * (2 – 2) = 0
GROUPING WITH PARENTHESES
ALGEBRAIC EXPRESSIONS
¡ Multiplication requires an operator:
Area=lw is written as Area = l * w;
sum = sum + 1;
1 #include <iostream>
2 using namespace std
3
4 int main()
5 {
6 cout << "Programming is fun << endl;
7
8 system(“Pause”);
9 }
SYNTAX ERRORS
q When you compile the program using Dev C++, it displays the
following errors:
q Two errors are reported. First, the semicolon (;) is missing at the end of
line 2. Second, the string Programming is fun should be closed
with a closing quotation mark in line 6.
RUNTIME ERRORS
q Runtime errors cause a program to terminate abnormally.
q They occur while an application is running if the environment detects an
operation that is impossible to carry out.
q Input mistakes typically cause runtime errors. An input error occurs when the
program is waiting for the user to enter a value, but the user enters a value
that the program cannot handle.
q For instance, if the program expects to read in a number, but instead, the user
enters a string, this causes data-type errors to occur.