c++ Coding Standards 001

The preferred method of looping through data or sets is the for-next-loop.  In the example below a list box is populated from a collection of MySQL data records.

[codesyntax lang=”cpp”]

void dlgDe2::PopLb()
{
    size_t iend = da.kjbill_read();
    for (int ii=0; ii < iend; ii++)
    {
        lbxItems->Append(std::to_string(da.kjbilla[ii].id) + " " + da.kjbilla[ii].item);

    }

}

[/codesyntax]

The definition for object da is below.

[codesyntax lang=”cpp”]

#include "bcswebtools_kjbill_io.h"

bcswebtools_kjbill_io da;

[/codesyntax]

The method to read the data table is below.

[codesyntax lang=”cpp”]

size_t bcswebtools_kjbill_io::kjbill_read()
{
    int ii = 0;
    size_t numRecs = 0;
    dbc.driver = get_driver_instance();
    dbc.con = dbc.driver->connect(dbc.server, dbc.user, dbc.pwd);
    dbc.con->setSchema(dbc.catalog);
    dbc.stmt = dbc.con->createStatement();
    string cmd;
    cmd = "SELECT id,cnt,item,ltot,cost FROM kjbill order by item";
    if (isel == 1)
    {
        cmd = "SELECT id,cnt,item,ltot,cost FROM kjbill where id = " + oneRec;
    }
    if (isel == 2)
    {
        cmd = "SELECT id,cnt,item,ltot,cost FROM kjbill order by id";
    }
    dbc.res = dbc.stmt->executeQuery(cmd.c_str());
    numRecs = dbc.res->rowsCount();
    if (numRecs < maxRecs)
    {
        while (dbc.res->next())
        {
            kjbilla[ii].id = dbc.res->getInt("id");
            kjbilla[ii].cnt = dbc.res->getInt("cnt");
            kjbilla[ii].item = dbc.res->getString("item");
            kjbilla[ii].ltot = dbc.res->getDouble("ltot");
            kjbilla[ii].cost = dbc.res->getDouble("cost");
            ii++;
        }
    }
    delete dbc.res;
    delete dbc.stmt;
    delete dbc.con;
    return numRecs;
}

[/codesyntax]

Below is the discription for the records.

[codesyntax lang=”cpp”]

    struct kjbill_area {
        int id;
        int cnt;
        string item;
        double ltot;
        double cost;
        dcost_area dca;
    };

[/codesyntax]

Mr. Arch Brooks, Software Engineer, Brooks Computing Systems, LLC authored this article.

Leave a Reply

Your email address will not be published. Required fields are marked *