|
I want to generate a number in a random form. 1-10. with the number...
That's how you do this:
#include "stdafx.h"
#include <afx.h>
#include <stdlib.h>
void main()
{
srand(0);
while (1)
{
int rnd = rand() % 10 + 1;
if (rnd < 1 || rnd > 10)
{
printf("Out of range [1-10], rnd = %d\n", rnd);
return;
}
else
{
printf("generated rnd = %d\n", rnd);
Sleep(500);
}
}
}
|
|
|
|
|
lets see,... could you do...
Serge Krynine wrote:
#include "stdafx.h" "could this be stdlib.h?"
#include
#include
void main()
{
srand(0);
while (1) "what does this do?"
{
int rnd = rand() % 10 + 1; "Do you have to do 10 + 1, or can you do 11?"
if (rnd < 1 || rnd > 10)
{
printf("Out of range [1-10], rnd = %d\n", rnd);
return;
}
else
{
printf("generated rnd = %d\n", rnd);
Sleep(500);
}
}
}
I got most of that, thanks, Ill try it out and see what happends.
~SilverShalkin
|
|
|
|
|
SilverShalkin wrote:
#include "stdafx.h" "could this be stdlib.h?"
Actually stdafx.h is the VC++ precompiled header file
SilverShalkin wrote:
while (1) "what does this do?"
that's for an endless loop
Nish
[Signature temporarily down]
|
|
|
|
|
a) Stdafx was generated by devstudio when I build a windows console app project, but yes stdio.h should do.
b) while (1) {} means “loop forever”; The idea of the test that if a random number would be out of [1-10], the test would exit; otherwise it will run forever (what I guarantee) (until you terminate it by ctrl+c or power off your PC).
c) the expression int rnd = rand() % 10; gives you random integers in the range [0-9] (from zero to nine inclusive). If you want the [1-10] range, add 1 then: int rnd = rand() % 10 + 1; Note that expression: int rnd = rand() % 11 does not give you correct range [1-10] because it can generate zero when the rand() function returns 11, so 11 % 11 gives you null.
Enjoy,
Serge
|
|
|
|
|
The two #include statements with nothing besides them are the clue - the original poster forgot to tick 'no HTML' and so the includes in < > were lost.
I think no-one has bothered to explain the % operator - it means the remainder and can be used anywhere. So rand() generates a number between 0 and rand_max(). The remainer operator says 'what's the remainder if I divide by this', so if the number divides evenly by 10 in my example, I get a 0, and a value of 10 is not possible, because the highest number that can remain if I divide by 10 is 9, as in 29/10 = 2, 9 remainder.
The point of the endless loop I think is that if you ran it, you would never see 'out of range' come up, no matter how long you waited....
Christian
The tragedy of cyberspace - that so much can travel so far, and yet mean so little.
"I'm thinking of getting married for companionship and so I have someone to cook and clean." - Martin Marvinski, 6/3/2002
|
|
|
|
|
Thats cool, while (1) is a forever loop, i just wrote:
int x=1;
do
{
}
while (x == 1)
but i gues we all can see what one is faster.
Christian Graus wrote:
rand() generates a number between 0 and rand_max().
rand_max, how do you set how big it is? 0-rand_max right?
Well, thanks a bunch, I'm on the tip of the breakthrough, "Even though its not much of a break through "
~SilverSahlkin
Tragedy is Romeo and Julyet, not a syntax error on your if statement!
|
|
|
|
|
Yes, yours is doing the same thing in a more convoluted manner.
SilverShalkin wrote:
rand_max, how do you set how big it is? 0-rand_max right?
You don't set RAND_MAX, the system does. Changing the value of the #define might break other code, but it won't change the range of rand(). That is why you need to divide by RAND_MAX and multiply by a maximum, or use the % operator.
Christian
The tragedy of cyberspace - that so much can travel so far, and yet mean so little.
"I'm thinking of getting married for companionship and so I have someone to cook and clean." - Martin Marvinski, 6/3/2002
|
|
|
|
|
1.I create a atl dll project, then i insert a composite atl object in my project.
2.I insert a tree control into the dialog which are belong to the composite object.
3.it seems to be ok when a test it in the vb entironment, but ,when I build the vb proect the exe directly, it meeting an Assert error in atlctl.h at line 842.
Could somebody tell me that's why?
Thank's a lot
i am realfly
|
|
|
|
|
Did you call InitCommonControlsEx to initialize the tree control?
|
|
|
|
|
That's it!
Thank you!
i ma realfly
|
|
|
|
|
Hi All,
I'm making an MFC Extension DLL and when I try and use it in my project it wont update the members I have created to the Auto popup/complete feature of Visual C++. It has all the members for the Derived class (CObject) and the destructor for the new class, but none the new member variables/functions?
Would I need to change some setting to make it work? And where, the DLL project or the Application. Is this even possible??
When I manually type the function anyway it works, but I like this feature as it often highlights if I've made a simple error somewhere, plus I'm lazy and like to do less typing!
Thanks
-Mark
|
|
|
|
|
The autocomplete feature of VS is a bit hit and miss. I've found if I include the DLL in the same workspace as the project it is a little more reliable.
If you want to do less typing and have a little cash to spare - have a look at the Visual Assist addon for VC. There should be an ad floating around CodeProject somewhere (I can't remember the URL). It's certainly worth the investment.
Michael
|
|
|
|
|
I'm trying to find out why the program doesn't sleep for 2 seconds after the first cout.
#include <iostream.h>
#include <windows.h>
main()
{
cout << "blah\n";
Sleep(2000);
cout << "blah\n";
return 0;
}
|
|
|
|
|
Try this:
main()
{
cout << "blah\n" << endl;
Sleep(2000);
cout << "blah\n";
return 0;
}
|
|
|
|
|
Thanks, endl worked. I can't believe it was as simple as that and I couldn't figure it out. I didn't know that endl was different than \n.
|
|
|
|
|
endl also does a flush.
Christian
The tragedy of cyberspace - that so much can travel so far, and yet mean so little.
"I'm thinking of getting married for companionship and so I have someone to cook and clean." - Martin Marvinski, 6/3/2002
|
|
|
|
|
Well, im not used to the sleep command, but i am used to a command like it.
you can use the clock_t command. It requires the header time.h
#include iostream
#include time.h
using namespace std;
clock_t wait // declares wait as clock_t, like declaring an int.
void main()
{
wait = clock() + 2000;
cout << "Blah\n";
if (wait <= clock() )
{
Cout << "Blah";
}
}
Hope that helps. And it may not be all completly true, but im 99% sure it will work.
Good Luck
~SilverShalkin
Stay at the hights of the language for it will grow ever more, with non stop.
|
|
|
|
|
|
I was wondering, how can I parse html with let's say C?
I want to parse a form so that I can read in the variables of the Name, Email and Message of my guestbook and then POST them.
|
|
|
|
|
You're a sadist! I mean it's not impossible, and I'm sure there are already libraries that exist for this sort of thing, but I recommend using Perl, ASP, PHP, etc... These scripting languages have very convenient string maninuplation facilities built into them, and they integrate really well with existing web servers.
Jon Sagara
What about ?
Sonork ID: 100.9999 jonsagara
|
|
|
|
|
Hehe I know... it's hard to do... but I want to do it nevertheless.
regexp here I come!!!
|
|
|
|
|
Well in that case, check out these free scripts. And may the force be with you.
Jon Sagara
What about ?
Sonork ID: 100.9999 jonsagara
|
|
|
|
|
Jon Sagara wrote:
You're a sadist
The word you probably intended to use was 'masochist'
Nish
[Signature temporarily down]
|
|
|
|
|
Thank you, Mr. Pedantic. Will that be all?
Jon Sagara
What about ?
Sonork ID: 100.9999 jonsagara
|
|
|
|
|
Jon Sagara wrote:
Thank you, Mr. Pedantic. Will that be all?
LOL
Nish
[Signature temporarily down]
|
|
|
|