Solved

help me !!!!!!!!!!!!!

Posted on 1999-01-23
6
173 Views
Last Modified: 2010-04-02
this is my own library named mylib.h

#include <iostream.h>
void set_col(int n);
void set_col(int n)
{
   for(int i = 0; i < n; ++i)
      cout<<" ";
}


in my try .cpp

#include <iostream.h>
#include "mylib.h"

void main()
{
  set_col(5);
  cout <<"Hello ! World ! "<< endl;
}

OK, If I ran this program , you will see "Hello ! World !"
displayed at position 5. Like

     Hello ! world !

But if I did the following , I will get compiled error

  cout << set_col(5) << "Hello ! World !"<< endl;

how to change mylib.h to suit my need.
Thank you !
0
Comment
Question by:tanc02
6 Comments
 
LVL 8

Accepted Solution

by:
Answers2000 earned 100 total points
ID: 1184971
You need to make set_col return a type that cout's operator << understands, (e.g. a pointer to a string)

//Crude example
const char * set_col( int n )
{
static char str[256] ; // 256 is max number of cols
for(int i = 0; i < n; ++i)
{
str[i] = ' ' ;
}
str[n] = '\0' ;
return str ;
}

// str is static so the object it points to will still exists when set_col returns.
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1184972
Answers2000, wouldn't that start filling the array from 1 and not from 0...

I think it should be like this....

const char * set_col( int n )
   {
   static char str[256] ; // 256 is max number of cols
   for(int i = 0; i < n; i++)
     str[i] = ' ' ;
   str[n] = '\0' ; //str[i] = NULL; is also a way to do it....
     return str ;
   }

I think that's the way to do it..

-Viktor
--Ivanov
0
 
LVL 22

Expert Comment

by:nietod
ID: 1184973
I think the best way to handle this is to write an I/O stream manipulator.  Then You don't need the static memory.
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 22

Expert Comment

by:nietod
ID: 1184974
The following example from MS VC does exactly what you propose.

#include <iostream.h>
#include <iomanip.h>

ostream& fb( ostream& os, int l )
{
   for( int i=0; i < l; i++ )
        os << ' ';
   return os;
}

OMANIP(int) fillblank( int l )
{
   return OMANIP(int) ( fb, l );
}

void main()
{
    cout << "10 blanks follow" << fillblank( 10 ) << ".\n";
}

0
 
LVL 1

Author Comment

by:tanc02
ID: 1184975
nietod :

   I have tried your program, it gave me compiled error.

viktornet and answers200 :
   
  your program is also work.

answer2000 :

  could you modify you program that will allow these :

  set_col(10);
  cout <<"f"<<endl;
  cout << set_col(10)<<"f"<<endl;

to work ?
0
 
LVL 22

Expert Comment

by:nietod
ID: 1184976
That is not my program.  That is an example from VC's help.  It shows how to create I/O manipulators using the methods that the STL library designers had in mind.  This is a much safer and more robust way to create I/O manipulators.  I would suggest you pursue that route.  If there was a compiler error, it can be fixed.  The problems in the other method cannot.  (Like thread safety and the fact that the space is limited to the size of the static storage--which is also a waste of space.)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question