How to store words from the same text document into a char and a string array?

This is what I currently have


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
    while(!inFile2.eof())
    {
        inFile2.getline(line, 256);


        char *pch = strstr(line, "//");
        if (pch!= nullptr)
            *pch = '\0';
        ptr = strtok(line, ",.-';?()+*\%$#!\"@^&<>[]{} ");



        while (ptr != 0)
        {
            cout << ptr << endl;
            ptr = strtok(NULL, ",.-';?()+*\%$#!\"@^&<>[]{} ");


        }


    }



Here I read the file into a char array(and then parse it).
How can I read the same file into a string array?

I tried doing

x=0
string text
string stringline[256];
while (inFile2 >> text)
{
stringline[x] = text;
x++
}

but my program keeps crashing at this part when I run it

thanks for any help!
Last edited on
Why did you open a new topic for this, you already have two topics for this same basic problem. I suggest you use one of those other topics instead of continually opening new topics for the same basic problem.

Ps. You need to re-read the instructions provided in your original topic, you're not supposed to be using C++ strings in this assignment.
Last edited on
My apologies, I will do that next time.

Can I ask where it says I can't use C++ strings? All I see is "you may not use the stringstream classes for this assignment".
I'd say on the first line of the assignment.

The purpose of this assignment is to give you practice using dynamic memory allocation, c-string functions, sorting, and searching. You will write a spell checking program and test it.
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <fstream>
#include <iterator>
#include <vector>
using namespace std;

int main()
{
   ifstream in( "in.txt" );
   vector<string> myWords( istream_iterator<string>{ in }, {} );

   for ( string s : myWords ) cout << s << '\n';
}


in.txt:
Mary had a little lamb,
its fleece was white as snow;
and everywhere that Mary went
the lamb was sure to go.


Output:
Mary
had
a
little
lamb,
its
fleece
was
white
as
snow;
and
everywhere
that
Mary
went
the
lamb
was
sure
to
go.
Nice code, too bad it isn't even close to following the assignment directions.

Topic archived. No new replies allowed.