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

Learn C++ Language

Basic Ios Library bad() Function in C++ Programming Language

Basic Ios Library bad() Function in C++
Check whether badbit is set. Returns true if the badbit error state flag is set for the stream. The bad() method of ios class in C++ is used to check if the stream is has raised any bad error. It means that this function will check if this stream has its badbit set.
Syntax for Ios bad() Function in C++
bool bad() const;
This method does not accept any parameter. Function returns true if the stream's badbit error state flag is set. false otherwise. This flag is set by operations performed on the stream when an error occurs while read or writing data, generally causing the loss of integrity of the stream. Notice that this function is not the exact opposite of good, which checks whether none of the error flags (eofbit, failbit and badbit) are set, and not only badbit: iostate value indicates functions to check state flags (member constants) good() eof() fail() bad() rdstate() goodbit No errors (zero value iostate) true false false false goodbit eofbit End-of-File reached on input operation false true false false eofbit failbit Logical error on i/o operation false false true false failbit badbit Read/writing error on i/o operation false false true true badbit eofbit, failbit and badbit are member constants with implementation-defined values that can be combined (as if with the bitwise OR operator). goodbit is zero, indicating that none of the other bits is set.
Data races
Accesses the stream object. Concurrent access to the same stream object may cause data races.
Exception safety
Strong guarantee: if an exception is thrown, there are no changes in the stream.
/* ios::good() and ios::bad() functions in C++ are used to check the state of the stream whether it is good or bad to do our task. Both of these are defined in ios library. ios::bad() is used to check whether badbit is set. This flag is set by operations performed on the stream when an error occurs while read or writing data, generally causing the loss of integrity of the stream. Notice that this function is not the exact opposite of good, which checks whether none of the error flags (eofbit, failbit and badbit) are set, and not only badbit */ /* Check whether badbit is set by bad() function code example */ #include <bits/stdc++.h> using namespace std; int main() { // Stream stringstream ss; ss.clear(ss.badbit); // Using bad() function bool isBad = ss.bad(); // print result cout << "is stream bad: " << isBad << endl; return 0; }




Examples on different ways to calculate GCD of two integers (for both Positive & Negative integers) using "Loops and Decision" making statements. The "Largest integer" which can





This is a C++ Program code to check whether graph is DAG. In mathematics and computer science, a directed acyclic graph, is a directed graph with no directed cycles. It is formed by