I'm trying to read a line from a text document so I can move it into an array. So I've tried to get this to work out and it stops when it sees the "while" line.

void input (ifstream& file, int& end, string word[])
     { int x = 0;
       string line;
       getline (file, line);
       while (line != '/n')
             { line >> word[x];
               x++; }
       end = x - 1;
     }

I understand that it doesn't code in the /n when using getline, but how do I get it to keep doing this until there is nothing left in string line?

I've attempts just getting the line and printing it out right after and that just caused it to crash so I'm at a loss of what to try.

Is there a reason you are using '/n' instead of '\n' ?

Also, is there a defined expression for a string being equivalent to a char? If not then you could try line != "\n" instead, though I don't mess with file I/O in C++ too much so I can't completely confirm this without a brief look-up =P

Edit: After looking here I realized that you might have to encapsulate the "\n" value in a string object before doing the comparison.

Sorry, it is a \n. I'm just tried and frantic so I'm typing weird.

And I tried changing the coding to "\n" and now it's yelling at me about >> not being a valid operator or something like that. It's giving me:
no match for 'operator>>' in 'line >> *((+(((unsigned int)x) * 4u)) + word)'

Really, my problem is that no matter what I try, I can't get just one line of text to break apart into my array so that each array cell contains one word. That's all I need to do and I can't get it to work.

Your problem is that std::strings don't support the >> operator. The geline function takes one line out of your input file and puts it in a string, but to break up that string into words, you should use a stringstream. Here's the corrected code:

void input (ifstream& file, int* end, string word[])
{ 
    int x = 0;
    string line;
    getline (file, line);
    stringstream str(line);

    while (str >> word[x])
    { 
        x++; 
    }
    *end = x - 1;
}

You can also see that I changed the "end" variable to expect an int-pointer.

But instead of using a string-array, you might want to consider vectors. That way you wouldn't have to worry about it's size and the end-var will become obsolete. It also looks alot cleaner :)

void input (ifstream& file, vector<string>* AllWords)
{ 
    string line, word;
    getline (file, line);
    stringstream str(line);
    while (str >> word)
        AllWords->push_back(word);
}

int main()
{   
    ifstream in("c:\\input.txt");
    vector<string> words;
    input(in,&words);
    cout << "The line contains " << words.size()
        << " words. Here they are:\n";
    for (unsigned int i = 0; i < words.size(); i++)
        cout << words[i] << "\n";

    return 0;
}
commented: Excellent =) +4

Yeah, I knew I couldn't use the >> operator, I think I was just too tired to realize I needed to change it. Thanks, you're stringstream worked.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.