Solved

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

Posted on 1999-01-23
6
181 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 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
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 clear a vector as well as how to detect empty vectors in C++.

635 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