Professional Documents
Culture Documents
College of Information Technology: CSC 103: Computer Programming For Scientists and Engineers
College of Information Technology: CSC 103: Computer Programming For Scientists and Engineers
? ? ? ?
Figure 2-2 Memory allocation after the four variable declaration statements
? ? ?
Figure 2-3 Memory spaces after executing the statement length = 6.0;
double length;
int num1;
Data Types
Integral
Floating-Point
(int, bool, Enumeration
(double, …)
char, …)
Simple Data Types
Data Types
Integral
Floating-Point
(int, bool, Enumeration
(double, …)
char, …)
string Type
• Evaluation rules:
– The precedence and associativity rules from above
still apply.
– If an operator’s operands are of the same type
(both integer or both floating-point):
• The result is the same type as the operands.
– If an operator has both types of operands:
• Integer is “promoted” to floating-point.
• The operation is performed.
• The result is floating-point.
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 37
Type Conversion and Casting
• A single input statement can assign values to more than one variable.
– Example: if feet and inches are variables of type int, the
following statement reads two integers from the keyboard and
places these integer values in feet and inches respectively:
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 53
Prompt Lines
• cin >> feet >> inches;
– cin >> feet;
cin >> inches;
• A prompt line is a cout statement that tells the user what to
do. Example:
cout << "Please enter a number between 1 and 10 and "
<< "press the return key." << endl;
cin >> num;
• For example:
#include <iostream>
– Causes the preprocessor to include the header file
iostream in the program. Without this, you could not use
cout or cin in your program.
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 62
namespace and Using cin and cout in
a Program
• cin and cout are declared in the header file
iostream within the std namespace.
• To use cin or cout in a program, use the following
two statements:
#include <iostream>
using namespace std;
• Without the using statement, you would need to
type std::cin instead of just cin throughout
your program.