Log in

View Full Version : c++ question


UnderCover-
November 5th, 2001, 15:26
hello,

i'd like to know if i can use GetWindowText and SetWindowText in this case, i've been trying all day long and im getting error after error:

int nada;
int entered = GetDlgItemInt(hDlg, EDIT_NAME, &nada ,true);
int calc = entered * 31337 ;
calc = calc - 9989 ;
SetDlgItemInt(hDlg, EDIT_SERIAL, calc, TRUE );

tnx in advance.
UnderCover

Solomon
November 5th, 2001, 20:05
what's the error message of your compiler?
That's important

try SetWindowTextA instead of SetWindowText. Actually SetWindowText is only a macro. It's not a function. It will be translated into SetWindowTextA or SetWindowTextW by the pre-processor according to another macro UNICODE.

<winuser.h>

#ifdef UNICODE
#define SetWindowText SetWindowTextW
#else
#define SetWindowText SetWindowTextA
#endif // !UNICODE

UnderCover-
November 6th, 2001, 11:27
int nada;
int entered = GetWindowText(hwndnumber,serial,sizeof(number)); // user will only enter numbers
int calc = entered * 31337;
calc = calc-9989;

until this part it compiles but when i try to print calc using SetWindowtext i get:

error C2664: 'SetWindowTextA' : cannot convert parameter 2 from 'int' to 'const char *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast

here is my SetWindowtext : SetWindowText(hwndserialshow,calc);

UnderCover-
November 6th, 2001, 14:44
Hi,

lemme tell what i need:
use GetWindowText to get what the user input (it'll be only numbers)

suppose he enter "123" then i'll do

x = 123 * 31337

then i want to use SetWindowText to put x on screen.

Regards
UnderCover

AllYourBase
November 6th, 2001, 16:16
You need to convert the calc from an integer to the list of characters representing the integer probably using wsprintf:

int wsprintf(
LPTSTR lpOut, // pointer to buffer for output
LPCTSTR lpFmt, // pointer to format-control string
... // optional arguments
);


eg:

char szBuffer[255];
wsprintf(szBuffer, "%u", calc);
SetWindowText(hwndserialshow, szBuffer);

UnderCover-
November 6th, 2001, 19:16
Hello,

suppose i enter 1234 as number doing this manually i should get 2994004 as serial but im getting -546467

int calc,nada;
nada = GetWindowText(hwndnumber,serial,sizeof(number));
calc = nada * 2.8678;
calc= calc - 546487;
wsprintf(final,"%d", calc);
SetWindowText(hwndserialshow,final);

i think the program is assuming nada is the size of serial here :
nada = GetWindowText(hwndnumber,serial,sizeof(number));

if anyone can help me with this
tnx in advance.

AllYourBase
November 6th, 2001, 19:26
GetWindowText returns the length of the text (assuming its successful) not the actual text:

Return Values
If the function succeeds, the return value is the length, in characters, of the copied string, not including the terminating null character.

Instead use StrToInt on the variable which recieves the window text (serial?)

eg:

calc = StrToInt(serial) * 2.8678

Unregistered
November 6th, 2001, 19:55
hello,

what should i declare to use strtoint ?
error C2065: 'StrToInt' : undeclared identifier

Solomon
November 6th, 2001, 20:12
try this:

wsprintf(final,"%lu", calc);

"%lu" means "long unsigned integer".

UnderCover-
November 7th, 2001, 02:21
Hi solomon, thats not the case :/
the problem is getting wha the user inputs in GetWindowText and multiply with some value.

what the user inputs i char so i think it needs casting or something like that.

Solomon
November 7th, 2001, 02:50
use atol( ) to convert string to integer
#include <stdlib.h>


pls refer to MSDN for the following C runtime functions:
atof, atoi, _atoi64, atol

UnderCover-
November 7th, 2001, 03:02
char number[100]={0};
GetWindowText(hwndnumber,name,sizeof(number));
atol(number);

error C2296: '*' : illegal, left operand has type 'char [100]'

Solomon
November 7th, 2001, 03:19
char number[100]={0};
GetWindowText(hwndnumber, number, sizeof(number));
int n = atol(number);
n *= 2237;

I just compiled the above code with VC++ 6. It works.
Try changing "char number[100]={0}" to "char number[100]".


Quote:
Originally posted by UnderCover-
char number[100]={0};
GetWindowText(hwndnumber,name,sizeof(number));
atol(number);

error C2296: '*' : illegal, left operand has type 'char [100]'

Unregistered
November 7th, 2001, 09:14
tnx solomon it worked i just had to change n *= 2237; to n = n * 2237;

dunno why..

Clandestiny
November 7th, 2001, 17:43
Quote:
Originally posted by Unregistered
tnx solomon it worked i just had to change n *= 2237; to n = n * 2237;

dunno why..


Hiya,

n *= 2237 is just another way of way of saying n = n * 2237 and both of these expressions should evaluate exactly the same. This notation is just a syntactical shorthand in the C language. Of course, you can also use the other arithmetic operators in this way ...+=, -=, /=...ect.

Regards,
Clandestiny