Thread: how can i do this in c ?

  1. #1
    Registered User dune911's Avatar
    Join Date
    Sep 2001
    Posts
    140

    how can i do this in c ?

    hi!

    my program should read "words" out of a main-database and then print between 1 and 10 "words" out of the main-db with a randomizer function on the screen.

    my ways:

    a) an external file, which my program can read. but in this way the main-db can be "harmed" by other people. that is not what i want it to be...

    b) is there a way to include the whole main-db somewhere in my main-code? maybe direct in the main-code.c oder in an additional.c file? maybe a .h file?

    if you can help me, please use plain c code without c++ extensions.

    the main-program is already done. there is a main-menu from where you can start two functions and also end the program. function 1 should read a number between 1 and 10 and then print between 1 and 10 "words" out of the main-db on the screen.

    thanks alot for any help!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > b) is there a way to include the whole main-db somewhere in my main-code?
    If your file looks like this, then yes
    Code:
    "word1",
    "word2",
    "word3", "word4",
    /* etc */
    If your word file is not in this form, then you need to implement some mechanism to do this.

    On linux say, I would think about using either sed or awk to do this formatting for me.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User dune911's Avatar
    Join Date
    Sep 2001
    Posts
    140
    hi salem!

    my list would be something like this:

    Word001
    Word002
    Word003
    Word004
    Word005
    ...

    the words are "names". i want to program a name-generator for rpg-names. the data-type must be case sensitive (but i think it is).

    can you tell me more about store that kind of information in my code, so nobody can change it?

  4. #4
    Registered User Engineer's Avatar
    Join Date
    Oct 2001
    Posts
    125
    if you have constant word that never change, you can hard code them in your code:

    char db[10][4] = { "aaa", "bbb", "ccc", ... "kkk" };

    and then use the character array db as your database.
    1 rule of the Samurai Code: if you have nothing to say, don't say anything at all!

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > Word001
    > Word002
    So long as you've got the double quotes and commas in place, then you can do this

    Code:
    char *db[] = {
    #include "word-list.h"
    };
    Where word-list.h contains (you guessed it ) a word list, formatted as described.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User dune911's Avatar
    Join Date
    Sep 2001
    Posts
    140
    there is not always the same word...

    for an example my wordlist could be:

    {
    Salem;
    dune911;
    Engineer;
    }

    and by pressing [1] in the main menu the code should output one of this three names random on the screen.

    i want to know, how to include the worldlist in my program and how to read the entries of my worldlist random by a function...

    man, this sounds really hard to me *sigh*

  7. #7
    Registered User dune911's Avatar
    Join Date
    Sep 2001
    Posts
    140
    hi salem,

    i haven't worked with pointers, yet...
    do you think i should better read my books first before asking "wide above"-stuff on this board?

    if you don't think so, can you post a sample code of a function which gets a random name out of my list.h and print it on the screen? *damn* and, how exactly does my list.h have to look like?

    (how can sourcecode can be displayed in my posts??)

    > Word1;
    > Word2;

    ?

  8. #8
    Registered User dune911's Avatar
    Join Date
    Sep 2001
    Posts
    140
    salem,

    i forgot a little something:

    please don't think i want you to make my homework... i can not imagine how many time you may need to write a sample code for me and also i don't want you to take so much time for it.

    but i will be happy if you can help me a little bit...
    it's better to RTFM first, huh?

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The answer is fairly simple: Given an array of Z size, you can grab a random item from the array by simply doing:

    srand( time( 0 ) ); //only called once per program
    item = array[rand()%size];


    Quzah.

  10. #10
    Registered User dune911's Avatar
    Join Date
    Sep 2001
    Posts
    140
    sorry...
    i think i should really better read something about pointers and things like this in my books before asking on a professional message board like this one...
    actually i cannot "understand" anything you're posting here. damn, i'm a newbie! *sigh*

    thank you for your help! i will print this thread, maybe i find something that helps me with your examples in my books...

Popular pages Recent additions subscribe to a feed