-1

Hello together

void rev_out(istream& is){
char a;
is >> a;
if(a!='g')rev_out(is);
cout << a;
}


int main()
{
stringstream is("abcdefg");
rev_out(is);
return 0;   
}

now the Output is gfedcba, but i have a problem. I`d like to give an universally valid if-statement like "stop after the string is read completely". So if there is any stream you don`t know, the function knows when it has to stop. Is there a possibility without counting the string-elements first?

Thanks for your help!

Suslik
  • 929
  • 8
  • 28

4 Answers4

2

Just stop when you can't read any more:

void rev_out(istream& is){
    char a;
    if (is >> a)          // If we could read a character...
    {
        rev_out(is);      // ... keep reversing ...
        cout << a;        // ... and print the character.
    }
    // Otherwise, nothing happens.
}
molbdnilo
  • 64,751
  • 3
  • 43
  • 82
  • Thank you! can you explain me an other thing? I wonder how the <<-Operator works, if you have: stringstream is("abc") and is << "xyz" what happens? i think the first letter will be overwritten by x the second by y and so on. Is that true? Because i also read the explanation: << adds a string to a stringstream object. – Suslik Nov 25 '14 at 14:19
1

All string literals end with a null character (\0), so you can use that to determine when you've reach the end of an arbitrary string. However, the is >> a; will fail once you've reached that point, so you won't want to do cout << a; on the last recursive call, but that's easy to avoid. You could instead use the failure state of the string stream to determine when to stop.

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
0

I believe this should work, but it's untested.

void rev_out(istream& is){
    char a;
    is >> a;

    if(is.bad() || is.eof()){
        return;
    }
    rev_out(is);
    cout << a;
}

Essentially you check if the end of the stream has been reached before trying to output. If you haven't made it to the end, get the next one, then the output comes out as you fall back through the stack.

David
  • 4,744
  • 5
  • 33
  • 64
  • Put the check after the input... Your current code prints one character too much (one that couldn't be read), if I'm not wrong... And an explanation of your code should be included. – leemes Nov 25 '14 at 12:46
  • Added an explanation. And pretty sure you're wrong about that, see edit. – David Nov 25 '14 at 12:51
  • http://ideone.com/YjLnN2 - Corrected (but not using `eof` but the implicit conversion to bool **after** trying to read) / Or a bit simpler: http://ideone.com/AXb746 – leemes Nov 25 '14 at 12:54
  • Hmm, odd. As I said, untested. :) Nonetheless, I've reversed it now. Much appreciated. – David Nov 25 '14 at 12:56
0

in >> a will try to read a character from in. It may fail. The result of in >> a can be converted to bool to check if it was successful.

You should only perform the recursive call and the output if a character could be read. This can be checked by using in >> a as a condition in an if statement:

void rev_out(istream& is) {
    char a;
    if (is >> a) {
        rev_out(is);
        cout << a;
    }
}

Live demo

Note that it's always a good idea to check the result of such an expression, as you never know if the input maches the pattern you're trying to read, in particular if this pattern is complicated such as when you're reading floating point numbers...

leemes
  • 44,967
  • 21
  • 135
  • 183