Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 183
  • Last Modified:

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

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
tanc02
Asked:
tanc02
1 Solution
 
Answers2000Commented:
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
 
viktornetCommented:
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
 
nietodCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
nietodCommented:
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
 
tanc02Author Commented:
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
 
nietodCommented:
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now