Advertisement

Three kinda newbie questions

Started by August 13, 2000 01:10 AM
22 comments, last by Peon 24 years, 4 months ago
Alright, I got three questions that I could use help with: - First of all, after tossing all my QB experience out the window in exchange for C++, I''m stuck doing stupid cout / text stuff now. I''m working on a text based RPG as practice (don''t laugh ) and I need a random number generator or something. I read something about seeds and stuff, but I don''t want to have to seed it, is there a function that will just spit out a random number based on specifed limits? A lot like the "randomize timer" / "rnd" combo in BASIC. - OK, my second question has to do with strings. Say I have something like this: char[20] name; char[20] highscore; blah name = highscore What I want to do is assign whatever name''s value is to highscore. So this is two questions I guess -- am i declaring those variables correctly, and if so, how would I solve the above problem? - Third and final question. OK, can someone explain the advantages to using a library? They sound almost controversial in some cases, but I''m not sure what they are, really. Any help would be appreciated! Alright, thanks in advance Peon
Peon
quote: Original post by Peon
I read something about seeds and stuff, but I don''t want to have to seed it, is there a function that will just spit out a random number based on specifed limits? A lot like the "randomize timer" / "rnd" combo in BASIC.


LOL! That is soooo funny, "Randomize timer" IS a seed, it seeds it to the timer, which makes it random... You can do similar in C++ by using srand(&time); or something like that... hehehe... no seeding! HA!

quote:
- OK, my second question has to do with strings. Say I have something like this:

char[20] name;
char[20] highscore;

blah

name = highscore

What I want to do is assign whatever name''s value is to highscore. So this is two questions I guess -- am i declaring those variables correctly, and if so, how would I solve the above problem?


BASIC was really poor about this, but C++ is stricter. You need to #include <string.h> and then call
strcpy(highscore, name);

I think that is the correct order... next!

quote:
- Third and final question. OK, can someone explain the advantages to using a library? They sound almost controversial in some cases, but I''m not sure what they are, really. Any help would be appreciated!

Alright, thanks in advance

Peon


You NEED libraries. Thats where all of the functions that you use are from. By having lots of libraries, you can choose which ones you need and create smaller more efficient programs. Because you are using cout you are obviosly using a library (I think it is iostream). Common libraries that you link through header files are:

stdio.h
string.h
iostream.h
stdlib.h

Does that answer your questions?

-Chris Bennett ("Insanity" of Dwarfsoft)

Check our site:
http://www.crosswinds.net/~dwarfsoft/
Check out our NPC AI Mailing List :
http://www.egroups.com/group/NPCAI/
made due to popular demand here at GDNet :)
Advertisement
Wow, that was quick. Thanks for your answers, I need to hit myself after the first one, lol I''ll try out what you said. As far as #3 goes, I see what you''re saying. I just never considered include files as libraries but they are I guess, hehe.

Thanks again, I really appreciate it!
Peon
Welcome... Response == so_quick because I lurk in the "active topics" and see everything as soon as it gets posted.

Just check the help files, they really do help. I learned by reading them . Good luck

-Chris Bennett ("Insanity" of Dwarfsoft)

Check our site:
http://www.crosswinds.net/~dwarfsoft/
Check out our NPC AI Mailing List :
http://www.egroups.com/group/NPCAI/
made due to popular demand here at GDNet :)
to seed the random timer do
srand((unsigned)time(0));
and dont forget to #include time.h
you only have to seed the generator once anyhow - painless!

are you generating random int''s for do you want floating point numbers?

this:

inline float g_rnd()
{
return static_cast(rand()) / static_cast(RAND_MAX)
}

will return a float between 0 and 1. not that useful in itself, but can be used in a max/min func like:

inline float g_random(float min, float max)
{
return (max-min) * g_rand() + min;
}

which returns a random floating point number between limits given
Advertisement
Its ok fellas, I sorted it through email .. Just another post closer to #3.. 2 to go

-Chris Bennett ("Insanity" of Dwarfsoft)

Check our site:
http://www.crosswinds.net/~dwarfsoft/
Check out our NPC AI Mailing List :
http://www.egroups.com/group/NPCAI/
made due to popular demand here at GDNet :)
Hm, also Dwarfsofts answer to question 2 is slightly wrong.
(it is the other way round)

If you wanted:

name = highscore

you would do:

strcpy(name,highscore)

GRIN!!!!
FReY
do unto others... and then run like hell.
Dwarf, what the heck are you doing!?!?! You couldn''t have gained all those rankings from normal posts! *sniffs the air* I sense some message posting-deleting here!

lntakitopi@aol.com - http://www.geocities.com/guanajam
Because I''m in a ranting mood I thought I would moan about libraries! By all means, use them but for the most part, the C standard ones are painfully slow because they are so generic and you would be better off making your own functions rather than waste time learning how to use C''s ones. All that happens if you do is get stumped when you need a function that you can''t seem to find and it turns out was never created. Over the years I''ve had to rewrite so much I wish I hadn''t listened when everyone told me libraries were the way to go.

This topic is closed to new replies.

Advertisement