Make a vector of strings from a .txt file

closed account (EwCjE3v7)
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
  #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();

            } else if(input == "Hint") {
                cout << "\nHere is your hint" << endl;

                char secondLetter = country[1];

                cout << "The second letter is: " << secondLetter << endl;
            } else if (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;
}

I was wondering how would you make a vector from a .txt file


Simply open a file, loop reading each line from the file (assuming one country per line), push the line onto the vector, close the file.

1
2
3
4
5
6
7
8
9
10
void load_countries (vector<string> & countries)
  ifstream  ifs ("countries.txt");
  string temp;

  while (ifs.good())
  { ifs >> temp;
     countries.push_back (temp);
  }
  ifs.close ();
}

closed account (EwCjE3v7)
Sorry I can`t get it to work, could you please edit the code and send it to me. I get errors and I`m lost sorry

Edit:

Fized it

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#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();

            } else if(input == "Hint") {
                cout << "\nHere is your hint" << endl;

                char secondLetter = country[1];

                cout << "The second letter is: " << secondLetter << endl;
            } else if (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;
}
Last edited on
create a text file , name it countries ,and put it in the same project folder,it will work

countries.txt

Afghanistan
Albania
Algeria
Brazil
Cuba
closed account (EwCjE3v7)
Yep got it, Thanks
Topic archived. No new replies allowed.