fscanf

This is a discussion on fscanf within the C Programming forums, part of the General Programming Boards category; Hi, I started learning C 4 weeks ago at University and part of the first piece of coursework requires a ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    329

    fscanf

    Hi,

    I started learning C 4 weeks ago at University and part of the first piece of coursework requires a file to be scanned in and random words to be selected from that list. I am comfortable with scanning in the file, however I am struggling with selecting a random word.

    Would it be better to select a random word from the file and then to store that into an array, or store all words (10 of them) into seperate arrays and then select a random array?

    Oh....its for a hangman game!

    Thanks

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'd read all the words into an array, and then select randomly from the array.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,729
    Quote Originally Posted by darren78
    Would it be better to select a random word from the file and then to store that into an array, or store all words (10 of them) into seperate arrays and then select a random array?
    I assume that by "array" you mean "array of strings", since a string is a null terminated array of char. If so, then it makes most sense to either read in all the words into an array and then select one at random, or if you know the number of words, then you can select one at random, and then read in until you get the selected word (discarding the other words read in).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Wow, that was quick thanks. Do you guys know of any resources online that can help me with randomly selecting the words? My lecturer hasn't really gone into too much depth on this and I can't find what i'm looking for in any of my books.

    Thanks

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Use rand() and srand(). Try http://www.eternallyconfuzzled.com for details of how to use them.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,729
    Read cppreference.com's entry on rand() and srand() from <stdlib.h>, and Prelude's article on using rand().
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Thanks guys....will check them out now

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by laserlight View Post
    Read cppreference.com's entry on rand() and srand() from <stdlib.h>, and Prelude's article on using rand().
    We think alike... I must have been on this forum too long ... ;-)

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Yes...it worked!!!!!

    I have set up an array of strings and by using the Rand() function I can print any random word from the text file I am using.

    I now need to use the random word in a game of hangman. Am I right in thinking that I would need to set up a new char array to store the selected random word so that the user can select single characters within that word to play the game, or can this be done direct from the array of strings?

    Thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf causes a SEGMENTATION FAULT
    By yougene in forum C Programming
    Replies: 15
    Last Post: 12-28-2008, 11:11 PM
  2. fscanf in different functions for the same file
    By bchan90 in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 08:31 PM
  3. fscanf causing a crash
    By dougwilliams in forum C Programming
    Replies: 6
    Last Post: 11-18-2007, 03:52 PM
  4. fscanf problem in C
    By kepler in forum C Programming
    Replies: 6
    Last Post: 09-30-2003, 06:24 AM
  5. fscanf on sun's
    By brif in forum C Programming
    Replies: 2
    Last Post: 04-14-2002, 01:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21