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

Check status: EOF encountered, Non-Fatal I/O error, Fatal I/O error

/* Check status: EOF encountered, Non-Fatal I/O error, Fatal I/O error */ #include <iostream> #include <fstream> using namespace std; void checkstatus(ifstream &in); int main(int argc, char *argv[]) { if(argc!=2) { cout << "Usage: Display <filename>\n"; return 1; } ifstream in(argv[1]); if(!in) { cout << "Cannot open input file.\n"; return 1; } char c; while(in.get(c)) { if(in) cout << c; checkstatus(in); } checkstatus(in); // check final status in.close(); return 0; } void checkstatus(ifstream &in) { ios::iostate i; i = in.rdstate(); if(i & ios::eofbit) cout << "EOF encountered\n"; else if(i & ios::failbit) cout << "Non-Fatal I/O error\n"; else if(i & ios::badbit) cout << "Fatal I/O error\n"; }

Get characters. Extracts characters from the stream, as unformatted input. The get() function is used to read a character(at a time) from a file. The classes istream and ostream define two member functions get(), put() respectively to handle the single character input/output operations. There are two types of get() functions. Both get(char *) and get(void) prototype can be used to fetch a character including the blank space,tab and newline character. The get(char *) version assigns the input character to its argument and the get(void) version returns the input character. Since these functions are members of input/output Stream classes, these must be invoked using appropriate objects.

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.

The if...else statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities. The if...else ladder allows you to check between multiple test expressions and execute different statements. In C/C++ if-else-if ladder helps user decide from among multiple options. The C/C++ if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the C else-if ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.

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.

Get error state flags. Returns the current internal error state flags of the stream. The internal error state flags are automatically set by calls to input/output functions on the stream to signal certain errors. It is used to check get error state flags. The internal error state flags are automatically set by calls to input/output functions on the stream to signal certain errors. This function does not accept any parameter. Function returns an object of type ios_base::iostate that can contain any combination of the following state flag member constants:

In C++ programming we are using the iostream standard library, it provides cin and cout methods for reading from input and writing to output respectively. To read and write from a file we are using the standard C++ library called fstream. Let us see the data types define in fstream library is: • ofstream: This data type represents the output file stream and is used to create files and to write information to files. • ifstream: This data type represents the input file stream and is used to read information from files. • fstream: This data type represents the file stream generally, and has the capabilities of both ofstream and ifstream which means it can create files, write information to files, and read information from files.

#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.

In computer programming, we use the if statement to run a block code only when a certain condition is met. An if statement can be followed by an optional else statement, which executes when the boolean expression is false. There are three forms of if...else statements in C++: • if statement, • if...else statement, • if...else if...else statement, The if statement evaluates the condition inside the parentheses ( ). If the condition evaluates to true, the code inside the body of if is executed. If the condition evaluates to false, the code inside the body of if is skipped.

In while loop, condition is evaluated first and if it returns true then the statements inside while loop execute, this happens repeatedly until the condition returns false. When condition returns false, the control comes out of loop and jumps to the next statement in the program after while loop. The important point to note when using while loop is that we need to use increment or decrement statement inside while loop so that the loop variable gets changed on each iteration, and at some point condition returns false. This way we can end the execution of while loop otherwise the loop would execute indefinitely. A while loop that never stops is said to be the infinite while loop, when we give the condition in such a way so that it never returns false, then the loops becomes infinite and repeats itself indefinitely.

Find the minimum spanning tree of the given graph using Prims algorihtm. This algorithm is a "greedy algorithm" that finds a minimum spanning tree for a "Connected Undirected"

Merge-Sort is based on an algorithmic design pattern called Divide & Conquer. It forms tree structure. The height of the tree will be log(n) And we merge n element at every level of the