1

Ok, its been a while since I've done any file input or string manipulation but what I'm attempting to do is as follows

while(infile >> word) { 
    for(int i = 0; i < word.length(); i++) {
        if(word[i] == '\n') { 
            cout << "Found a new line" << endl; 
            lineNumber++; 
        }

        if(!isalpha(word[i])) {
            word.erase(i);
        }

        if(islower(word[i])) {
            word[i] = toupper(word[i]);         
        }
    }
}

Now I assume this is not working because >> skips the new line character?? If so, whats a better way to do this.

3 Answers 3

9

I'll guess that word is a std::string. When using >>, the first white-space character terminates the 'word' and the next invocation will skip white-space so no white-space while occur in word.

You don't say what you're actually trying to do but for line based input you should consider using the free function std::getline and then splitting each line into words as a separate step.

E.g.

std::string line;
while( std::getline( std::cin, line ) )
{
    // parse line
}
Sign up to request clarification or add additional context in comments.

Comments

1

How about using getline()?

string line;
while(getline(infile, line))
{
    //Parse each line into individual words and do whatever you're going to do with them.
}

Comments

0

There is getline function for istream.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.