Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

C++ version of C code

bodhi2016bodhi2016 Member Posts: 50
In th book of win32 api by charles petzold there ia an example code as given below :-
[code]int iLength ;
TCHAR szBuffer [40] ;
[ other program lines ]
iLength = wsprintf (szBuffer, TEXT ("The sum of %i and %i is %i"),
iA, iB, iA + iB) ;
TextOut (hdc, x, y, szBuffer, iLength) ;[/code]
Since i am a c++ programmer can anybody give the c++ version of this code .Like i know that that the portion
[code]("The sum of %i and %i is %i"),iA, iB, iA + iB) [/code]
will be like
[code]cout<<"the sum of "iA"and" iB"is"iA+iB;[/code]
in c++
Again if i apply cout here then what about the wsprintf
And i have seen this underlined portion of the code in many books but can somebody explain its meaning
[code]cxCaps = (tm.tmPitchAndFamily & [u]1 ? 3 : 2[/u]) * cxChar / 2 ;[/code]
And can this code be written like this:-
[code]#define NUMLINES ((int) (sizeof sysmetrics / sizeof sysmetrics [0]))[/code]
[code]#define NUMLINES (int= (sizeof sysmetrics / sizeof sysmetrics [0]))[/code]
Plzzz brothers somebody explain it.
Thank You .

«1

Comments

  • MT2002MT2002 Member Posts: 1,444
    : Since i am a c++ programmer can anybody give the c++ version of this
    : code .Like i know that that the portion
    : [code]: ("The sum of %i and %i is %i"),iA, iB, iA + iB) [/code]:
    : will be like
    : [code]: cout<<"the sum of "iA"and" iB"is"iA+iB;[/code]:
    : in c++

    No it would not. wsprintf/sprintf are used to write the string to a buffer, not stdout. You can either use std::stringstream or just use std::sprintf() instead.

    : Again if i apply cout here then what about the wsprintf
    : And i have seen this underlined portion of the code in many books
    : but can somebody explain its meaning
    : [code]: cxCaps = (tm.tmPitchAndFamily & [u]1 ? 3 : 2[/u]) * cxChar / 2 ;[/code]:

    The ?: operator is a short-hand if operation:

    [code]
    return (!p) ? false : true;

    //is the same as...

    if (!p)
    return false;
    else
    retuen true;[/code]

    : And can this code be written like this:-
    : [code]: #define NUMLINES ((int) (sizeof sysmetrics / sizeof sysmetrics [0]))[/code]:
    : [code]: #define NUMLINES (int= (sizeof sysmetrics / sizeof sysmetrics [0]))[/code]:

    No. The second line should create a compile time error.

    Also, because you are using C++, you should be using inline routines over preprocessor macros, and static_cast<> over the C-style casting style (the (int) in the first line)

    ie, something likes this:
    [code]
    inline int NumLines (const int i) const {

    return static_cast(sizeof (sysmetrics / sizeof sysmetrics[0]);
    }[/code]

    [hr][size=1][leftbr].:EvolutionEngine[rightbr][leftbr].:MicroOS Operating System[rightbr][leftbr][link=http://www.brokenthorn.com]Website :: OS Development Series[rightbr][/link][/size]
  • bodhi2016bodhi2016 Member Posts: 50
    MT2002 can wsprintf be used in c++??
    Is it a c++ function or c.If c then what is the c++ version
  • LundinLundin Member Posts: 3,711
    Neither. wsprintf is not ISO C nor ISO C++.
  • AsmGuru62AsmGuru62 Member Posts: 6,519
    : Neither. wsprintf is not ISO C nor ISO C++.

    [color=Blue]Not sure about that statement...
    wsprintf() is Win32 API, so I think it is perfectly normal for Win32 coding. By the same logic we can't use CreateWindow().
    :-)
    [/color]
  • MT2002MT2002 Member Posts: 1,444
    : MT2002 can wsprintf be used in c++??
    : Is it a c++ function or c.If c then what is the c++ version
    :
    Throw away wsprintf() and use std::sprintf(). Its at least ISO C. (sprintf() is C not C++, but can be used in C++...Its inside of the std namespace [std::sprintf])

    For ISO C++:

    [code]std::stringstream stream;
    strean << "The sum of" << iA << " and " << iB << " is " << iA + iB;

    std::string string=stream.str();[/code]

    //std::string contains your string. To get the length, call string.length()

    [hr][size=1][leftbr].:EvolutionEngine[rightbr][leftbr].:MicroOS Operating System[rightbr][leftbr][link=http://www.brokenthorn.com]Website :: OS Development Series[rightbr][/link][/size]
  • LundinLundin Member Posts: 3,711
    I wasn't sure either, so I did a pdf search in both standards. There are similar wide character functions in C99, but this one is not among them.
  • bodhi2016bodhi2016 Member Posts: 50
    Mt2002 what does strean means like strlen means string means string length.And when i am compling i am getting a prompt that stringstream is not a member of std
  • MT2002MT2002 Member Posts: 1,444

    [b]#include [/b] should fix the stringstream error.

    A stream represents an abstraction for which input and output operations are performed.

    You are already working with them. std::cout, std::cin, std::fstream et al use stream operators to output and input from a source (stdout/stdin). These are streams.
    [hr][size=1][leftbr].:EvolutionEngine[rightbr][leftbr].:MicroOS Operating System[rightbr][leftbr][link=http://www.brokenthorn.com]Website :: OS Development Series[rightbr][/link][/size]
  • bodhi2016bodhi2016 Member Posts: 50
    MT2002 everything is working perfectly except the numbers
    Instead of
    SM_CXSCREEN Screen breath in pixel 640
    SM_CYSCREEN Screen height in pixel 480
    the output is something like this
    SM_CXSCREEN Screen height in pixel |||
    SM_CYSCREEN Screen height in pixel ||
    .
    .
    .
    and so on.
    and sometime other kinds of symbols
    here's the modified code
    [code]LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    static int cxChar, cxCaps, cyChar ;
    HDC hdc ;
    int i ;
    PAINTSTRUCT ps ;
    TCHAR szBuffer [10] ;
    TEXTMETRIC tm ;
    int iLength;

    switch (message)
    {
    case WM_CREATE:
    hdc = GetDC (hwnd) ;

    GetTextMetrics (hdc, &tm) ;
    cxChar = tm.tmAveCharWidth ;
    cxCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2) * cxChar / 2 ;
    cyChar = tm.tmHeight + tm.tmExternalLeading ;

    ReleaseDC (hwnd, hdc) ;
    return 0 ;

    case WM_PAINT :
    hdc = BeginPaint (hwnd, &ps) ;

    for (i = 0 ; i < NUMLINES ; i++)
    {
    TextOut (hdc, 0, cyChar * i,
    sysmetrics[i].szLabel,
    lstrlen (sysmetrics[i].szLabel)) ;

    TextOut (hdc, 22 * cxCaps, cyChar * i,
    sysmetrics[i].szDesc,
    lstrlen (sysmetrics[i].szDesc)) ;

    SetTextAlign (hdc, TA_RIGHT | TA_TOP) ;
    [u][color=Red]std::stringstream stream;
    stream<<GetSystemMetrics (sysmetrics[i].iIndex);
    std::string string=stream.str();
    iLength=string.length(); [/color][/u]
    TextOut (hdc, 22 * cxCaps + 40 * cxChar, cyChar * i, szBuffer,iLength);
    //wsprintf (szBuffer, TEXT ("%5d"),
    //GetSystemMetrics (sysmetrics[i].iIndex))) ;

    SetTextAlign (hdc, TA_LEFT | TA_TOP) ;
    }
    EndPaint (hwnd, &ps) ;
    return 0 ;
    case WM_DESTROY :
    PostQuitMessage (0) ;
    return 0 ;
    }
    return DefWindowProc (hwnd, message, wParam, lParam) ;
    }[/code]can you tell where i am going wrong.
    I have attached a screeshot you can take a look.
    Thank You
  • bodhi2016bodhi2016 Member Posts: 50
    This post has been deleted.
«1
Sign In or Register to comment.