Click to See Complete Forum and Search --> : using >> operator
squirrel
November 28th, 2001, 06:22 PM
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!
Igor Soukhov
November 29th, 2001, 01:50 AM
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)
igor@soukhov.com | ICQ:57404554 | http://soukhov.com
Russian Software Developer Network http://rsdn.ru
squirrel
November 30th, 2001, 10:24 PM
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:).
NMTop40
December 3rd, 2001, 10:32 AM
you can use ostrstream class.
Then you can concatenate a string with << (not >>)
So you can do:
void function( std::ostrstream& 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.
NMTop40
December 3rd, 2001, 10:34 AM
what arrays exactly?
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.