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