Like Tree2Likes
  • 2 Post By manasij7479

Practical way to pick a name out of a list?

This is a discussion on Practical way to pick a name out of a list? within the C++ Programming forums, part of the General Programming Boards category; I was planning to create a list of names, and have a program pick one out randomly and print it. ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    34

    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
    Posts
    71
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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.
    R41D3N and King Mir like this.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



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, 09:18 AM
  5. making it practical
    By zbap in forum C++ Programming
    Replies: 2
    Last Post: 03-08-2002, 09:51 AM

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