![]() |
|
|||||||
|
|
Thread Tools | Search this Thread | Rate Thread |
|
#1
|
|||
|
|||
Need help with getting a random function to connect with an array of structsA little background information...
I'm in my first programming class, and we are required to create a "game" in C++ as a final project. My game consists of asking for the capital of a given country. When the question is asked, the player will be given 3 capitals to pick from. If the player guesses correctly, they get a point and the game continues. If the player guesses incorrectly, they get a strike. When the player accumulates 3 strikes, the game is over. Having said all of that, I've written a random function that will randomly select one of the countries I have. There are 130 countries to pick from. My problem is that I don't know how to associate the random function with my countries. With the way my game is set up, my professor said I needed to make my countries and capital choices into an array of structs. CPP / C++ / C Code:
Please.. Someone help me. This is due Wednesday night, and I still have so much left to do. |
|||
|
#2
|
||||
|
||||
Re: Need help with getting a random function to connect with an array of structsYou want a random number from 0 to 129. That number is used as an index into your struct array:
Code:
Keep in mind that once you use a particular random number (say 20), you don't want to use it again. You should have some way to test if rand() gives you a number again. __________________
During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence? |
|
#3
|
||||
|
||||
Re: Need help with getting a random function to connect with an array of structsQuote:
I think that your instructor is probably looking for something like this: CPP / C++ / C Code:
...as the array of structs. I wouldn't embed the "wrong answers" into the structure. You are creating a lot of extra work for yourself. If you want to have a right choice and some number of wrong choices, you only have to pick 3 or 4 random elements out of the structure and then use the "wrong" capitals (indexes into the structure) as your data to display to the user. No sense in storing a bunch of extra data. You already have it! Let me know if you want a hint as to how to accomplish what I'm suggesting. Bob de Mexican |
Recent GIDBlog
Problems with the Navy (Chiefs) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| run script command on ns2.26 | newbie06 | Computer Software Forum - Linux | 65 | 19-Aug-2009 08:50 |
| Need Help with input files. | Efferus | C++ Forum | 2 | 24-Nov-2007 17:19 |
| [Include] Doubly-linked List | dsmith | C Programming Language | 6 | 14-Apr-2006 14:12 |
| Passing Array of Structs to Function | kallen6879 | C++ Forum | 1 | 22-Mar-2006 15:58 |
| Need help deleting the last element in the array | headphone69 | C++ Forum | 2 | 15-Mar-2006 20:31 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The