guess the capital game

Hi everyone, I'm having some problems here with this code.
The program is running but is not being able to discern between right and wrong answers. I wonder how I could get the strings to work together. I have taken a look at the string tutorial but still am a bit confused.


#include <iostream>
#include <string>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

using namespace std;


char Country [63] [256] = {"Albania",
"Andorra",
"Armenia",
"Austria",
"Azerbaijan",
"Belarus",
"Belgium",
"Bosnia Hezergovina",
"Bulgaria",
"Croatia",
"Cyprus",
"Czech Republic",
"Denmark",
"Estonia",
"Finland",
"France",
"Georgia",
"Germany",
"Greece",
"Hungary",
"Iceland",
"Ireland",
"Italy",
"Kazakhstan",
"Latvia",
"Liechstein",
"Lithuania",
"Luxembourg",
"Macedonia",
"Malta",
"Moldova",
"Monaco",
"Montenegro",
"Netherldands",
"Norway",
"Poland",
"Portugal",
"Romania",
"Russia",
"San Marino",
"Serbia",
"Slovakia",
"Slovenia",
"Spain",
"Sweden",
"Switzerland",
"Turkey",
"Ukraine",
"United Kingdom",
"Vatican City",
"Aland",
"Akrotiri & Dhekelia",
"Faroe Islands",
"Gibraltar",
"Guernsey",
"Isle of man",
"Jersey",
"Abkhazia",
"Kosovo",
"Northern Cyprus",
"South Ossetia",
"Nagorno Karabakh",
"Transnistria",};


char Capital [63] [256] = {"Tirana",
"Andorra la Vella",
"Yerevan",
"Vienna",
"Baku",
"Minsk",
"Brussels",
"Sarajevo",
"Sofia",
"Zagreb",
"Nicosia",
"Prague",
"Copenhagen",
"Tallinn",
"Helsinki",
"Paris"
"Tbilisi",
"Berlin",
"Athens",
"Budapest",
"Reykjavik",
"Dublin",
"Rome",
"Astana",
"Riga",
"Vaduz",
"Vilnius",
"Luxembourg City",
"Skopje",
"Valletta",
"Chisinau",
"Monaco",
"Podgorica",
"Amsterdam",
"Oslo",
"Warsaw",
"Lisbon",
"Bucharest",
"Moscow",
"City of San Marino",
"Belgrade",
"Bratislava",
"Ljubljana",
"Madrid",
"Stockholm",
"Bern",
"Ankara",
"Kiev",
"London",
"Vatican City",
"Mariehamn",
"Episkopi Cantonment",
"Torshvan",
"Gibraltar",
"St.Peter Port",
"Douglas",
"St.Helier",
"Sukhumi",
"Pristina",
"Lefkosa",
"Tskhinvali",
"Stepanakert",
"Tiraspol",};

int main()

{
string name;

srand(time(NULL));
int Rand = rand() % 63;


cout << "Guess the capital of: \n"<< Country[Rand];
cin >> name;

if (name == Capital [63]);
cout << "The answer is correct";


if(name != Capital [63]);
cout << "The answer is incorrect";


}








Ouch. char Country [63] [256] should be const char* Country [63] (and const char* Capital [63]). You can't compare char arrays with ==, you can only do this with std::string. You also have a ; after if, which doesn't belong there.
Don't use magic numbers - define and use const int numberOfCountries=63;

So you need to write below:
1
2
if (name == string(Capital[Rand]))cout << "The answer is correct";
else cout << "The answer is incorrect";


You can avoid the temporary string object by making Capital an array of strings, but that will increase code size.
Edit: name == Capital[Rand] is actually okay, since name is a string and has an overloaded operator== for const char*.
Never try this when both are C strings, though.
Last edited on
hi,disni,

first of all i gotta say that i don't quite understand the function of your program.
but i guess this might be the problem:

if (name == Capital [63]);

in this line the comparison between name and Capital[63] is not correct,as the capital names are stored in a two_dimensional array,the type of capital accordingly is a char* type while name is a string type.
so maybe you can store the names using string Capital[63] instead of char Capital[63][256].
If you use a const char* Country[] array, you don't need to specify the number of elements - the compiler will figure it out. Also, you won't have to define a constant for the number of countries - you can just use this in your main function:

 
unsigned int nCountries = sizeof(Country) / sizeof(char*);


which will give you the number of elements in the array, and you can expand the array to include more countries, without ever having to use a fixed number like "63" in your code.

You have semicolons at the end of your "if" statements. These are "empty" statements. That means that nothing happens after the strings are compared. In your code, both "The answer is correct" and "The answer is incorrect" will be displayed, no matter the input, because the conditional statements (empty) have already been executed. Also, you should use an "if-else" block instead of two "if" statements. This would not cause an error, but it's wasteful because you're doing an extra comparison that doesn't need to take place. That section should look like this:

1
2
3
4
5
6
7
8
if (name == Capital [Rand])
{
    cout << "The answer is correct";
}
else
{
    cout << "The answer is incorrect";
}


Finally, you need to return an "int" value from main(). Use:

 
return 0;


...as your last statement.
Last edited on
Thanks guys, I got it done, somehow I was getting the concept wrong.
Thanks a lot.
Topic archived. No new replies allowed.