Extraction operator overloading for string input skips space

Answered Extraction operator overloading for string input skips space

  • Thursday, April 26, 2012 10:12 AM
     
      Has Code

    istream& operator>>(istream &input, String &s) { char *data=new char[100]; int i=0; input>>setw(1)>>data[i]; i++; cout<<data[0]; while(data[i-1]!=NULL){ input>>setw(1)>>data[i]; cout<<data[i]; i++; } return input; }

    Here the code should take input from the stream and put it in data[] array. It does so but if i give input "1 2 3". The output that comes is "123".

    Like wise when i put "12 3". The output is "123". I want the istream to give me spaces to, I want to get same output as given by user. How to do that? Is there a way to tell istream to give me spaces etc too.

All Replies

  • Thursday, April 26, 2012 12:27 PM
     
     Answered
    istream& operator>>(istream &input, String &s)
        {
            char *data=new char[100];
            int i=0;
            input>>setw(1)>>data[i];
            i++;
            cout<<data[0];
            while(data[i-1]!=NULL){
                input>>setw(1)>>data[i];
                cout<<data[i];
                i++;
            }
            return input;
    }
    The extraction operator skips white space by default. Try using
     
    input.get(data[i]);
     
    Some other points
     
    1. Your code leaks memory. Instead do
     
    char data[100];
     
    2. Yu are not assigning to your parameter s
     
    3. NULL is the null pointer, not the null character. Use '\0' instead.
     

    David Wilkinson | Visual C++ MVP
  • Thursday, April 26, 2012 7:30 PM
     
      Has Code

    In your example, what happens if there are more than 100 characters in the input stream? As David already pointed out you are leaking memory.

    If you want to read in a line of string characters the usual method is

    istream &operator>>(istream &input, string &s)
    {
      return getline( input, s );
    }

    However, this terminates at a typical line terminator. In your example you consume data until there is no more. It would be easy to modify the above code to do so.

  • Sunday, April 29, 2012 1:32 PM
     
     

    thanks that does work. And yup i was intentionaly skipping the part of asigning my inputed data to String s.

    and thanks for the points.


    But the NULL also points to '\0' so whats the difference.
    • Edited by RANA TALLAL JAVED Sunday, April 29, 2012 1:55 PM want to know NULL and \0 difference
    •  
  • Sunday, April 29, 2012 2:18 PM
     
     

    thanks that does work. And yup i was intentionaly skipping the part of asigning my inputed data to String s.

    and thanks for the points.


    But the NULL also points to '\0' so whats the difference.

    NULL does not point anywhere. The difference is

    1. Because NULL might not actually be 0 on some systems

    2. Because it makes your code clearer if you distinguish:

    (a) a zero integer (0)

    (b) a null character ('\0')

    (c) the null pointer (NULL, or better the new nullptr)

    Actually some people advocate using 0 for the null pointer, rather than NULL, but nobody recommends using NULL in place of '\0'.


    David Wilkinson | Visual C++ MVP

  • Sunday, April 29, 2012 7:13 PM
     
     
    >>Is there a way to tell istream to give me spaces etc too.

    >The extraction operator skips white space by default.

    By default the skipws flag is on, which causes the
    extraction operator to skip *leading* whitespace
    characters. This can be changed. e.g. -

    istream& operator>>(istream &input, String &s)
      {
      char *data=new char[100];
      int i=0;
      input >> noskipws; // <<---
      input>>setw(1)>>data[i];
    ...

    - Wayne

  • Sunday, April 29, 2012 7:17 PM
     
     
    Note change in my last post.

    From:

    cin >> noskipws; // <<---

    To:

    input >> noskipws; // <<---

    - Wayne
  • Sunday, April 29, 2012 7:39 PM
     
     
    Got it. Changed my code to use '\0' instead of NULL. Thanks :)
  • Sunday, April 29, 2012 8:08 PM
     
     
    >char *data=new char[100];

    Note that this allocation will be uninitialized.
    In a Release build it will contain random data,
    whatever is in that area of the heap at run time.
    In a Debug build it will be preset to a special
    character pattern which facilitates the debugger's
    tracking of memory overwrites.

    To initialize it to binary zeros:

    char *data=new char[100]();

    The same with this:

    char data[100];

    As a local variable it will be uninitialized in
    Release builds and will contain what's in that
    area of the stack at run time. It will be preset
    to a special value in Debug builds.

    To initialize it to binary zeros:

    char data[100] = {0}; // C and C++

    Or:

    char data[100] = {}; // C++ only

    - Wayne