// This program prompts the user for a portfolio number // and displays the current value of each holding // in that portfolio and the profit or loss #include #include #include using namespace std; // Include the sqlca communications area #include void main () { // Declare host variables: EXEC SQL BEGIN DECLARE SECTION; varchar userid[20]; varchar password[20]; char dbstring[10]; char portfoliono[5]; char stocksymbol[4]; char purchasedate[11]; long numbershares; float purchaseprice; float currentprice; EXEC SQL END DECLARE SECTION; double holding_value, profit_loss; bool found = false; // Prompt user for username and password: cout << "Enter your username: "; cin >> userid.arr; cout << "Enter your password: "; cin >> password.arr; // Connect to the INVESTMENTS database: userid.len = strlen((char *) userid.arr); password.len = strlen((char *) password.arr); strcpy(dbstring, "cp01"); EXEC SQL connect :userid identified by :password using :dbstring; // Check connection is OK if (sqlca.sqlcode != 0) { cout << "\nConnection failed; check username and password"; return; } cout << endl << endl << "Connected to database" << endl; EXEC SQL whenever sqlerror go to error_routine; cout << setiosflags(ios::fixed) << setprecision(2); // Get the portfolio number from the user cout << endl << "Enter the portfolio number: "; cin >> portfoliono; // Declare the cursor EXEC SQL declare valcrsr cursor for select holding.stocksymbol, purchasedate, numbershares, purchaseprice, currentprice from holding, stock where holding.stocksymbol = stock.stocksymbol and portfoliono = :portfoliono; // Open the cursor EXEC SQL open valcrsr; // Fetch first row EXEC SQL fetch valcrsr into :stocksymbol, :purchasedate, :numbershares, :purchaseprice, :currentprice; cout << endl << endl << "Stock Purchase Number Purchase Current Current Profit/"; cout << endl << "Number Date Bought Price Price Value Loss"; cout << endl << "-----------------------------------------------------------------"; while (sqlca.sqlcode == 0) { found = true; holding_value = numbershares * currentprice; profit_loss = holding_value - numbershares * purchaseprice; cout << endl << stocksymbol << setw(15) << purchasedate << setw(5) << numbershares << setw(10) << purchaseprice << setw(11) << currentprice << setw(11) << holding_value << setw(10) << profit_loss; // Fetch next row EXEC SQL fetch valcrsr into :stocksymbol, :purchasedate, :numbershares, :purchaseprice, :currentprice; } if (!found) cout << endl << "No holdings found for this portfolio number" << endl; // Close cursor EXEC SQL close valcrsr; // Disconnect from the database: cout << endl << endl << "Disconnecting from database" << endl; EXEC SQL commit release; return; error_routine: // Negative SQLCODE returned cout << endl << "*** SQL Error Routine entered ***"; cout << endl << endl << "SQLCODE is: " << sqlca.sqlcode; cout << endl << "Error Message: " << sqlca.sqlerrm.sqlerrmc; cout << endl << "Processing terminated" << endl << endl; return; }