Question

In: Computer Science

what are stream manipulators in C++? Write at least 3 short example programs of stream manipulators.

what are stream manipulators in C++? Write at least 3 short example programs of stream manipulators.

Solutions

Expert Solution

Stream Manipulators are built in functions which are often used in conjunction with the insertion (<<) and extraction (>>) operators on stream objects.

  • endl

This manipulator has the same functionality as ‘\n’(newline character). But this also flushes the output stream.

Example

#include<iostream>
int main() {
   std::cout << "Hello" << std::endl << "World!";
}

Output

Hello
World!
  • setprecision

This manipulator changes floating-point precision.

Example

#include <iostream>
#include <iomanip>
int main() {
   const long double pi = 3.141592653589793239;
   std::cout << "default precision (6): " << pi << '\n'
             << "std::setprecision(10): " << std::setprecision(10) << pi << '\n';
}

Output

default precision (6): 3.14159
std::setprecision(10): 3.141592654
  • setw

This manipulator changes the width of the next input/output field.

Example

#include <iostream>
#include <iomanip>
int main() {
   std::cout << "no setw:" << 42 << '\n'
             << "setw(6):" << std::setw(6) << 42 << '\n'
             << "setw(6), several elements: " << 89 << std::setw(6) << 12 << 34 << '\n';
}

Output

no setw:42
setw(6):    42
setw(6), several elements: 89    1234

Related Solutions

what are stream manipulators in C++? write a detail note.
what are stream manipulators in C++? write a detail note.
Write 3 short paragraphs of at least four sentences each on: What would an Utilitarian say...
Write 3 short paragraphs of at least four sentences each on: What would an Utilitarian say about spanking? Is it the greatest good for the greatest number? What would a Duty ethicist say about spanking? Consider Kant’s second formulation, that all human beings should be treated only as ends in themselves. Or, should this be determined by looking at nature or a religious text? What would a Virtue Ethicist say? Consider Aristotle’s Golden Mean. How does your spanking your children...
Write C++ programs to perform the following tasks. In the program descriptions below, example input and...
Write C++ programs to perform the following tasks. In the program descriptions below, example input and output is provided. NOTE: You don’t need arrays to solve any of these problems. You should NOT use arrays to solve any of these problems. • stat.cpp: Let the user input a one or more integers, space separated, on a single line (as seen below), then work out and display the sum, average, sum of squares and population variance of the numbers. Remember, you...
What is a terminal condition in stream? Provide an example.
What is a terminal condition in stream? Provide an example.
Problem 1. Write two computer programs to simulate a Unicode stream cipher that consists of both...
Problem 1. Write two computer programs to simulate a Unicode stream cipher that consists of both encryption and decryption algorithms. The encryption program accepts inputs from an existing text file, called “letter.txt.” The encryption program produces an output ciphertext file, called “secret” The decryption program takes “secret” as input and decrypts it into a plaintext, called “message.txt.” - The random “seed” must be known, but be kept secure, by the pseudorandom number generators in both encryption and decryption programs. Note...
The goal of this assignment is to write five short Java/Pyhton programs to gain practice with...
The goal of this assignment is to write five short Java/Pyhton programs to gain practice with expressions, loops and conditionals. Boolean and integer variables and Conditionals. Write a program Ordered.java that reads in three integer command-line arguments, x, y, and z.   Define a boolean variable isOrdered whose value is true if the three values are either in strictly ascending order  (x < y < z) or in strictly descending order  (x > y > z), and false otherwise. Print out the variable isOrdered...
Write 2 short Java programs based on the description below. 1) Write a public Java class...
Write 2 short Java programs based on the description below. 1) Write a public Java class called WriteToFile that opens a file called words.dat which is empty. Your program should read a String array called words and write each word onto a new line in the file. Your method should include an appropriate throws clause and should be defined within a class called TextFileEditor. The string should contain the following words: {“the”, “quick”, “brown”, “fox”} 2) Write a public Java...
please write short essays answer this question:Name at least 3 legally required benefits. How can Mangers...
please write short essays answer this question:Name at least 3 legally required benefits. How can Mangers minimize the costs that are associated with legally required benefits?
Write a complete C++ program that at least consists of the main() function and at least...
Write a complete C++ program that at least consists of the main() function and at least two recursive functions. The first function has no return value and can be named printPrime(). It prints first n prime numbers with proper prompt. Note that number 1 is not regarded as a prime number. We assume the first prime number is 2. The printout should start from 2. The prototype of the recursive function should be void printPrime(int n); The algorithm of printPrime()...
C PROGRAM ..... NOT C++ PROGRAM 3 (UPDATE TO TWO PRIOR PROGRAMS FOR REFERENCE OF PRIOR...
C PROGRAM ..... NOT C++ PROGRAM 3 (UPDATE TO TWO PRIOR PROGRAMS FOR REFERENCE OF PRIOR PROGRAM INSTRUCTIONS PLEASE SEE BELOW) PROGRAM 3 Adjust program II to make use of functions. All the same rules from the previous program specifications still apply, for example input gathering, output formatting and breaking on -1 still apply. Additional requirements. Write a function that prompts the user for hours worked, rate and name. Use parameter passing, and pass by reference. Write a function that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT