Solved

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

Posted on 1999-01-23
6
170 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 22

Expert Comment

by:nietod
Comment Utility
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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now