Loading....
Coupon Accepted Successfully!

 

Type Casting With The cout Statement

Type casting refers to conversion data of one basic type to another by applying external use of data type keywords. The description of type casting is explained in Chapter 3. Programs on type casting are as follows.


3.10 Write a program to use different formats of type casting and display the converted values.

Explanation: In the above program, the variables of int float, double, and char type are declared and initialized. The variable a is initialized with 66, f with 2.5, d with 85.22, and c with character ‘K’.

The first cout statement converts integer value to the corresponding character according to the ASCII character set and the character B is displayed.

The second cout statement converts float value to integer. The value displayed is 2 and not 2.5. When type cast format (int) is used, the decimal portion of float value is removed and only the integer part is considered.

In the third statement, the double value is converted to character. The number 85.22 is converted to integer and then to character. The char data type is nothing else than int data type. The only difference is that the char data type has range from −128 to 127, which requires one byte in the memory.

The last statement converts character to int. The value of ‘K’ is 75 when printed as an integer. The format (int) converts char to int.

In Figure, 65 is an integer and it is converted to character A by using type casting format (char). Table 3.3 describes various type casting formats and their output results.
 

Typecasting integer
 

Table: Type Casting Formats

Type Casting Formats

Outputs

Conversion

cout<<(char)65;

A

int to char

cout<<(int)‘A’;

65

char to int

cout<<(int)5.22;

5

float to int

cout<<(char)78.33;

N

float to char

cout<<(double)123445338.33;

1.234453e+08

float to double

cout<<(unsigned)-1;

65535

signed to unsigned


3.11 Write a program to display data using type casting.

Explanation: Consider the following statements.

int x=77 – Declares integer variable x and initializes it with 77.

float y=5.1252 – Declares float variable y and initializes it with 5.1252.

char z=‘A’; – Declares character variable z and initializes it with character ‘A’.

 

The following statements are used to display the contents on the screen.
 

cout<<“ x = ”<<(char) x<<endl Variable x is an integer but value displayed will be ‘M’ because the statement (char) converts integer to the corresponding ASCII character.
cout<<“ y = ”<<(int)y <<endl Variable y is a float but before printing the value 5.1252, it is converted to integer and the output will be 5.
cout<<“ z = ”<<(int)z Variable z is of character type. The character value is converted to integer and the output displayed will be 65.


3.12 Write a program to display A to Z alphabets using ASCII values.

Explanation: In the above program, A to Z alphabets are displayed using cout() and printf() statements. In cout() statement before printing, type casting is done. The integer is converted to corresponding char type symbol and displayed. In the printf() statement, the control string %c performs this task. The quotation mark (“ ” ) inserts space between two successive characters.
 

3.13 Write a program to display addresses of variables in hexadecimal and unsigned integer formats.

Explanation: The cout statement displays the address of a variable in hexadecimal format. Using type casting, syntax (unsigned) converts hexadecimal to unsigned integer (decimal). The output shows addresses in both hexadecimal and unsigned integer (decimal) formats.

The & (ampersand) operator is used to display the address of the variable. The address operator is preceded by the variable name. The address is always represented as an unsigned integer. The cout statement displays the address in hexadecimal format. To convert the hexadecimal address to an unsigned integer, type casting is used.

3.14 Write a program to display string using & and * operators with cout statements.

Explanation: In the above program, the character pointer name is assigned to the string “c plus plus”. The first cout statement displays the string using variable name. The statement cout<<&name[0]<<“\n” displays the string. Here, & operator is used and the 0 (zero) points to the base address of the string. In the second statement, if the base address is not specified, then it will display the address. The third statement uses the pointer notation to display the string.

 

3.15 Write a program to display a string using different syntaxes using the operators * and & with cout statement.

Explanation: In the first statement, the operators * and & are used one after another and the variable name is inside the parentheses. In the second statement, operator & is outside and the operator * and variable name are inside the parentheses. In the third statement, parentheses are not used. All the three statements display the output “c plus plus.”

Difference of Using C and C++ I/O Functions

The printf() and scanf() of C language needs format string. For example, to read and display an integer, the scanf and printf() statement can be written as follows.

 

int x;

scanf (“%d”,&x)

printf (“%d”,x);

 

In the above statements, %d is used to tell the I/O functions to treat the data as integer.

If the integer x is changed to long integer, the programmer needs to change every occurrence of %d in the program with %ld.

The C++ statement reads and displays the same data as follows.

 

int x;

cin>>x;

cout<<x;

 

Here, the cin and cout statements do not require any format string. If the type of x is changed to long integer, the user need not specify the type of data or any correction in the statement. The cin and cout statement identifies the data type. The format of cin and cout statement is the same for all types of variables.


get() and put() functions

get() function

 

The single character input and output operations in C++ can be done using put() and get() functions. The classes istream and ostream provide the two member functions put() and get(). The get() is used to read a character and put() is used to display the character on the screen.

The get() function has two syntaxes:

  1. get(char*);
  2.  get(void);

If syntax (a) is used, the get() function assigns the read data to its argument, whereas when the statement (b) is used, the get() function returns the data read. The data is assigned to the variable present on the left-hand side of the assignment operator. These functions are members of I/O stream classes and can be called using object.


put() function

 

The put() function is used to display the string on the screen. It is a member of ostream class. The syntax of put() is as follows:

  1. cout.put (‘A’);
  2. cout.put (x);

The statement (a) displays the character ‘A’ on the screen and the statement (b) displays the contents of variable x on the screen. If an integer is used as an argument, its corresponding ASCII value is displayed. Few examples are illustrated below.

3.16 Write a program to display the character on the screen using put() function.

Explanation: The cout.put() statement displays one character at a time on the screen. In this program, three characters are displayed on the screen using cout.put() statement.

3.17 Write program to use escape sequence with cout.put() statement.

Explanation: The escape sequences such as ‘\n’, ‘\t’, etc., can be used with cout.put() statement. The escape sequences are combinations of two characters. The output of the program displays the number 1 and 2 in two separate lines. The statement cout.put(‘\n’) splits a line.

 

3.18 Write a program to use multiple put() statements with single cout object and display the characters.

 

Explanation: In the above program, the single object cout is used followed by sequence of put() statement. The put() statements are separated by dot operators. In this way, multiple statements can be combined.

 

3.19 Write a program to read character using get() and display it using put().

Explanation: In the above program, character variable ch is declared. The first cout statement displays message “Enter a character:” on the screen. The cin.get() function activates input stream and character entered by the user is stored in the variable ch. The cout.put() statement displays the character on the screen.
 

3.20 Write a program to read characters using a sequence of get() statements and display the characters read using a sequence of put() statements.

Explanation: In the above program, a character array ch[3] is declared. The sequence of get() and put() functions are used to read and display the characters. The get() function reads characters and stores in array ch[3]. The put() function displays the same on the screen.

 

3.21 Write a program to read and display the string. Use get() and put() functions.

Explanation: In the above program, the character array x[ ] is declared. The first while loop reads characters using cin.get() function through the keyboard. When a user presses the enter key, the while loop terminates. The second while loop displays the characters read using the function cout.put(). The output of the program is as shown above.

getline() and write() functions

getline() function

 

The getline() and write() functions are useful in string input and output. The getline() functions read the string including white space. The cin() function does not allow to enter the string with blank spaces. The input reading is terminated when a user presses the enter key. The new line character is accepted but not saved and replaced with the null character. The object cin calls the function as follows.

 

cin.getline (variable, size);

 

where the variable name may be any character array name and the size is the size of the array.

write() function

 

The write() function is used to display the string on the screen. Its format is similar to getline() function, but the function is exactly the opposite. The syntax is as follows.

 

cout.write (variable, size);

 

where the variable name may be a character type and size is the size of the character arrays. The cout.write() statement displays only a specified number of characters given in the second argument, though the actual string may be more in length. If the size of the array is larger than the actual string length, then the size argument contains the actual size of the array. In this case, the getline() displays blank spaces for the remaining unfilled elements. The following program illustrates both the functions.
 

3.22 Write a program to display a string using cout.write() statements.

Explanation: In this program, the first statement displays “INDIA” followed by one blank space. This is because the argument value is greater by one than the actual string length.

 

Similarly, the second statement displays “IS” followed by one blank space. The reason is same.

 

The last statement displays “GREAT.” Here, the argument value and the string lengths are same. Hence, no blank spaces are displayed.

 

In case the argument value is less than the actual string length, the complete string will not be displayed. The number of characters of the string displayed depends on the value of the given argument.
 

3.23 Write a program to show the effect if less argument is given than the actual string length in the cout.write() statement.

 

Explanation: In the above program, the cout.write() will not display the complete string. The statement displays the characters according to the value of the second argument. The value of the second argument is three. Hence, instead of six characters only three characters are displayed and the remaining characters are skipped.

 

3.24 Write a program to read a string using getline() function and display it using the write() statement.

Explanation: In the above program, the x[] is a character array. The getline() function reads the string through the keyboard. The getline() function accepts the string including spaces. The cout() statement displays the string including white spaces. The first write() statement displays the string with garbage values. This is because the string length is less than the actual size of the array. In the second write() statement, strlen() function calculates the length of the string and that length is used as an argument in the write() statement. This statement displays the entered string without any garbage collection.

 

The cin() statement cannot accept a string including spaces. It accepts only a single word. The cout() statement displays the string read through cin() and getline() functions, that is, it can display the string with or without blank spaces. The write() statement displays the string according to the specified size. It displays the string with or without blank spaces. The write() does not support any escape sequence.

3.25 Write a program to display the string using different arguments in write() statement.

Explanation: In the above program, two strings are entered in character arrays x[] and a[]. The successive write() statement in one line displays the string one after another. Thus, we can use multiple write() statements followed by a single cout object.





Test Your Skills Now!
Take a Quiz now
Reviewer Name