|
I want to generate a random number between a specified range of 1 to 100, how can I do this?
Thanks in advance, Dave
"The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson
|
|
|
|
|
Technically, you can't generate a truly random number. You can, however, get pseudo-random numbers using rand() . For your specific range, try:
int x = (rand() % 100) + 1;
You may or may not also want to use srand() , which seeds the algorithm.
|
|
|
|
|
Thanks, I'll give it a try.
"The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson
|
|
|
|
|
You'd better initialize the seed first. Use srand(time(NULL)).
Otherwise you will always get the same random number sequence.
|
|
|
|
|
Which is not necessarily a bad thing, especially when trying to reproduce a problem.
|
|
|
|
|
I have already ask the question but I cannot see it posted.
password displayed as ****
Thanks
rapace
|
|
|
|
|
already asked. correction
rapace
|
|
|
|
|
the question is ?
anyway, you can make the editbox look like a password field by setting a
style in the resource editor.
Maximilien Lincourt
"Never underestimate the bandwidth of a station wagon filled with backup tapes." ("Computer Networks" by Andrew S Tannenbaum )
|
|
|
|
|
:-DThank you.
Indeed by changing the setting of the property password from False to True.
As simple as that, yet one needs to know this.
Thanks again
rapace
|
|
|
|
|
no problem ...
Maximilien Lincourt
"Never underestimate the bandwidth of a station wagon filled with backup tapes." ("Computer Networks" by Andrew S Tannenbaum )
|
|
|
|
|
Hi,
Another very basic question... I am trying to save the contents of a listbox. I assume that the best way of doing this would be to add the contents list to a string array, something like the following:
void AddListBoxContentsToStringArray(HWND hWnd)
{
//get number of files in List Box:
int filecount = SendDlgItemMessage(hWnd,IDC_FILES,LB_GETCOUNT,0,0);
for(int currfile = 0; currfile < filecount; currfile++)
{
//(Somehow!) add filenames/paths to string array...
SendDlgItemMessage(hWnd,IDC_FILES,LB_GETTEXT,currfile,(LPARAM)(LPTSTR)szFileName[?]);
}
}
In the above example, szFileName[?] represents where I am guessing my string array should go...
My (very basic) question is, how do I define a string array that would work for this purpose and get something along the lines of the above code to work? (I'm using the Windows API with no MFC.) I'm a novice coder, and unfortunately I can't find anything about the basics string arrays in my Sam's and seem only to find very complex stuff when Googling...
All I actually need to do is save the contents of a listbox so that I can use it later.
Many thanks for any help,
KB
|
|
|
|
|
one easy way would be to use a std::string in an std::vector, you can then add elements with push_back, etc.
modified 12-Sep-18 21:01pm.
|
|
|
|
|
char array[100][MAX_PATH];
void AddListBoxContentsToStringArray(HWND hWnd)
{
char szText[MAX_PATH];
int filecount = SendDlgItemMessage(hWnd, IDC_FILES, LB_GETCOUNT, 0, 0);
for (int currfile = 0; currfile < filecount; currfile++)
{
SendDlgItemMessage(hWnd, IDC_FILES, LB_GETTEXT, currfile, (LPARAM) szText);
strcpy(array[currfile], szText);
}
}
|
|
|
|
|
Thanks for the answers, I used David's code in the end as it was the easiest way to go. (Even though it took me a stupid amount of time to realise that I had to delete the items from my listbox in reverse order, because trying to delete file no. 7 isn't going to work if there are only 3 files left in there... d'oh.)
David Crow - thank you loads for all the help you have given me today. You have pretty much single-handedly written my listbox code (not that you'll ever see it, but I'll be sure to give you a shout in the readme ).
Many thanks,
KB
|
|
|
|
|
|
|
Terry O`Nolley wrote:
I'll give you a C++
Ryan "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
|
|
|
|
|
How can I change the toolbar background color? I want it to have a custom color.
Eilzabeth
|
|
|
|
|
Is it possible to use a macro on another macro?
For example (and purely for illustrative purposes), suppose I have the following:
#define YES "1"
#define NO "0"
#define StartYourEngines "1"
and in my code I would like to do something like this:
StartYourEngines = YES;
is that possible? Or how would I go about doing something like that (using macros)?
Thanks.
William
Fortes in fide et opere!
|
|
|
|
|
#define YES "1"
#define NO "0"
#define StartYourEngines YES
|
|
|
|
|
Thanks for replying.
#define StartYourEngines YES
doesn't work. The compiler complains that "YES" is an undeclared identifier.
Actually, prior to posting the question, I had tried a few experiments and was unable to get a clean compile (understanding that a clean compile is not a guarantee for a correct execution of the program), which makes me think either it cannot be done, or it would require a clever way of getting around the compiler, and at the same time getting it to work correctly.
William
Fortes in fide et opere!
|
|
|
|
|
Compiles OK for me in VC6.
If you want to do conditional compilation using macros, then you would typically do something like this:
#define OPTION_A
#ifdef OPTION_A
#endif
#ifdef OPTION_B
#endif If you want to define a number of options, and create a default for one with any of them being dynamically available, you would do something like this:
#define OPTION_A 0
#define OPTION_B 1
#define DEFAULT_OPTION OPTION_A
int nOption = DEFAULT_OPTION;
switch( nOption )
{
case OPTION_A:
{
}
break;
case OPTION_B:
{
}
break;
}
|
|
|
|
|
Thanks for replying.
You posted some very good samples here, and I'll see which one will work for my situation at hand.
Actually, I have a MACRO file that I '#include' in my application. In that MACRO file is where I have (and again for purely illustrative purposes):
#define YES "1"
#define NO "0"
#define StartYourEngines "YES" // using your suggestion
==============================
In the application, I have:
#include "MACROFILE.h"
etc.
if(StartYourEngines == YES)
{
...
StartYourEngines = NO;
}
==============================
That's what I was referring to about the compiler giving the error messages (about "YES" and "NO" being undeclared identifiers).
That's what I'm trying to accomplish without resorting to cluttering up the global namespace. Ordinarily, I would use a specialized class object to do what I'm after, but I'm suffering heavily from time pressure and I'm looking for the quickest and easiest way of getting the work done. Later, I could always go back and clean things up with a better solution, but for right now, I'm doing working lunches and other meals, and surviving on the least amount of sleep as possible.
Thanks again for your suggestions.
William
Fortes in fide et opere!
|
|
|
|
|
If you expand your macros, the resulting code will look like this:
if( "1" == "1" )
{
...
"1" = "0";
} As you can see that's not valid code. If you want to change the value of StartYourEngines at runtime, then you'll need to use a variable something like this:
#define YES 1
#define NO 0
int StartYourEngines = YES;
if( StartYourEngines == YES )
{
...
StartYourEngines = NO;
} Expanding the macros from that example, the code will look like this:
int StartYourEngines = 1;
if( StartYourEngines == 1 )
{
...
StartYourEngines = 0;
}
|
|
|
|
|
It looks like you might be wanting to run something once, if so you could do it like this:
class MyClass
{
protected:
BOOL m_bFirstTime;
public:
MyClass()
: m_bFirstTime( TRUE )
{
}
void MyFunction()
{
if( m_bFirstTime )
{
m_bFirstTime = FALSE;
}
}
}; Although, in real world apps it would be better to structure the class something like this:
class MyClass
{
protected:
BOOL m_bInitialized;
public:
MyClass()
: m_bInitialized( FALSE )
{
}
virtual ~MyClass()
{
UnInitialize();
}
BOOL Initialize()
{
BOOL bResult = FALSE;
{
m_bInitialized = TRUE;
bResult = TRUE;
}
return bResult;
}
void UnInitialize()
{
if( m_bInitialized )
{
m_bInitialized = FALSE;
}
}
BOOL MyFunction()
{
BOOL bResult = FALSE;
{
if( m_bInitialized )
{
bResult = TRUE;
}
}
return bResult;
}
}; Or if you aren't using classes, and you want to be bad, you could do something like this:
void MyFunction()
{
static v_bFirstTime = TRUE;
if( v_bFirstTime )
{
v_bFirstTime = FALSE;
}
} Or this:
static g_bFirstTime = TRUE;
void MyFunction()
{
if( g_bFirstTime )
{
g_bFirstTime = FALSE;
}
}
|
|
|
|