Can you use seperate files as arrays of stings?

Write your question here.
I was trying to make an program that lets you test your typing speed, but ran in to a problem.
I want to use a seperate file (.txt or sth) to store strings of random words, like an array.

Thank you!
P.s. Im on my phone, so i dont have the current code, but i might put it in later ;).
Here a solution which reads all words from a text file, stores them into a vector and use it to randomly access.
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
#include <fstream>
#include <iostream>
#include <random>
#include <vector>


int main()
{
    // Opens the file:
    std::ifstream fileStream( "someWords.txt");
    if (!fileStream)
    {
        std::cout << "File couldn't opened!\n";
        exit(1);
    }

    // Reads all words from file into a vector: 
    std::vector<std::string> wordList;
    std::string word;
    while (fileStream >> word)
    {
        wordList.push_back(word);
    }

    // Sets up C++ random facilities:    
    std::default_random_engine e( std::random_device{}());
    std::uniform_int_distribution<> d(0, wordList.size()-1);
     
    // print a random word from the vector:
    std::cout << wordList[d(e)];
    
    exit(0);
}
Thanks for the help. :)
Topic archived. No new replies allowed.