|
Is there a function to find the differences between two time structs of type _timeb ? If not, is there any easy way to show atleast the hundreth's of seconds between to time_t values?
Whenever I do something below I always get 0's, it can't always be that exact:
double diff;
diff = difftime(end, start);
char msg[] = "blah blah blah runtime %.10f second(s).";
- Nick Parker My Blog | My Articles
|
|
|
|
|
time_t only has 1-second resolution, so you'll never get a different less than 1 second (unless it's 0). Your best bet would probably be to use FILETIME , which is a 64-bit number with 100ns resolution. It's a plain number so calculating differences is easy. (I think the CRT time structs are incredibly clunky and I avoid them as much as possible.)
--Mike--
Personal stuff:: Ericahist | Homepage
Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt
CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ
----
Laugh it up, fuzzball.
|
|
|
|
|
Thanks Mike, are there other functions other than GetFileTime that you would suggest, I am not working with files so I won't have a HANDLE for it?
Nevermind, I think SystemTimeToFileTime should work. Thanks again for pointing me in the right direction.
- Nick Parker My Blog | My Articles
|
|
|
|
|
First off apols if this is in the wrong section i am new here
I have this code now and try as i may i cannot get the random number to change it is always 41 when it should be between 0-100. I am not that good at C++ and would appreciate any help can i please reinforce the basis that my knowledge is very basic thanks for any help guys.
#include <iostream.h>
#include <stdlib.h>
int main(void)
{
// declare our variables
// Guess entered by user and random number
int guess, random;
random = rand();
// Creates a loop for the main program
do
{
cout << "My High-Low game!\n\n";
cout << "Please enter a number between 0 and 100: ";
cin >> guess;
cout << "You entered " << guess << "!\n";
if (guess == random)
{
cout << "Correct guess, the answer is " << guess << "!\n";
}
else
{
if (guess > random)
{
cout << "Your guess is too high!\n";
}
else
{
cout << "Your guess is too low!\n";
}
}
// Terminating condition for the loop
// Program stops when the guess equals the random number
}
while(guess!=random);
// This command pauses the program
system("PAUSE");
return 0;
}
This Better ******* Work!
|
|
|
|
|
Put the line
random = rand();
in the do while block
and better add this to the start of the main
srand(time(NULL));
this way the seed is randomly generated too
(you wont start with the same population over and over again)
Papa
while (TRUE)
Papa.WillLove ( Bebe ) ;
|
|
|
|
|
You need to call srand() first, otherwise rand() will always return the same number. A line like
srand((unsigned)time(NULL));
before the call to rand() should do the trick.
|
|
|
|
|
To elaborate a bit more on the previous response, it is recommend that you first seed the random number generator. If you wish a consistent pattern of values then always seed with the same value. If not then you can use this :
srand( time(NULL) % RAND_MAX );
to obtain values between 0 and 100 then you can do this :
random = rand() % 101;
The modulus operator returns the remainder of the division operation of the two numbers. This has the effect of clamping the return value between 0 and one less than the dividend.
__________________________________________
a two cent stamp short of going postal.
|
|
|
|
|
rand() returns pseudo random numbers rather than real random numbers. This is why you always get the same value. You can work your way around this by setting the random seed, before you invoke rand(). Seed is a number which is used inside the rand() function to produce the sequence of these pseudo random numbers.
So, different seed value gives you different sequence of numbers. Common trick is to give the current time as seed number as this gives you a different sequence each time you run your application. You can set the seed like this:
srand(static_cast<unsigned>(time(0)));
Make this function call before you use rand() for the first time.
If you wan't the numbers be in a spesified range (between 1 and 100) you must use the following line instead of just invoking rand().
random = 1 + (rand() % 100);
rand() returns numbers between 0 (?) and RAND_MAX (== big number, depends on you compiler I think) and the above line takes a modulus from the returned value and this conveniently scales the result between 0 and 99.
Hope this helps,
Cohen
|
|
|
|
|
Thank you for the tips guys i will try them out i just have to figure a way fo getting it to give the user 8 trys max. Thanks for not talking to clever for me lol
This Better ******* Work!
|
|
|
|
|
bobski2200 wrote:
i just have to figure a way fo getting it to give the user 8 trys max.
Change
while(guess!=random); to
while(guess!=random && num_tries < 8); Start num_tries at 0 and increment it after each guess.
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
|
|
|
|
|
Graham - Slow
Rick - Slower
Cohen - Slowest
|
|
|
|
|
Hi!
I've got a struct which needs to be byte swapped, so I figured, why not overload the conversion functions to take my struct as a param, byte swap the fields, then return a new struct. However, I keep getting an error that the compiler doesnt know how to convert one parameter 1 (of htons) from unsigned int to myStruct. Below is my code:
typedef struct
{
u_8 protocol_id;
u_8 msg_type;
u_16 length;
} FrameHdr;
_inline FrameHdr ntohs (FrameHdr i_param)
{
FrameHdr mObj;
mObj.length = ntohs (i_param.length);
return (mObj);
}
The compiler error is:
error C2664: 'ntohs' : cannot convert parameter 1 from 'unsigned short' to 'FrameHdr'
I tried adding 'const' at the end of the function definition but that didnt work either.
Any ideas?? TIA!
-C
|
|
|
|
|
typedef struct FrameHdr
{
u_8 protocol_id;
u_8 msg_type;
u_16 length;
FrameHdr ntohs (FrameHdr i_param);
} FrameHdr;
FrameHdr FrameHdr::ntohs (FrameHdr i_param)
{
FrameHdr mObj;
mObj.length = ::ntohs (i_param.length);
return (mObj);
}
Papa
while (TRUE)
Papa.WillLove ( Bebe ) ;
|
|
|
|
|
I cannot add a function definition to my struct. The structs are used for data to send in a msg and in byte aligned etc.
Any other way of doing this w/o adding a function definition to my struct?
Thanks.
-C
|
|
|
|
|
Why not a wrapper to your structure
Papa
while (TRUE)
Papa.WillLove ( Bebe ) ;
|
|
|
|
|
I know there's a much simpler answer to this. I only want to overload a function and its not letting me. I only want a simple solution to this compiler error. Thanks.
-C
|
|
|
|
|
A namespace would serve in your case
this way you separate your symbols from the global ones.
No?
Papa
while (TRUE)
Papa.WillLove ( Bebe ) ;
|
|
|
|
|
I've never used namespaces before.
|
|
|
|
|
Hello,
I've made a windows app with Visual C++ using the MFC's. Everything works grand.
The problem is when I send the program i.e. the .exe to someone who dosen't have Visual Studio on their machine the app won't work saying it needs something like "mfc40.dll".
My question is, how do I compile my program so that the required stuff is compiled into the .exe???
Any ideas??
Cheers.
|
|
|
|
|
Very easy.
Go to your "Project Settings" for your project, and there will be a setting called "Use of MFC" or something. Change it to "Use MFC in a static libary". Note that you will have to do this for both debug and release configurations.
Then do a complete clean and rebuild. If you need to verify your program is no longer looking for Mfc40.dll or anything, run the Dependency Walker on it.
Now, if you have the crippleware version (er, the Standard version),
this[^] article will help.
"Fish and guests stink in three days." - Benjamin Franlkin
|
|
|
|
|
Why does it work that way b) and not a)?
a)
// SomeClass.h
#ifndef _SOMECLASS_
#define _SOMECLASS_
class some_class
{
public:
some_class(){}
~some_class(){}
template<class t="">
void callfunc(T var);
};
template<class t="">
void some_class::callfunc(T var)
{
}
#endif
b)
// SomeClass.h
#ifndef _SOMECLASS_
#define _SOMECLASS_
class some_class
{
public:
some_class(){}
~some_class(){}
template<class t="">
void callfunc(T var){}
};
#endif
//main.cpp
#include "SomeClass.h"
void main(void)
{
some_class example;
int nt=4;
example.callfunc(nt);
}
|
|
|
|
|
Because templates are expanded as they are compiled - just including the .h with the function declared (not defined) is not enough
This is the pain the arse problem that you usually have. Really, its much easier if you stick to the thin template pattern - short template code. Then, put it in at the same time as decliration - as in b). If you really want to split it up - you can put the defination at the bottom of the .h its declared in. Or, if you really want the defination in a .CPP file - you can (uck) #include the .CPP into the other CPP you are using the template in.
|
|
|
|
|
I am trying to develop an ActiveX control using Visual C++ 6 for usage on a web page which will display info on our Exchange Servers.
The client computers are Win2K & WinXP.
Initially I want this control to be "trivial" and only display the status of the Exchange servers ie up/down. In the future I would like to extend this information to possibly throughput/load etc.
Which API should I use for maximum usability and compatibility ??
Any help or URLs for tutorials on accessing this kind of info from an ActiveX control would be greatly appreciated.
Thanks,
Dels
|
|
|
|
|
Related to my earlier question, when the focus for a dialog is on a Date/Time Picker control, or any 'entry style' control for that matter, how can I make the 'Enter' key act like a data entry key. In other words, I simply want pressing 'Enter' to finalize data entry in that control and possibly send the cursor to the next tab stop.
|
|
|
|
|
Try catching the DTN_WMKEYDOWN notification message. If that doesnt work, try using a message hook for messages like WM_KEYDOWN/WM_KEYUP...
HTH
Bikram Singh
|
|
|
|