Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


C++ Programming Code Examples

C++ > Code Snippets Code Examples

Formatted IO using IO manipulators

/* Formatted IO using IO manipulators */ #include <iostream> #include <iomanip> using namespace std; int main() { float num[5] = {1.0, -1.2345, 2350.1, 23.4, 45.34}; int i; cout << setiosflags(ios::showpos); cout << setiosflags(ios::scientific); cout << setprecision(2); for(i = 0; i < 5; i++) { cout << setw(20); cout << setfill('$'); cout << num[i] << endl; } return 0; }

Set decimal precision. Sets the decimal precision to be used to format floating-point values on output operations. Behaves as if member precision were called with n as argument on the stream on which it is inserted/extracted as a manipulator (it can be inserted/extracted on input streams or output streams). This manipulator is declared in header <iomanip>. This method accepts n as a parameter which is the integer argument corresponding to which the floating-point precision is to be set.

A program shall contain a global function named main, which is the designated start of the program in hosted environment. main() function is the entry point of any C++ program. It is the point at which execution of program is started. When a C++ program is executed, the execution control goes directly to the main() function. Every C++ program have a main() function.

The floating-point precision determines the maximum number of digits to be written on insertion operations to express floating-point values. How this is interpreted depends on whether the floatfield format flag is set to a specific notation (either fixed or scientific) or it is unset (using the default notation, which is not necessarily equivalent to either fixed nor scientific). Get/Set floating-point decimal precision. The first form (1) returns the value of the current floating-point precision field for the stream. The second form (2) also sets it to a new value. Function returns the precision selected in the stream before the call.

In computer programming, loops are used to repeat a block of code. For example, when you are displaying number from 1 to 100 you may want set the value of a variable to 1 and display it 100 times, increasing its value by 1 on each loop iteration. When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop. A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.

Set field width. Sets the field width to be used on output operations. The C++ function std::setw behaves as if member width were called with n as argument on the stream on which it is inserted/extracted as a manipulator (it can be inserted/extracted on input streams or output streams). It is used to sets the field width to be used on output operations. This manipulator is declared in header <iomanip>. This method accepts n as a parameter which is the integer argument corresponding to which the field width is to be set. This function returns an object of unspecified type. The setw function should only be used as a stream manipulator.

#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program.

Set format flags. Sets the format flags specified by parameter mask. Behaves as if member setf were called with mask as argument on the stream on which it is inserted/extracted as a manipulator (it can be inserted/extracted on input streams or output streams). See ios_base::fmtflags for more information on the particular flags that can be modified with this manipulator function. This manipulator is declared in header <iomanip>.

Get/set format flags. The first form (1) returns the format flags currently selected in the stream. The second form (2) sets new format flags for the stream, returning its former value. The format flags of a stream affect the way data is interpreted in certain input functions and how these are written by certain output functions. See ios_base::fmtflags for the possible values of this function's argument and the interpretation of its return value. The second form of this function sets the value for all the format flags of the stream, overwriting the existing values and clearing any flag not explicitly set in the argument. To access individual flags, see members setf and unsetf.

Consider a situation, when we have two persons with the same name, jhon, in the same class. Whenever we need to differentiate them definitely we would have to use some additional information along with their name, like either the area, if they live in different area or their mother's or father's name, etc. Same situation can arise in your C++ applications. For example, you might be writing some code that has a function called xyz() and there is another library available which is also having same function xyz(). Now the compiler has no way of knowing which version of xyz() function you are referring to within your code.

Set fill character. Sets c as the stream's fill character. Behaves as if member fill were called with c as argument on the stream on which it is inserted as a manipulator (it can be inserted on output streams). The setfill() method of iomaip library in C++ is used to set the ios library fill character based on the character specified as the parameter to this method. This manipulator is declared in header <iomanip>.






Prints a total number of permutation possible for a given string. The time complexity of this algorithm is "O(n)". Algorithm takes the input of the string. Then it checks for the repetition