CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2001
    Location
    Tree
    Posts
    10

    using >> operator

    How do I write a function, class (not sure what I have to write to use it) so that I can return a value to an array. Example, cin is programmed to use that operator to put a char in or whatever.

    How can I make a function that uses that? Just to demonstrate an example, let's say that I have this:

    int function(char data[10])
    {
    strcat(data," bla bla")

    return data;
    }

    this function would not work since c++ is worse then a nazi when it comes to arrays. Is there a way I can get data using the >> operator? This will help me allot since I have allot of functions I want to write that could use this. Thanks!


  2. #2
    Join Date
    Feb 2001
    Location
    Sydney, Australia
    Posts
    1,909

    Re: using >> operator

    Frankly didn't get why you need a >> operator, may be just you need to use this approach:


    #include <stdio.h>
    #include <string.h>

    char* f(char a[])
    {

    strcat(a, "Appended String\n");
    return a;
    }




    int main()
    {
    char a[128] = "Initial String\n";//large
    printf(a);
    printf(f(a));


    return 0;
    }




    Please - rate answer if it helped you
    It gives me inspiration when I see myself in the top list =)

    Best regards,

    -----------
    Igor Soukhov (Brainbench/Tekmetrics ID:50759)
    [email protected] | ICQ:57404554 | http://soukhov.com

    Russian Software Developer Network http://rsdn.ru
    Best regards,
    Igor Sukhov

    www.sukhov.net

  3. #3
    Join Date
    Nov 2001
    Location
    Tree
    Posts
    10

    Re: using >> operator

    I want to be able to use this way since I can get arrays very quickly once I write a class that can do whatever I want it to do. Instead of going trough all the pointer stuff and return special data types to be able to use more then one char.

    I remembering replying to this for some reason but there's none shown to it so I must of dreamed of it or something!(I think I'm too much on the computer if I start getting dreams like that!) Unless I din't notice and typed it in the e-mail... not in my sent items though. hmm, mistery.


  4. #4
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: using >> operator

    you can use ostrstream class.

    Then you can concatenate a string with << (not >&gt

    So you can do:


    void function( std:strstream& os )
    {
    os << " bla bla";
    }




    You can use this to stream in formatted, the same way as you would any other stream.

    of course you could just use std::string with append or += but that would only allow you to concatenate in strings.

    Your function, by the way, is wrong in two ways. Firstly your passing of data[10] - should really pass char *data.

    Secondly you are returning a char* while your function is declared to return an int.



  5. #5
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: using >> operator

    what arrays exactly?



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured