GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 09-Dec-2008, 18:49
anbrick anbrick is offline
New Member
 
Join Date: Dec 2008
Posts: 1
anbrick is on a distinguished road
Exclamation

Need help with getting a random function to connect with an array of structs


A 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:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <stdlib.h>
#include <sys/time.h>
using namespace std;

struct Question {
  string country;
  string capital1;
  string capital2;
  string capital3;
  short correctAnswer;
};

Question QL[150]= {
                    {"Afghanistan", "Brussels", "Moroni", "Kabul", 3},
                    {"Albania", "Tirane", "Gaborone", "Brasilia", 1},
                    {"Algeria", "Algiers", "Jakarta", "Zagreb", 3},
                    {"Angola", "San Salvador", "Luanda", "Malabo", 2},
                    {"Argentina", "Buenos Aires", "Managua", "Lilongwe", 1}
};

int QLSize = sizeof(QL);

void displayQuestion(Question QL) {
   cout << "What is the captital of " << QL.country << "? " << endl;
   cout << endl;
   cout << endl;
   cout << "1. " << QL.capital1 << endl;
   cout << endl;
   cout << "2. " << QL.capital2 << endl;
   cout << endl;
   cout << "3. " << QL.capital3 << endl;
}

void setuprand() {
  struct timeval tv; struct timezone tz;
  gettimeofday(&tv, &tz);
  srand(tv.tv_usec);
}

double random(double m, double n) {
 double  ans = m + ((n - m + 1) * (rand() / (RAND_MAX + 1.0)));
 return static_cast<int>(ans); 
}

int main() {

 return 0;
}


Please.. Someone help me. This is due Wednesday night, and I still have so much left to do.
  #2  
Old 09-Dec-2008, 21:51
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Need help with getting a random function to connect with an array of structs


You want a random number from 0 to 129. That number is used as an index into your struct array:

Code:
num = rand()... QL[num] is the structure of the random country.

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  
Old 15-Dec-2008, 05:36
Mexican Bob's Avatar
Mexican Bob Mexican Bob is offline
Member
 
Join Date: Mar 2008
Location: Chicxulub, Yucatán
Posts: 226
Mexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the rough

Re: Need help with getting a random function to connect with an array of structs


Quote:
Originally Posted by anbrick
Please.. Someone help me. This is due Wednesday night, and I still have so much left to do.

I think that your instructor is probably looking for something like this:

CPP / C++ / C Code:
#include <iostream>

typedef struct st_capitals
{
    char* country;
    char* capital;
} CAPITALS;

const CAPITALS capitals[] = 
{{"Afghanistan", "Kabul"},
{"Albania", "Tirane"},
{"Algeria", "Algiers"},
{"Andorra", "Andorra la Vella"},
{"Angola", "Luanda"},
{"Antigua and Barbuda", "Saint John's"},
{"Argentina", "Buenos Aires"},
{"Armenia", "Yerevan"},
{"Australia", "Canberra"},
{"Austria", "Vienna"},
{"Azerbaijan", "Baku"},
{"The Bahamas", "Nassau"},
{"Bahrain", "Manama"},
{"Bangladesh", "Dhaka"},
{"Barbados", "Bridgetown"},
{"Belarus", "Minsk"},
{"Belgium", "Brussels"},
{"Belize", "Belmopan"},
{"Benin", "Porto-Novo"},
{"Bhutan", "Thimphu"},
{"Bolivia", "La Paz"},
{"Bosnia and Herzegovina", "Sarajevo"},
{"Botswana", "Gaborone"},
{"Brazil", "Brasilia"},
{"Brunei", "Bandar Seri Begawan"},
{"Bulgaria", "Sofia"},
{"Burkina", "Faso - Ouagadougou"},
{"Burundi", "Bujumbura"},
{"Cambodia", "Phnom Penh"},
{"Cameroon", "Yaounde"},
{"Canada", "Ottawa"},
{"Cape Verde", "Praia"},
{"Central African Republic", "Bangui"},
{"Chad", "N'Djamena"},
{"Chile", "Santiago"},
{"China", "Beijing"},
{"Colombia", "Bogota"},
{"Comoros", "Moroni"},
{"Congo - Republic of the", "Brazzaville"},
{"Congo - Democratic Republic of the", "Kinshasa"},
{"Costa Rica", "San Jose"},
{"Cote d'Ivoire", "Yamoussoukro"},
{"Croatia", "Zagreb"},
{"Cuba", "Havana"},
{"Cyprus", "Nicosia"},
{"Czech Republic", "Prague"},
{"Denmark", "Copenhagen"},
{"Djibouti", "Djibouti"},
{"Dominica", "Roseau"},
{"Dominican Republic", "Santo Domingo"},
{"East Timor (Timor-Leste)", "Dili"},
{"Ecuador", "Quito"},
{"Egypt", "Cairo"},
{"El Salvador", "San Salvador"},
{"Equatorial Guinea", "Malabo"},
{"Eritrea", "Asmara"},
{"Estonia", "Tallinn"},
{"Ethiopia", "Addis Ababa"},
{"Fiji", "Suva"},
{"Finland", "Helsinki"},
{"France", "Paris"},
{"Gabon", "Libreville"},
{"The Gambia", "Banjul"},
{"Georgia", "Tbilisi"},
{"Germany", "Berlin"},
{"Ghana", "Accra"},
{"Greece", "Athens"},
{"Grenada", "Saint George's"},
{"Guatemala", "Guatemala City"},
{"Guinea", "Conakry"},
{"Guinea-Bissau", "Bissau"},
{"Guyana", "Georgetown"},
{"Haiti", "Port-au-Prince"},
{"Honduras", "Tegucigalpa"},
{"Hungary", "Budapest"},
{"Iceland", "Reykjavik"},
{"India", "New Delhi"},
{"Indonesia", "Jakarta"},
{"Iran", "Tehran"},
{"Iraq", "Baghdad"},
{"Ireland", "Dublin"},
{"Israel", "Jerusalem"},
{"Italy", "Rome"},
{"Jamaica", "Kingston"},
{"Japan", "Tokyo"},
{"Jordan", "Amman"},
{"Kazakhstan", "Astana"},
{"Kenya", "Nairobi"},
{"Kiribati", "Tarawa Atoll"},
{"Korea, North", "Pyongyang"},
{"Korea, South", "Seoul"},
{"Kosovo", "Pristina"},
{"Kuwait", "Kuwait City"},
{"Kyrgyzstan", "Bishkek"},
{"Laos", "Vientiane"},
{"Latvia", "Riga"},
{"Lebanon", "Beirut"},
{"Lesotho", "Maseru"},
{"Liberia", "Monrovia"},
{"Libya", "Tripoli"},
{"Liechtenstein", "Vaduz"},
{"Lithuania", "Vilnius"},
{"Luxembourg", "Luxembourg"},
{"Macedonia", "Skopje"},
{"Madagascar", "Antananarivo"},
{"Malawi", "Lilongwe"},
{"Malaysia", "Kuala Lumpur"},
{"Maldives", "Male"},
{"Mali", "Bamako"},
{"Malta", "Valletta"},
{"Marshall Islands", "Majuro"},
{"Mauritania", "Nouakchott"},
{"Mauritius", "Port Louis"},
{"Mexico", "Mexico City"},
{"Micronesia - Federated States of", "Palikir"},
{"Moldova", "Chisinau"},
{"Monaco", "Monaco"},
{"Mongolia", "Ulaanbaatar"},
{"Montenegro", "Podgorica"},
{"Morocco", "Rabat"},
{"Mozambique", "Maputo"},
{"Myanmar (Burma)", "Rangoon (Yangon)"},
{"Namibia", "Windhoek"},
{"Nauru", "no official capital; government offices in Yaren District"},
{"Nepal", "Kathmandu"},
{"Netherlands", "Amsterdam"},
{"New Zealand", "Wellington"},
{"Nicaragua", "Managua"},
{"Niger", "Niamey"},
{"Nigeria", "Abuja"},
{"Norway", "Oslo"},
{"Oman", "Muscat"},
{"Pakistan", "Islamabad"},
{"Palau", "Melekeok"},
{"Panama", "Panama City"},
{"Papua New Guinea", "Port Moresby"},
{"Paraguay", "Asuncion"},
{"Peru", "Lima"},
{"Philippines", "Manila"},
{"Poland", "Warsaw"},
{"Portugal", "Lisbon"},
{"Qatar", "Doha"},
{"Romania", "Bucharest"},
{"Russia", "Moscow"},
{"Rwanda", "Kigali"},
{"Saint Kitts and Nevis", "Basseterre"},
{"Saint Lucia", "Castries"},
{"Saint Vincent and the Grenadines", "Kingstown"},
{"Samoa", "Apia"},
{"San Marino", "San Marino"},
{"Sao Tome and Principe", "Sao Tome"},
{"Saudi Arabia", "Riyadh"},
{"Senegal", "Dakar"},
{"Serbia", "Belgrade"},
{"Seychelles", "Victoria"},
{"Sierra Leone", "Freetown"},
{"Singapore", "Singapore"},
{"Slovakia", "Bratislava"},
{"Slovenia", "Ljubljana"},
{"Solomon Islands", "Honiara"},
{"Somalia", "Mogadishu"},
{"South Africa", "Pretoria"},
{"Spain", "Madrid"},
{"Sri Lanka", "Colombo"},
{"Sudan", "Khartoum"},
{"Suriname", "Paramaribo"},
{"Swaziland", "Mbabane"},
{"Sweden", "Stockholm"},
{"Switzerland", "Bern"},
{"Syria", "Damascus"},
{"Taiwan", "Taipei"},
{"Tajikistan", "Dushanbe"},
{"Tanzania", "Dar es Salaam"},
{"Thailand", "Bangkok"},
{"Togo", "Lome"},
{"Tonga", "Nuku'alofa"},
{"Trinidad and Tobago", "Port-of-Spain"},
{"Tunisia", "Tunis"},
{"Turkey", "Ankara"},
{"Turkmenistan", "Ashgabat"},
{"Tuvalu", "Vaiaku village, Funafuti province"},
{"Uganda", "Kampala"},
{"Ukraine", "Kyiv"},
{"United Arab Emirates", "Abu Dhabi"},
{"United Kingdom", "London"},
{"United States of America", "Washington D.C."},
{"Uruguay", "Montevideo"},
{"Uzbekistan", "Tashkent"},
{"Vanuatu", "Port-Vila"},
{"Vatican City", "Vatican City"},
{"Venezuela", "Caracas"},
{"Vietnam", "Hanoi"},
{"Yemen", "Sanaa"},
{"Zambia", "Lusaka"},
{"Zimbabwe", "Harare"}};

int main()
{
    for( size_t i = 0; i < sizeof(capitals)/sizeof(CAPITALS); i++ )
    {
        std::cout << "Country: " << capitals[i].country << "\nCapital: " << capitals[i].capital << "\n" << std::endl;
    }
    return 0;
}


...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 GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 09:26.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.