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++ > Algorithms Code Examples

This program connects to an ODBC data source called "db97".

/* This program connects to an ODBC data source called "db97". It then executes a SQL statement SELECT Model */ #include <windows.h> #include <sqlext.h> #include <stdio.h> int main(void) { HENV hEnv = NULL; // Env Handle from SQLAllocEnv() HDBC hDBC = NULL; // Connection handle HSTMT hStmt = NULL; // Statement handle UCHAR szDSN[SQL_MAX_DSN_LENGTH] = "db97"; // Data Source Name buffer UCHAR* szUID = NULL; // User ID buffer UCHAR* szPasswd = NULL; // Password buffer UCHAR szModel[128]; // Model buffer SDWORD cbModel; // Model buffer bytes recieved UCHAR szSqlStr[] = "Select Model From Makes Where Make='Vauxhall'"; // SQL string RETCODE retcode; // Return code // Allocate memory for ODBC Environment handle SQLAllocEnv (&hEnv); // Allocate memory for the connection handle SQLAllocConnect (hEnv, &hDBC); // Connect to the data source "db97" using userid and password. retcode = SQLConnect (hDBC, szDSN, SQL_NTS, szUID, SQL_NTS, szPasswd, SQL_NTS); if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) { // Allocate memory for the statement handle retcode = SQLAllocStmt (hDBC, &hStmt); // Prepare the SQL statement by assigning it to the statement handle retcode = SQLPrepare (hStmt, szSqlStr, sizeof (szSqlStr)); // Execute the SQL statement handle retcode = SQLExecute (hStmt); // Project only column 1 which is the models SQLBindCol (hStmt, 1, SQL_C_CHAR, szModel, sizeof(szModel), &cbModel); // Get row of data from the result set defined above in the statement retcode = SQLFetch (hStmt); while (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) { printf (" %s ", szModel); // Print row (model) retcode = SQLFetch (hStmt); // Fetch next row from result set } // Free the allocated statement handle SQLFreeStmt (hStmt, SQL_DROP); // Disconnect from datasource SQLDisconnect (hDBC); } // Free the allocated connection handle SQLFreeConnect (hDBC); // Free the allocated ODBC environment handle SQLFreeEnv (hEnv); return 0; }

Check whether eofbit is set. Returns true if the eofbit error state flag is set for the stream. This flag is set by all standard input operations when the End-of-File is reached in the sequence associated with the stream. Note that the value returned by this function depends on the last operation performed on the stream (and not on the next). Operations that attempt to read at the End-of-File fail, and thus both the eofbit and the failbit end up set. This function can be used to check whether the failure is due to reaching the End-of-File or to some other reason.

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.

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.

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.

A relational operator is used to check the relationship between two operands. C++ Relational Operators are used to relate or compare given operands. Relational operations are like checking if two operands are equal or not equal, greater or lesser, etc. Relational Operators are also called Comparison Operators.

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

Logical Operators are used to compare and connect two or more expressions or variables, such that the value of the expression is completely dependent on the original expression or value or variable. We use logical operators to check whether an expression is true or false. If the expression is true, it returns 1 whereas if the expression is false, it returns 0. Assume variable A holds 1 and variable B holds 0:

The C++ comments are statements that are not executed by the compiler. The comments in C++ programming can be used to provide explanation of the code, variable, method or class. If we write comments on our code, it will be easier for us to understand the code in the future. Also, it will be easier for your fellow developers to understand the code. By the help of comments, you can hide the program code also. There are two types of comments in C++: • Single Line comment. • Multi Line comment

The sizeof() is an operator that evaluates the size of data type, constants, variable. It is a compile-time operator as it returns the size of any variable or a constant at the compilation time. The size, which is calculated by the sizeof() operator, is the amount of RAM occupied in the computer. The sizeof is a keyword, but it is a compile-time operator that determines the size, in bytes, of a variable or data type. The sizeof operator can be used to get the size of classes, structures, unions and any other user defined data type. The data_type can be the data type of the data, variables, constants, unions, structures, or any other user-defined data type.

A return statement ends the processing of the current function and returns control to the caller of the function. A value-returning function should include a return statement, containing an expression. If an expression is not given on a return statement in a function declared with a non-void return type, the compiler issues an error message. If the data type of the expression is different from the function return type, conversion of the return value takes place as if the value of the expression were assigned to an object with the same function return type.