Okay I have updated this code to not use the main() call. But I still would like to know what caused the problem in my old code(below). Do the following
1. Compile and run the code
2. guess the country and wait for it to tell you would you like to play again, the first time type 'y' but once you guess the second country, type 'n'. It should turn of but does not
(This is the code that I`m not using but still want to know the problem.)
#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <time.h>
using std::cout; using std::endl; using std::cin;
using std::vector; using std::string;
int main()
{
vector<string> countries{"Afghanistan", "Brazil", "Cuba"};
unsigned counter = 0;
srand(time(NULL));
int randomIndex = rand() % countries.size();
cout << "We will give you the first and last letter of a country, you have to guess the country. Points will be counted." << endl;
for (auto c : countries[randomIndex]) {
if (counter == 0) {
cout << "First letter is: " << c << endl;
}
elseif (counter == countries[randomIndex].size() - 1) {
cout << "Last letter is: " << c << endl;
}
++counter;
}
string input;
while (cin >> input) {
if (!input.empty()) {
input[0] = toupper(input[0]);
if (input == countries[randomIndex]) {
cout << "Correct!!! Would you like to try again? (y,n)" << endl;
char tryAgain = 'n';
cin >> tryAgain;
if (tryAgain == 'y') {
main();
}
elseif (tryAgain == 'n'){
cout << "Bye!" << endl;
return 0;
}
} else {
cout << "Incorrect!!! Try again." << endl;
}
}
}
return 0;
}