You are on page 1of 22

Programming

Fundamentals

1
C++ DATA TYPES- REVISITED

Data types

Boolean
Numeric Character

Floating Single Multiple


Integer character characters
point

bool

short int long float .. char string


THE BOOL DATA TYPE
 Represents values that are true or false
 bool variables are stored as small integers
 false is represented by 0, true by 1:
bool allDone = true;
bool finished = false;

allDone finished

1 0
4
Character Strings

•Comprised of the characters between the " "


"Hello“
•From storage perspective:
•A series of characters in consecutive memory locations:
•Stored with the null terminator, \0, at the end:

H e l l o \0

5
THE C++ STRING CLASS


Special data type supports working with strings
#include <string>

Can define string variables in programs:
string firstName, lastName;

Can receive values with assignment operator:
firstName = "George";
lastName = "Washington";

Can be displayed via cout
cout << firstName << " " << lastName;

6
THE STRING CLASS IN PROGRAM

7
THE PARTS OF A C++ PROGRAM
C++ programs have parts and components that serve specific purpose

// sample C++ program comment


#include <iostream> preprocessor directive
using namespace std; which namespace to use
int main() beginning of function named main
{ beginning of block for main
cout << "Hello, there!"; output statement
string literal
return 0; send 0 to operating system
} end of block for main
THE \N ESCAPE SEQUENCE
 You can also use the \n escape sequence to start a new
line of output. This will produce two lines of output:

cout << "Programming is\n";


cout << "fun!";

Notice that the \n is INSIDE


the string.
ESCAPE SEQUENCE

10
FORMATTING OUTPUT
 The cout object provides ways to format data as it
is being displayed.
 This affects the way data appears on the screen

 The same data can be printed or displayed in


several different ways.
 E.g. all of the following numbers have the same
value, although they look different:
 720
 720.0
 720.00000000
 7.2e+2
 +720.0
FORMATTING OUTPUT
 The way a value is printed is called its formatting.

 By default cout object has a standard way of formatting


variables of each data type.

 Sometimes, however, you need more control over the


way data is displayed.
• Unfortunately, the
numbers do not line
up in columns.

• This is because some


of the numbers, such
as 5 and 7, occupy
one position on the
screen, while others
occupy two or three
positions.

• cout uses just the


number of spaces
needed to print each
number.
SPECIFYING FIELD WIDTH FOR COUT
 To solve this problem cout offers a way of specifying the
minimum number of spaces to use for each number.

 Here is an example of how it is used:


value = 23; //default cout will use two
//positions
cout << setw(5) << value; //print the
//no using 5 positions
MORE STREAM MANIPULATORS IN
PROGRAM 3-17

Continued…
MORE STREAM MANIPULATORS IN
PROGRAM 3-17
STREAM MANIPULATOR
 Stream manipulator functions with cout can control how
output displays for numeric, string data:

 Minimum positions used to print a field


 number of digits
 Justification of printed text
STREAM MANIPULATORS

Requires iomanip header file


STREAM MANIPULATORS
 Some affect values until changed again:
 fixed: force fixed point notation for floating-point values
during cout
STREAM MANIPULATORS
 setprecision(x): when used with fixed, print
floating-point value using x digits after the decimal.
 Without fixed, print floating-point value using x significant
digits

You might also like