0% found this document useful (0 votes)
9 views

M4 Guide

Uploaded by

Janina Torre
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

M4 Guide

Uploaded by

Janina Torre
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

STUDY GUIDE

C++ INPUT/OUTPUT STATEMENTS

The console is the basic interface of computers, normally it is the set composed of the keyboard and the
screen. The keyboard is generally the standard input device and the screen the standard output device.

In the iostream C++ library, standard input and output operations for a program are supported by two data
streams: cin for input and cout for output. Additionally, cerr and clog have also been implemented - these are two
output streams specially designed to show error messages. They can be redirected to the standard output or to a
log file.

Therefore cout (the standard output stream) is normally directed to the screen and cin (the standard input
stream) is normally assigned to the keyboard.

Output (cout)

The cout stream is used in conjunction with the overloaded operator << (a pair of "less than" signs).

cout << "Output sentence"; // prints Output sentence on screen


cout << 120; // prints number 120 on screen
cout << x; // prints the content of variable x on screen

The << operator is known as insertion operator since it inserts the data that follows it into the stream that
precedes it. In the examples above it inserted the constant string Output sentence, the numerical constant 120 and
the variable x into the output stream cout. Notice that the first of the two sentences is enclosed between double
quotes (") because it is a string of characters. Whenever we want to use constant strings of characters we must
enclose them between double quotes (") so that they can be clearly distinguished from variables. For example, these
two sentences are very different:

cout << "Hello"; // prints Hello on screen


cout << Hello; // prints the content of Hello variable on screen

The insertion operator (<<) may be used more than once in a same sentence:
cout << "Hello, " << "I am " << "a C++ sentence";
this last sentence would print the message Hello, I am a C++ sentence on the screen. The utility of repeating the
insertion operator (<<) is demonstrated when we want to print out a combination of variables and constants or more
than one variable:

cout << "Hello, I am " << age << " years old and my zipcode is " << zipcode;

If we suppose that variable age contains the number 24 and the variable zipcode contains 90064 the output
of the previous sentence would be:

Hello, I am 24 years old and my zipcode is 90064

It is important to notice that cout does not add a line break after its output unless we explicitly indicate it,
therefore, the following sentences:
cout << "This is a sentence.";
cout << "This is another sentence.";

will be shown followed in screen:

This is a sentence.This is another sentence.

even if we have written them in two different calls to cout. So, in order to perform a line break on output we must
explicitly order it by inserting a new-line character, that in C++ can be written as \n:

cout << "First sentence.\n ";


cout << "Second sentence.\nThird sentence.";

produces the following output:

First sentence.
Second sentence.
Third sentence.

To add a new-line, you may also use the endl manipulator. For example:

cout << "First sentence." << endl;


cout << "Second sentence." << endl;

would print out:


First sentence.
Second sentence.

The endl manipulator has a special behavior when it is used with buffered streams: they are flushed. But
anyway cout is unbuffered by default.

You may use either the \n escape character or the endl manipulator in order to specify a line jump to cout.
Notice the differences of use shown earlier.

Input (cin)

Handling the standard input in C++ is done by applying the overloaded operator of extraction (>>) on the
cin stream. This must be followed by the variable that will store the data that is going to be read. For example:

int age;
cin >> age;

cin can only process the input from the keyboard once the RETURN key has been pressed. Therefore, even
if you request a single character cin will not process the input until the user presses RETURN once the character has
been introduced.

// i/o example Please enter an integer value: 702


#include <iostream.h> The value you entered is 702 and its double is
int main () 1404.
{
int i;
cout << "Please enter an integer value: ";
cin >> i;
cout << "The value you entered is " << i;
cout << " and its double is " << i*2 << ".\n";
return 0;
}

You can also use cin to request more than one datum input from the user:

cin >> a >> b;

is equivalent to:

cin >> a;
cin >> b;

In both cases the user must give two data, one for variable a and another for variable b that may be
separated by any valid blank separator: a space, a tab character or

Escape Sequences

Type Meaning

\\ backslash

\' single quote

\" double quote

\? question mark

\a audible bell

\b backspace

\f formfeed

\n newline

\0 NULL character

\r carriage return

\t tab (horizontal)

\v tab (vertical)

\xhhh insert ASCII code hhh


TYPE CONVERSION

Type Conversion is the process of converting one predefined type into another.
When variables of one type are mixed with variables of another type, a type conversion will
occur.
C++ facilitates the type conversion into the following two forms :
• Implicit Type Conversion
• Explicit Type Conversion

IMPLICIT TYPE CONVERSION


Implicit also known as automatic type conversion. It is a conversion performed by the compiler
without programmer's intervention.
It is applied generally whenever differing data types are intermixed in an expression, so as not
to lose information.
bool → char → short int → int → unsigned int → long → unsigned →
long long → float → double → long double

Example 1: Conversion From int to double

Example 2: Automatic Conversion from double to int


DATA LOSS DURING CONVERSION

Conversion from one data type to another is prone to data loss. This happens when data of a
larger type is converted to data of a smaller type.

EXPLICIT TYPE CONVERSION


When the user manually changes data from one type to another, this is known as explicit
conversion. An explicit type conversion is user-defined that forces an expression to be of
specific data type. This is the general form to perform type casting in C++:
(datatype) expression;
For example, to make sure that expression (x + y/2) evaluates to type float, write it as:
(float) (x + y/2);

EXPLICIT TYPE CONVERSION


Let’s consider this program below,

Example 1: Float to integer type casting

Example 2: Float to integer type casting


Example 3: Float to integer type casting

COMMON MATH FUNCTIONS

pow( ) and sqrt ( )


pow() is used to find the power of the given number while sqrt() returns the square root of
value. These math functions are part of <math.h> header.

abs() and fabs () functions, both are used to retrieve or calculate the absolute value. abs() is
used to calculate the absolute value for integer type numbers whereas fabs() are used for
floating type numbers.
abs( ) and fabs( )

floor( ) and ceil ( )


The floor and ceiling functions map a real number to the greatest preceding or the least
succeeding integer, respectively. Also, they are part of <math.h> header.
floor( ) and ceil ( )

Examples: floor()
Input : 2.5
Output : 2
Input : -2.1
Output : -3
Input : 2.9
Output : 2

trunc( )
trunc() is used to round (truncate) the value toward zero and returns the nearest integral value.

round ( )
round() is used to round the given number to the closest integer.

setprecision( )
setprecision when used along with ‘fixed’ provides precision to floating point numbers correct
to decimal numbers mentioned in the brackets of the setprecision.
setprecision function is part of <iomanip> header.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy