|
alogaritm for mini calender project
|
|
|
|
|
|
hello
this is a try at a verry simple game
it is a try at moving a circle whit cursor-key's
i use code::block's
4 error's were detected :
somting wrong whit case
any help is welkome
#if defined(UNICODE) && !defined(_UNICODE)
#define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
#define UNICODE
#endif
#include <tchar.h>
#include <windows.h>
bool key[ 256 ] ;
int playerx , playery , winx , winy ;
#define FRAME_TIMER 1
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
TCHAR szClassName[ ] = _T("CodeBlocksWindowsApp");
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
HWND hwnd;
MSG messages;
WNDCLASSEX wincl;
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure;
wincl.style = CS_DBLCLKS;
wincl.cbSize = sizeof (WNDCLASSEX);
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL;
wincl.cbClsExtra = 0;
wincl.cbWndExtra = 0;
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
if (!RegisterClassEx (&wincl))
return 0;
hwnd = CreateWindowEx (
0,
szClassName,
_T("Game 1"),
WS_OVERLAPPEDWINDOW | WS_MAXIMIZE ,
CW_USEDEFAULT,
CW_USEDEFAULT,
800,
600,
HWND_DESKTOP,
NULL,
hThisInstance,
NULL
);
ShowWindow (hwnd, nCmdShow);
while (GetMessage (&messages, NULL, 0, 0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return messages.wParam;
}
LRESULT CALLBACK WindowProcedure (HWND hwnd,
UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE :
playerx = 100 ;
playery = 100 ;
SetTimer( hwnd
, FRAME_TIMER
, 40 , NULL ) ;
break ;
case WM_KEYDOWN :
key[ wParam ] = true ;
switch( wParam )
{
case VK_ESCAPE :
PostQuitMessage( 0 ) ;
break ;
default : ;
}
break ;
case WM_KEYUP :
key[ wParam ] = false ;
break ;
case WM_TIMER :
switch( wParam )
{
case FRAME_TIMER :
if ( key[ VK_UP ] ) playery-- ;
if ( key[ VK_DOWN ] ) playery++ ;
if ( key[ VK_LEFT ] ) playerx-- ;
if ( key[ VK_RIGHT ] ) playerx++ ;
if ( playery > winy ) playery = 0 ;
if ( playery < 0 ) playery = winy ;
if ( playerx > winx ) playerx = 0 ;
if ( playerx < 0 ) playerx = winx ;
InvalidateRect( hwnd , NULL , true ) ;
break ;
default : ;
}
break ;
case WM_PAINT :
PAINTSTRUCT paint ;
HDC hdc = BeginPaint( hwnd , &paint ) ;
RECT rect ;
GetClientRect( hwnd , &rect ) ;
winx = rect.right ;
winy = rect.bottom ;
Ellipse( hdc ,
playerx - 20 ,
playery - 20 ,
playerx + 20 ,
playery + 20 ) ;
EndPaint( hwnd , &paint ) ;
break ;
case WM_DESTROY :
PostQuitMessage( 0 ) ;
break ;
default :
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0 ;
}
|
|
|
|
|
You should at least report the exact error messages and mark the offending lines.
|
|
|
|
|
Most compilers object if you put local variables inside a case statement. You should either declare them before the first switch or enclose the case block in braces, thus:
case WM_PAINT :
{
PAINTSTRUCT paint ;
HDC hdc = BeginPaint( hwnd , &paint ) ;
RECT rect ;
GetClientRect( hwnd , &rect ) ;
winx = rect.right ;
winy = rect.bottom ;
Ellipse( hdc ,
playerx - 20 ,
playery - 20 ,
playerx + 20 ,
playery + 20 ) ;
EndPaint( hwnd , &paint ) ;
}
break ;
Also, in future, please provide the exact text of error messages, and indicate where they occur.
|
|
|
|
|
 update :
i tryed it [ thanks for sugestions
and
i got somthing on screen
error :
my screen isn't black and isn't fulscreen
my ellipse isn't yellow
#if defined(UNICODE) && !defined(_UNICODE)
#define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
#define UNICODE
#endif
#include <tchar.h>
#include <windows.h>
bool key[ 256 ] ;
int playerx , playery , winx , winy ;
#define FRAME_TIMER 1
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
TCHAR szClassName[ ] = _T("CodeBlocksWindowsApp");
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
HWND hwnd;
MSG messages;
WNDCLASSEX wincl;
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure;
wincl.style = CS_DBLCLKS;
wincl.cbSize = sizeof (WNDCLASSEX);
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL;
wincl.cbClsExtra = 0;
wincl.cbWndExtra = 0;
wincl.hbrBackground = (HBRUSH)BLACK_BRUSH;
if (!RegisterClassEx (&wincl))
return 0;
hwnd = CreateWindowEx (
0,
szClassName,
_T("Game 1"),
WS_OVERLAPPEDWINDOW | WS_MAXIMIZE ,
CW_USEDEFAULT,
CW_USEDEFAULT,
800,
600,
HWND_DESKTOP,
NULL,
hThisInstance,
NULL
);
ShowWindow (hwnd, nCmdShow);
while (GetMessage (&messages, NULL, 0, 0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return messages.wParam;
}
LRESULT CALLBACK WindowProcedure (HWND hwnd,
UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT paint ;
HDC hdc = BeginPaint( hwnd , &paint ) ;
RECT rect ;
HBRUSH brush =
CreateSolidBrush( RGB(255,255,0) ) ;
HPEN pen =
CreatePen( PS_SOLID ,
0 ,
RGB(255,255,0) ) ;
switch (message)
{
case WM_CREATE :
playerx = 100 ;
playery = 100 ;
SetTimer( hwnd
, FRAME_TIMER
, 40 , NULL ) ;
break ;
case WM_KEYDOWN :
key[ wParam ] = true ;
switch( wParam )
{
case VK_ESCAPE :
PostQuitMessage( 0 ) ;
break ;
default : ;
}
break ;
case WM_KEYUP :
key[ wParam ] = false ;
break ;
case WM_TIMER :
switch( wParam )
{
case FRAME_TIMER :
if ( key[ VK_UP ] ) playery-- ;
if ( key[ VK_DOWN ] ) playery++ ;
if ( key[ VK_LEFT ] ) playerx-- ;
if ( key[ VK_RIGHT ] ) playerx++ ;
if ( playery > winy ) playery = 0 ;
if ( playery < 0 ) playery = winy ;
if ( playerx > winx ) playerx = 0 ;
if ( playerx < 0 ) playerx = winx ;
InvalidateRect( hwnd , NULL , true ) ;
break ;
default : ;
}
break ;
case WM_PAINT :
GetClientRect( hwnd , &rect ) ;
winx = rect.right ;
winy = rect.bottom ;
Ellipse( hdc ,
playerx - 20 ,
playery - 20 ,
playerx + 20 ,
playery + 20 ) ;
EndPaint( hwnd , &paint ) ;
break ;
case WM_DESTROY :
PostQuitMessage( 0 ) ;
break ;
default :
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0 ;
}
|
|
|
|
|
You are calling BeginPaint for every message (not just WM_PAINT ), but you only call EndPaint after processing a WM_PAINT message. You need to move all code related the the painting into the block that handles the WM_PAINT message. You also need to select your brushes and pens into the device context before you draw anything. And lastly you need to delete any DC objects that you use in painting.
I would suggest studying the GDI documentation on MSDN for sample code. But be aware that GDI and GDI+ are being deprecated by Microsoft in favour of Direct2D API Reference (Windows)[^]
|
|
|
|
|
Hi all of you. I come here hoping to solve a problem that I have met on retrieving data from DB (SQL Server).
I retrieve data in the following way:
pRs->GetFieldValue(i, *pDBVariant);
nothing special ... (pRS is CRecordset*, and pDBVariant is CDBVariant*).
And this is method to format data:
CString CMyDoc::FormatData(CDBVariant* pDBVariant)
{
CString sRet;
do
{
if(NULL == pDBVariant)
break;
switch(pDBVariant->m_dwType)
{
case DBVT_LONG:
TRACE("DBVT_LONG\n");
sRet.Format(_T("%d"), pDBVariant->m_lVal);
break;
case DBVT_DOUBLE:
TRACE("DBVT_DOUBLE\n");
break;
case DBVT_SHORT:
TRACE("DBVT_SHORT\n");
break;
case DBVT_SINGLE:
TRACE("DBVT_SINGLE\n");
break;
case DBVT_STRING:
TRACE("DBVT_STRING\n");
break;
case DBVT_ASTRING:
TRACE("DBVT_ASTRING\n");
sRet = *pDBVariant->m_pstring;
break;
case DBVT_DATE:
TRACE("DBVT_DATE\n");
break;
case DBVT_WSTRING:
TRACE("DBVT_WSTRING\n");
CStringW wstring = *pDBVariant->m_pstringW;
sRet = CString(wstring);
break;
}
}
while(FALSE);
return sRet;
}
I have the following SQL:
SELECT nUser FROM my_table;
nUser has int SQL data type, and when I test this data with CMyDoc::FormatData, the returning data type are: DBVT_LONG
Ok, let take another SQL:
SELECT dVAT FROM my_table;
dVAT has decimal (10.2) SQL data type, and when I test this data with CMyDoc::FormatData, the returning data type are: DBVT_ASTRING ... why ?
I have tested another SQL:
SELECT sName FROM my_table;
sName has string SQL data type, and when I test this data with CMyDoc::FormatData, the returning data type are: DBVT_ASTRING, which are ok ...
but, excepting int SQL data type, all other SQL data type are having DBVT_ASTRING when I test this with CMyDoc::FormatData ... why ?
Can you help me ?
Thank you.
|
|
|
|
|
Have you checked the actual data returned in the variant to see whether it is a string or the value type you expect?
|
|
|
|
|
I re-written the FormatData method:
CString CMyDoc::FormatData(CDBVariant* pDBVariant)
{
CString sRet;
switch(pDBVariant->m_dwType)
{
case DBVT_LONG:
TRACE("DBVT_LONG\n");
sRet.Format(_T("%d"), pDBVariant->m_lVal);
break;
case DBVT_DOUBLE:
TRACE("DBVT_DOUBLE\n");
break;
case DBVT_SHORT:
TRACE("DBVT_SHORT\n");
break;
case DBVT_SINGLE:
TRACE("DBVT_SINGLE\n");
break;
case DBVT_STRING:
TRACE("DBVT_STRING\n");
break;
case DBVT_ASTRING:
TRACE("DBVT_ASTRING\n");
sRet = *pDBVariant->m_pstring;
break;
case DBVT_DATE:
TRACE("DBVT_DATE\n");
break;
case DBVT_WSTRING:
TRACE("DBVT_WSTRING\n");
CStringW wstring = *pDBVariant->m_pstringW;
sRet = CString(wstring);
break;
}
return sRet;
}
I have the following SQL:
SELECT nUser FROM my_table
In DB, nUser has "int" type. The result in FormatData method: DBVT_LONG
I have another SQL:
SELECT dValue FROM my_table
In DB, dValue has "decimal (10.2)" type. The result in FormatData method: DBVT_ASTRING !? Why ?
I have another SQL:
SELECT sName FROM my_table
In DB, dValue has "varchar(1024)" type. The result in FormatData method: DBVT_ASTRING - correct.
I noticed that any king of column type in DB, except "int" type, return DBVT_ASTRING in FormatData method ... why ?
I have tried to get data with CRecordset as dynaset, snaphot and dynamic ... every one of them had the same result ...
|
|
|
|
|
That is what you wrote in your original question, and removing the unnecessary do ... while() is not going to affect things. The issue is, what value type is SQL returning into your recordset? What you are looking at here is happening long after the data has been sent from the database.
|
|
|
|
|
What you have described sounds perfectly correct depending how you set the tables up.
Your nUser will be (look at SQL spec integer value between the range 2^ -31 and 2^31 -1) which is 32 bits or 4 bytes and would be a long in C/C++.
Your decimal (10.2) will be stored as an IEEE-754/-854-compliant bit string which will be 5-17 bytes.
If you stored your decimal(10.2) as singles/doubles you would get issues when you added them etc the small fractions you can't see would roll do you understand lets add 3 digits with 3 decimal places
Stored .... 10.2 format
-------------------------
1.344 .... 1.34
2.364 .... 2.36
1.134 .... 1.13
----------------------
4.842 .... 4.83
See the Problem 4.842 down to 2 digits is 4.84 and is not right answer they don't give same result and that is why SQL does not automatically covert numbers with precision.
I am sure if you make a table of doubles, single or floats they will come back as that just don't expect that from decimal, numeric which will be strings
In vino veritas
modified 25-Oct-17 15:22pm.
|
|
|
|
|
Yes, you are right ... I must change the data type in SQL in order to get right values in CDBVariant ... but I should ask this to my SQL colleagues ...
Kindly thank you for your answer !!
|
|
|
|
|
It's unrelated to the problem but why the unnecessary do/while loop?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
Good point, it's unnecessary !
|
|
|
|
|
Hi,
Flaviu2 wrote: dVAT has decimal (10.2) SQL data type, and when I test this data with CMyDoc::FormatData, the returning data type are: DBVT_ASTRING ... why ?
Because the best way to pass a floating point value between network devices is as TEXT. The SQL server has no idea what architecture your device is using and how it will interpret the floating point value.
Best Wishes,
-David Delaune
|
|
|
|
|
|
#include <stdio.h>
int main()
{
int a=(1,2,3);
int b=(3,2,1);
for(; a>0; a--)
for(; b<3; b++);
printf("%d ", a*b);
return 0; }
output of this twister..??
|
|
|
|
|
|
|
The carpenter manufactures chairs and tables. He can sell them on the market, each table for Pt EUR and each chair for Pc EUR. He needs A hours and B wooden planks to produce one table and C hours and D planks for one chair. His weekly workload cannot be higher than H hours. His weekly supply for planks is S. The carpenter cannot store the planks. Help the carpenter to prepare his weekly schedule that maximizes his income. Design an algorithm that accepts A, B, C, D, H, S, Pt, Pc as an input and tells how many chairs and how many tables the carpenter should produce weekly, and what is the income assuming this production plan.
I haven't even got a clue... Can anyone please help?
|
|
|
|
|
We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.
Try it yourself, you may find it is not as difficult as you think!
If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
of course It's just that I'm a total beginner and I just figured this might be the place to look for help. Well obviously I know it'd be best to start from inputing all the variables and then decide whether the chairs or tables are more profitable. My problem is I don't know how to decide that since there are two variables for each. Any suggestions?
|
|
|
|
|
Start by looking at how to would do it manually: get a pen and paper, and start playing with numbers.
Work out how you do it yourself, and an algorithm should become apparent.
Working this out is part of the task - heck, it is the task, you aren't supposed to implement it yet, if ever. It's an exercise in logical thinking and analysis, intended to start you thinking in "appropriate" ways. If you don't do it yourself, the "thought patterns" that you will use later don't start to form, and things can be a load harder later.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
OriginalGriff wrote: Start by looking at how to would do it manually: get a pen and paper... I wholeheartedly agree.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|