reading words from file into string ?!

So I'm trying to read from an input file into a string, where each word is put into a separate element, and each word is seperated by a space, punctuation, or a number.

Like:
hello1i am a,boy.

is five words, hello, i, am, a, and boy.

below is my code, and I know it isn't doing what I have said, but I really need some help getting off the ground. Eventually it needs to find unique word counts given an input string. But for now, any help will do! thanks!!

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
  #include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(){

    ifstream infile;
    string ch[10000];
    int i = 0;

    infile.open("sample.txt");

    while(infile >> ch[i])
    {
      if(ch[i] == " ")
      i++;
      else
      infile >> ch[i];
    }

    infile.close();

    return 0;
}
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
#include <iostream>
#include <string>
#include <vector>
#include <cctype>

int main() {
    const size_t None = size_t(-1);
    std::vector<std::string> words;
    std::string line;
    while (getline(std::cin, line)) {
        size_t start = None;
        for (size_t i = 0; i < line.size(); i++) {
            if (std::isalpha(line[i])) {
                if (start == None)
                    start = i;
            }
            else if (start != None) {
                words.push_back(line.substr(start, i - start));
                start = None;
            }
        }
        if (start != None)
            words.push_back(line.substr(start, line.size() - start));
    }
    for (const auto &w: words)
        std::cout << '[' << w << "]\n";
}

Topic archived. No new replies allowed.