Thread: Practical way to pick a name out of a list?

  1. #1
    Registered User
    Join Date
    Jul 2009

    Practical way to pick a name out of a list?

    I was planning to create a list of names, and have a program pick one out randomly and print it. Did not sound hard. Just grab some enums and....

    well, .........
    Apparently there is no direct way to get the name of an enum. Is there a practical way to solve the above task? I came up with different very impractical ways, like having my randomly picked number compared in a switch case, which is not cool if you have more than 300+ names to pick from.

  2. #2
    Registered User
    Join Date
    May 2012
    Put the names in .txt file, then put them in a buffer. Pick a rand value and set that value as element index from the buffer.
    This way i used to create a Hangman game.

  3. #3
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Put the names in a std::vector<std::string> container.
    Choose a random number between 0 and the size of that container.
    Use the string at the index.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Practical Programming
    By TwoBabyJedis in forum Programming Book and Product Reviews
    Replies: 3
    Last Post: 08-23-2011, 06:00 PM
  2. Practical C Programming, Third Edition
    By jc99 in forum C Programming
    Replies: 10
    Last Post: 06-19-2009, 09:24 PM
  3. practical programming
    By IXxAlnxXI in forum C++ Programming
    Replies: 5
    Last Post: 05-27-2008, 08:06 AM
  4. A practical help
    By Ducky in forum C++ Programming
    Replies: 9
    Last Post: 12-29-2007, 10:18 AM
  5. making it practical
    By zbap in forum C++ Programming
    Replies: 2
    Last Post: 03-08-2002, 10:51 AM