• 0

Reading one character at a time from a string/stringstream


Question

So, what's the easiest way to read one character at a time from a string/ stringstream in C++ and display it on the screen.

I know it's easy, but somehow it's eluding me. istream.get()?

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0

If you want to do something a little more personally coded (since I don't really know a lot of C++.NET specifically) you could do something like....

//...
int n = sizeof(mychartext);
for (int pos = 0; pos < n; pos++)
{
    if (mychartext[pos] != ' ') //If the place where the position counter is not "space"
    {
         cout << mychartext[pos]; 
    }
    else if (mychartext[pos] == '\0') //If the place where the position counter is on holds a null value...
    {
         break; //break the loop, as we have reached the end of the string.
    }
    else
    {
         continue; //Do nothing, and allow the for loop to bring position to the next character.
    }
}

feel free to correct me if this is flawed. Hopefully the general idea can give you another idea as to how you could do this. :)

  • Like 1
Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.