?
Solved

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

Posted on 1999-01-23
6
Medium Priority
?
182 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 8

Accepted Solution

by:
Answers2000 earned 300 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

719 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