Hello there, I was wondering how would you make a vector from a .txt file. I need it for this country game. I would like to import country names form a .txt.
Here is the code
#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;
string game() // the main game
{
vector<string> countries{"Afghanistan", "Albania", "Algeria", "Brazil", "Cuba"}; // Our countries
srand(time(NULL));
int randomIndex = rand() % countries.size(); // choose a random country
auto firstLetter = countries[randomIndex].cbegin(), lastLetter = countries[randomIndex].cend() -1; // iterators to first and last letter
cout << "First letter is: " << *firstLetter << endl;
cout << "Last letter is: " << *lastLetter << endl;
return countries[randomIndex];
}
int main()
{
unsigned points = 0, tries = 0;
cout << "Points:" << points << " Tries:" << tries
<< "\n--------------------------------------------------------------------------------\n"
<< "We will give you the first and last letter of a country,\nyou have to guess the country. Points will be counted. \nYou can get a hint by typing \"hint\", or give up by typing \"give up\"\n" << endl;
string country = game();
string input;
while (getline(cin, input)) {
if (!input.empty()) {
input[0] = toupper(input[0]);
if (input == country) {
cout << "Correct!!!" << endl;
++points;
++tries;
cout << string(8,'\n') << "Points:" << points << " Tries:" << tries
<< "\n--------------------------------------------------------------------------------\n" << endl;
country = game();
} elseif(input == "Hint") {
cout << "\nHere is your hint" << endl;
char secondLetter = country[1];
cout << "The second letter is: " << secondLetter << endl;
} elseif (input == "Give up") {
cout << string(8,'\n') << "Points:" << points << " Tries:" << tries
<< "\n--------------------------------------------------------------------------------\n"
<< "The country was: " << country << "\nTry another one. \n" << endl;
country = game();
} else {
++tries;
cout << string(8,'\n') << "Incorrect!!! Try again.\n"
<< "Points:" << points << " Tries:" << tries
<< "\n--------------------------------------------------------------------------------\n" << endl;
}
}
}
return 0;
}
#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <time.h>
#include <fstream>
using std::cout; using std::endl; using std::cin;
using std::vector; using std::string;
vector<string> load_countries ()
{
vector<string> countries;
std::ifstream ifs ("countries.txt", std::ifstream::in);
string temp;
while (ifs.good()){
ifs >> temp;
countries.push_back (temp);
}
ifs.close ();
cout << countries[0];
return countries;
}
string game() // the main game
{
vector<string> countries = load_countries();
srand(time(NULL));
int randomIndex = rand() % countries.size(); // choose a random country
auto firstLetter = countries[randomIndex].cbegin(), lastLetter = countries[randomIndex].cend() -1; // iterators to first and last letter
cout << "First letter is: " << *firstLetter << endl;
cout << "Last letter is: " << *lastLetter << endl;
return countries[randomIndex];
}
int main()
{
unsigned points = 0, tries = 0;
cout << "Points:" << points << " Tries:" << tries
<< "\n--------------------------------------------------------------------------------\n"
<< "We will give you the first and last letter of a country,\nyou have to guess the country. Points will be counted. \nYou can get a hint by typing \"hint\", or give up by typing \"give up\"\n" << endl;
string country = game();
string input;
while (getline(cin, input)) {
if (!input.empty()) {
input[0] = toupper(input[0]);
if (input == country) {
cout << "Correct!!!" << endl;
++points;
++tries;
cout << string(8,'\n') << "Points:" << points << " Tries:" << tries
<< "\n--------------------------------------------------------------------------------\n" << endl;
country = game();
} elseif(input == "Hint") {
cout << "\nHere is your hint" << endl;
char secondLetter = country[1];
cout << "The second letter is: " << secondLetter << endl;
} elseif (input == "Give up") {
cout << string(8,'\n') << "Points:" << points << " Tries:" << tries
<< "\n--------------------------------------------------------------------------------\n"
<< "The country was: " << country << "\nTry another one. \n" << endl;
country = game();
} else {
++tries;
cout << string(8,'\n') << "Incorrect!!! Try again.\n"
<< "Points:" << points << " Tries:" << tries
<< "\n--------------------------------------------------------------------------------\n" << endl;
}
}
}
return 0;
}