vijayan121 1,152 Posting Virtuoso

> I'm going to research vijayan121's links to see if I can get them to work for me.
if you just need to communicate between the two processes, a file is not required at all. you could use a standard IPC technique (shared memory, pipes, sockets, rpc). a file is required only if you want the data to persist after process termination.

vijayan121 1,152 Posting Virtuoso

> I still don't know why my original code doesn't work, though...
to make the read end of a pipe the stdin for a process, you need to do a bit more than what was in your original code. the easy way to do this is to use popen and pclose which is a wrapper around the sequence of operations required.
http://www.freebsd.org/cgi/man.cgi?query=popen&apropos=0&sektion=3&manpath=FreeBSD+7.0-stable&format=html
for example, to pipe output through the the unix pager:

#include <stdio.h>

int main()
{
  /* popen does the following:
  a. create a pipe
  b. fork a child
  c. in parent: close the read end of the pipe
  d. in child: close the write end of the pipe
  e. in child: dup the read end of the pipe to stdin
  f. in child: exec the command. when the command 
      executes, its stdin is the read end of the pipe.
  g. returns a FILE* opened for writing to the parent.
      must be closed with pclose() 
      writes to the write end of the pipe */
  FILE* pipe_out = popen( "${PAGER:-/usr/bin/more}", "w" ) ;
  int i = 0 ;
  while( i<1024 ) fprintf( pipe_out, "line %d\n", ++i ) ;
  pclose( pipe_out ) ; /* calls waitpid(child) */
  return 0 ;
}
vijayan121 1,152 Posting Virtuoso
vijayan121 1,152 Posting Virtuoso

> I would like to be able to read from pipe in child so I know what parent has sent.
> How to do this ...
the simplest way would be to use read and write on the anonymous pipe.
man read(2) http://www.freebsd.org/cgi/man.cgi?query=read&apropos=0&sektion=2&manpath=FreeBSD+7.0-stable&format=html
man write(2) http://www.freebsd.org/cgi/man.cgi?query=write&apropos=0&sektion=2&manpath=FreeBSD+7.0-stable&format=html

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

int main()
{
  int toChild[2] = {0}, fromChild[2] = {0};
  pid_t pid = -1 ;
  char parent_msg[] = "*** message from parent" ;
  char child_msg[] = "### message from child" ;

  pipe(toChild) ;
  pipe(fromChild) ;
  pid = fork() ;

  if(pid == 0)
  {/* CHILD */
    close(toChild[1]); //child won't write to child... :)
    close(fromChild[0]); //child won't read it's own messages
    char buff[ sizeof(parent_msg) ];
    read( toChild[0], buff, sizeof(parent_msg) ) ;
    pid_t pid = getpid() ;
    printf( "in child (pid %d): read: %s\n", pid, buff ) ;
    write( fromChild[1], child_msg, sizeof(child_msg) ) ;
    close(toChild[0]);
    close(fromChild[1]);
  }
  else
  {/* PARENT */
    close(toChild[0]); //paren't won't read his own messages...
    close(fromChild[1]); //paren't won't write to parent... :)
    write( toChild[1], parent_msg, sizeof(parent_msg) ) ;
    char buff[ sizeof(child_msg) ];
    read( fromChild[0], buff, sizeof(child_msg) ) ;
    pid_t pid = getpid() ;
    printf( "in parent (pid %d): read: %s\n", pid, buff ) ;
    close(toChild[1]);
    close(fromChild[0]);
  }
  return 0;
}
vijayan121 1,152 Posting Virtuoso

use the Boost.Python library to load the python interpretor into C++ and use Boost.Python's eval or exec to call the python code from within C++.
http://www.boost.org/doc/libs/1_35_0/libs/python/doc/tutorial/doc/html/python/embedding.html#python.using_the_interpreter

vijayan121 1,152 Posting Virtuoso

> a different program to read from that file BEFORE the first program is finished writing to it.
the output to a file is buffered at many levels: by the fstreambuf of the c++ library and by the operating system (cache manager). to achieve coherence between the two processes' view of the file, consider memory mapping the file.
see: http://msdn2.microsoft.com/en-us/library/aa914748.aspx
and http://msdn2.microsoft.com/en-us/library/aa914405.aspx

vijayan121 1,152 Posting Virtuoso

put all the common stuff in a base class and inherit from it.

#include <iostream>

template< typename E > struct enemy_manager_base
{
  void same_for_all()
  { std::cout << "same for all enemies\n" ; }
};

template< typename E > struct enemy_manager : enemy_manager_base<E>
{
  void to_be_specialized()
  { std::cout << "generalization of to_be_specialized\n" ; }
};

struct tank ;

template<> struct enemy_manager<tank> : enemy_manager_base<tank>
{
  void to_be_specialized()
  { std::cout << "specialization of to_be_specialized (tank)\n" ; }
};

int main()
{
  enemy_manager<int> em_gen ;
  em_gen.same_for_all() ;
  em_gen.to_be_specialized() ;

  enemy_manager<tank> em_tank ;
  em_tank.same_for_all() ;
  em_tank.to_be_specialized() ;
}
vijayan121 1,152 Posting Virtuoso

a. place the call to the exit handler member function in the destructor of a class.
b. create an instance of the class with a static storage duration.

the order of calling C atexit / C++ destructors for objects with a static storage duration is specified by secion 3.6.3/3 of the standard:

If a function is registered with atexit then following the call to exit, any objects with static storage duration initialized prior to the registration of that function shall not be destroyed until the registered function is called from the termination process and has completed.
For an object with static storage duration constructed after a function is registered with atexit, then following the call to to exit, the registered function is not called until the execution of the object's destructor has completed.
If atexit is called during the construction of an object, the complete object to which it belongs shall be destroyed before the registered function is called.

for example, for the following program:

#include <iostream>
#include <cstdlib>

struct clean_up
{
  explicit clean_up( const char* m ) : msg(m) {}
  void do_clean_up()
  { std::cout << msg << ": cleaning up via mem fun...\n" ; }
  ~clean_up() { do_clean_up() ; }
  const char* const msg ;
};

void at_exit_one()
{ std::cout << "one: cleaning up via free fun...\n" ; }

void at_exit_three()
{ std::cout << "three: cleaning up via free fun...\n" ; }

int main()
{
  std::atexit( at_exit_one ) ;
  static clean_up two( "two" ) …
vijayan121 1,152 Posting Virtuoso

...Maybe GNU C++ doesn't support it?
Ups, actually there was mistake in code from my first post. It should be:

template <class A, class B> A* g (B&);
template <class T>
class X {
template <class A, class B>
friend A* g (T&);
};

the friend declaration in this snippet does not give the effect that you are seeking

( i mean that g<A, B> () is only friend of X <B> )

perhaps this example would clarify:

template <class A, class B> A* g (B&);
template <class A, class B> A* g (int&);

template <class T>
class X
{
  template <class A, class B>
    friend A* g (T&);
  private : T i ;
};

template <class A, class B> A* g (B& b)
{
  // when called with type B == X<int>
  return &b.i ; // error: not a friend of X<int>
}

template <class A, class B> A* g (int&)
{
  static B xx ;
  return &xx.i ; // ok, friend of X<int>
}

int main()
{
  X<int> xx ;
  // g< int, X<int> >(xx) ; // error: 'int X<int>::i' is private

  int i = 8 ;
  // ok, int* g< int, X<int> >(int&) is a friend of X<int>
  g< int, X<int> >(i) ; 
}

for some additional information see:
http://www.parashift.com/c++-faq-lite/templates.html#faq-35.16
http://www.devx.com/cplus/10MinuteSolution/30302/0/page/3

for a more detailed discussion, see
C++ Templates: The Complete Guide by David Vandevoorde, Nicolai M. Josuttis isbn: 0-201-73484-2
sections 8.4, 9.1, 9.2 and 9.3

vijayan121 1,152 Posting Virtuoso

> error C2514: 'food' : class has no constructors (line 15)
> see declaration of 'food' (line 7)
> is the error caused by friending MainControl with a derived class?
no.
class food has been declared, but not defined.
you need to #include "food.h"

vijayan121 1,152 Posting Virtuoso
// ...
A *b;
b = new B(param1, param2);
delete b; 
// ...

the destructor that is called in line 4 is A::~A()
you need to call B::~B() to destroy both strings
solution: the destructor for the base class must be virtual.

class A
{
public:
	string sa;
	A(string sia);
	virtual ~A() {}
};
vijayan121 1,152 Posting Virtuoso

the friend function that you require for this has a different signature.

#include <iostream>
template < typename T1,  typename T2 > T1* g ( T2& ) ;

template < typename T >  class X
{
  private:
      T *ptr;
  public:
      X (T *ptr) : ptr (ptr) {}

      friend T* g <> ( X<T>& );
};

template < typename T1,  typename T2> T1* g (T2& x)
{  return x.ptr; }

int main()
{
  X<int> klasa_x ( reinterpret_cast<int*>(1) ) ;
  int* wsk_wyd = g< int, X<int> > ( klasa_x );
  std::cout << wsk_wyd << '\n' ;
}

you might find that writing the code this way would be more flexible in the long run.

#include <iostream>
template < typename T > typename T::pointer_type g ( T& ) ;

template < typename T >  class X
{
  private:
      T* ptr;
  public:
      typedef T* pointer_type ; 
      X (T *ptr) : ptr (ptr) {}

      friend pointer_type g <> ( X<T>& );
};

template < typename T > typename T::pointer_type g( T& object )
{  return object.ptr;  }

int main()
{
  X<int> klasa_x ( reinterpret_cast<int*>(1) ) ;
  int* wsk_wyd = g( klasa_x );
  std::cout << wsk_wyd << '\n' ;
}
vijayan121 1,152 Posting Virtuoso

boost::algorithm::split works like std::strtok . delimiters that are just single characters.
use boost::algorithm::split_regex to split character sequences where delimiters are regular expressions.
for example, to split a string on delimiters which are either sequences of some number of digits or ->

#include <string>
#include <iostream>
#include <iterator>
#include <boost/regex.hpp>
#include <boost/algorithm/string/regex.hpp>
#include <vector>
using namespace std;
using namespace boost;

int main()
{  
  string str = "abc1234def->ijkl->mnop56qrs->tuv7890wxyz" ;
  vector< string > result;
  boost::algorithm::split_regex( result, str, 
                                 regex( "[0-9]+|->" ) ) ;
  copy( result.begin(), result.end(),
        ostream_iterator<string>( cout, "\n" ) ) ;
}

note: you need to link with libboost_regex. eg.

> g++ -Wall -std=c++98 -pedantic -Werror -I /usr/local/include -L /usr/local/lib -lboost_regex myprogram.cc
sjcomp commented: Good alternative, clear example +1
vijayan121 1,152 Posting Virtuoso
class objects;
class food;
class MainControl
{
  public:
  friend class objects;
  friend class food;
  // ...
};
vijayan121 1,152 Posting Virtuoso

> i mean that g<A, B> () is only friend of X <B>?

template < typename T1, typename T2 > T1* g( T2& ) ;

class A ;

template < typename T > class X
{
  friend A* g <> ( T& ) ;
  // ...
};
vijayan121 1,152 Posting Virtuoso

> Is set like to priority_queue according to algorithm?I need a heap algorithm.
no.

> removing the first is so easy.
> removing ...is difficult. Because priority_queue hasnt a remove function.
the simplest solution may be to write your own priority_queue which has an erase function. (it will not be a priority_queue in a strict sense, but a heap with erase functionality).

#include <vector>
#include <algorithm>
#include <iterator>

template< typename T, typename sequence_type = std::vector<T>,
          typename cmp_function_t  =
               std::less< typename sequence_type::value_type > >
struct my_priority_queue
{
  // *** note: ignoring c++0x concepts requirements ***
    // 1. T has AssignableConcept
    // 2. sequence_type has SequenceConcept
    // 3. sequence_type has RandomAccessContainerConcept
    // 4. T and sequence_type::value_type have SameTypeConcept
    // 5. cmp_function_t has bool(T,T) BinaryFunctionConcept

  typedef typename sequence_type::value_type value_type;
  typedef typename sequence_type::reference reference;
  typedef typename sequence_type::const_reference const_reference;
  typedef typename sequence_type::size_type size_type;
  typedef sequence_type container_type;

  explicit my_priority_queue(
        const cmp_function_t& fn = cmp_function_t(),
        const sequence_type& cntr = sequence_type() )
    : c( cntr ), comp( fn )
    { std::make_heap( c.begin(), c.end(), comp ) ; }

  template<typename iterator_t>
      my_priority_queue( iterator_t begin, iterator_t end,
         const cmp_function_t& fn = cmp_function_t(),
         const sequence_type& cntr = sequence_type() )
    : c( cntr ), comp( fn )
    {
       c.insert( c.end(), begin, end ) ;
       std::make_heap( c.begin(), c.end(), comp ) ;
    }

  bool empty() const { return c.empty() ; }

  size_type size() const { return c.size() ; }

  const_reference top() const { return c.front(); }

  void push( const value_type& v )
  {
    c.push_back(v);
    std::push_heap( c.begin(), c.end(), comp ) ; …
vijayan121 1,152 Posting Virtuoso

> ive never herd of a sliding window before
the sliding window algorithm is perhaps the most important algorithm used in the implementation of the TCP protocol. it is inefficient for the sender to wait after each packet for its ACK before sending the next. a sliding window is used to keep a record of the packet sequences sent and the respective ACKs received.

as an example, if we take the trivial case of a fixed window size M: a sender may sent M packets ( packet numbers N ... N+M-1 ) before receiving any ACK. If ACK is received for packet N, the window slides to N+1 ... N+M, and the sender can transmit packet N+M. sliding signifies a FIFO operation, trimming the range at one end (closing the window), and extending it at the other (opening the window).

> and i cant understand how its working?
consider simulating the program flow using pencil and paper.

> To get it to loop through my file and find every picture would i have to put it in a loop until EOF?
right.

vijayan121 1,152 Posting Virtuoso

> i have lots of jpeg's inside a text file full of data. I presently search for the start of every picture.
> I then want to stream the data into a new file until i reach the end of every picture ...
> the end of the data i need which is my 'match_criteria1'
how are you sure that the byte sequence specified by 'match_criteria1' is not present in the jpeg image? can't you just read the jpeg header instead?

to stream all the bytes between a start byte sequence and and end byte sequence, the simplest way would be to use a sliding window.

#include <deque>
#include <fstream>
#include <iostream>
#include <cstring>
using namespace std ;

int main()
{
  typedef unsigned char byte ;
  static byte match_1[] = { 0xFF, 0xD9, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00  };
  static byte match_0[] = { 0xFF, 0xD8, 0xFF, 0xC0 };
  enum { SIZE_0 = sizeof(match_0), SIZE_1 = sizeof(match_1) } ;
  std::ifstream fin( "infilename", std::ios::binary ) ;
  fin >> std::noskipws ;
  std::ofstream fout( "outfilename", std::ios::binary ) ;

  byte ch ;
  std::deque<byte> swindow ;

  while( ( swindow.size() < SIZE_0 ) )
  {
    if( !( fin >> ch ) ) return 1 ;
    swindow.push_back( ch ) ;
  }

  // note: could use std::search instead of std::memcmp
  while( std::memcmp( &swindow.front(), match_0, SIZE_0 ) != 0 )
  {
    if( !( fin >> ch ) ) return 1 ;
    swindow.push_back( ch ) ;
    swindow.pop_front() ; …
vijayan121 1,152 Posting Virtuoso

> line 7 vijay's function declaration is wrong or just plain convoluted
it is not wrong. if it seems to be just plain convoluted, it is so for a reason

template< std::size_t N > inline
void function_one( char (&array) [N][N] )
{ array[N-1][N-1] = 0 ; }

template< std::size_t N > inline
void function_two( char array [N][N] )
{ array[N-1][N-1] = 0 ; }

int main()
{
  char a[20][20] ;
  function_one( a ) ; // ok
  function_two( a ) ; // ok

  char b[5][15] ;
  function_one( b ) ; // error: no matching function call
  function_two( b ) ; // compiles without error
}
vijayan121 1,152 Posting Virtuoso

> ... but that didn't work either. Still weird numbers.

assert( ( matrix[i][3] == 0  ) || ( matrix[i][3] == 1 ) ) ; 
  assert( ( matrix[i][4] == 0 ) || ( matrix[i][4] == 1 ) ) ; 
  int sum =  matrix[i][3] + matrix[i][4] ;
  if( sum == 2 ) ++counters[1][1] ;
  else if( sum == 1 ) ++counters[0][1] ;
  else ++counters[0][0] ;
vijayan121 1,152 Posting Virtuoso
vijayan121 1,152 Posting Virtuoso
int sum =  matrix[i][3] + matrix[i][4] ;
  if( sum == 2 ) ++counters[1][1] ;
  else if( sum == 1 ) ++counters[0][1] ;
  else ++counters[0][0] ;
vijayan121 1,152 Posting Virtuoso

this is one (not the most efficient) way:

#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>

template< std::size_t N > inline 
void copy_string_to_array(  char (&array) [N][N], 
                           const char* cstr )
{
  std::memset( array, 0, N*N ) ;
  std::memcpy( array, cstr, std::min( std::strlen(cstr), N*N ) ) ; 
}

int main()
{
  enum { N = 4 } ;
  const std::string str = "this is the string to be copied" ;
  char array[N][N] ;

  std::size_t copied = 0 ;
  while( copied < str.size() )
  {
    copy_string_to_array( array, str.c_str()+copied ) ;
    copied += N * N ;
    // process array
  }
}
vijayan121 1,152 Posting Virtuoso

non-static member functions have an implicit argument: the this pointer (a pointer to the object the function is called on). a pointer to a free function has nothing of this kind. the dereference operators on pointers to members ( .* and ->* ) are binary operators; you dereference a pointer to a member with respect to an object on which the member is applied. it is best to think of pointers to members as opaque abstractions that aren't really pointers (in the C sense).

the usual work around is something like this:

class Testing
{
  private:
    cLog* m_Log;
    pthread_t tid; // would one suffice?
    // std::vector<pthread_t> tids ; ?

    public:
      Testing( );
      ~Testing( );
      void floodLog( int nThreads );

    private:
      void* writeToLog( void* );
      
      static void* thread_fun( void* args ) ;

      struct thread_fun_args
      {
        Testing* This ;
        // other args as required; in this case
        void* actual_arg ;
        thread_fun_args( Testing* t, void* p ) 
            : This(t), actual_arg(p) {}
      };
};

void Testing::floodLog( int nThreads )
{
  cout << "Flooding cLog class with " << nThreads << " threads.\n";
  for( int i = 0; i < nThreads; i++ )
  {
    cout << "Created Thread: " << ( i + 1 ) << endl;
    pthread_create( &tid, NULL, &Testing::thread_fun, 
                            new thread_fun_args(this,0) );
  }
  cout << "Finished flooding.\n";
}

void* Testing::thread_fun( void* pv )
{
    thread_fun_args* tf_args = static_cast<thread_fun_args*>(pv) ;
    Testing* This = tf_args->This ;
    void* args = tf_args->actual_arg ;
    void* result = This->writeToLog( args ) ;
    delete tf_args ;
    return result …
vijayan121 1,152 Posting Virtuoso

here is another way (which you may find easier). /sbin/ifconfig -a would give you an output of this kind:

fxp0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1500
        options=8<VLAN_MTU>
        ether 00:d0:43:7a:c0:6e
        inet 192.168.2.192 netmask 0xffffff00 broadcast 192.168.2.255
        media: Ethernet autoselect (100baseTX <full-duplex>)
        status: active
plip0: flags=108810<POINTOPOINT,SIMPLEX,MULTICAST,NEEDSGIANT> metric 0 mtu 1500
pflog0: flags=141<UP,RUNNING,PROMISC> metric 0 mtu 33204
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> metric 0 mtu 16384
        inet6 fe80::1%lo0 prefixlen 64 scopeid 0x4
        inet6 ::1 prefixlen 128
        inet 127.0.0.1 netmask 0xff000000
pfsync0: flags=0<> metric 0 mtu 1460
        syncpeer: 224.0.0.240 maxupd: 128

ie. info on all the network interfaces.
all you have to do now is extract the ip addresses out of this information.

#include <cstdlib>
#include <iostream>
#include <fstream>

int main()
{
  system( "ifconfig -a | grep inet | "
          "sed 's/\\([ ]*[^ ]*\\)\\([ ]*[^ ]*\\).*$/\\1 \\2/' "
          " > address.txt" ) ;
}

the file address.txt will contain the addresses. on my machine:

inet  192.168.2.192
        inet6  fe80::1%lo0
        inet6  ::1
        inet  127.0.0.1

the lines starting with inet are the ipv4 addresses, the ones with inet6 are the ipv6 addresses. read the addresses off the file using a std::ifstream.

note: if you want only externally visible ip addresses, discard addresses like the ipv4 address 127.0.0.x and the ipv6 address 0000:0000:0000:0000:0000:0000:0000:x ( ::x ). these are loopback addresses on lo0. also ignore any ipv6 address starting with fe80 (this is the ipv6 link-local prefix) or ipv4 addresses starting with 169.254.0. in the above example, the only address externally visible is the ipv4 address 192.168.2.192. you …

vijayan121 1,152 Posting Virtuoso

> What does this means (a => 1) or '=>'
it means a compile-time error in C++; there is no such operator.
what i intended was: convert each character in the string name to something else as required by your encryption logic. as an example, convert the character 'a' in the string name to a character '1' (before writing it into the file). a => 1 meaning transform character 'a' to character '1'.

vijayan121 1,152 Posting Virtuoso

gethostname will give you the name of the current host gethostbyname looks up the host with a particular name and will give you the address
> man 3 gethostname > man 3 gethostbyname

vijayan121 1,152 Posting Virtuoso

add a const specifier (in both the declaration and the definition of

const std::type_info& TE::MyClass::get_type(const TE::MyClass*) const ;

and you should probably write

/*const*/ TE::MyClass *obj= new MyClass(string);

or delete obj ; will not work.


why you need this function is beyond me:

const TE::MyClass *obj= new MyClass(string);
obj->get_type(obj); //try to get the type of my obj.

you know at this point the precise type of obj; a pointer to a TE::MyClass object.

vijayan121 1,152 Posting Virtuoso

can I use it like this:
const std::type_info& get_type( const MyClass* object )
{
return typeid(object) ;
}

And to call it
obj->get_type(myObj);

yes, you can. remember though that the type of the variable object is a pointer and this would give you the type of the pointer. if you want the type of the object pointed to,

const std::type_info& get_type( const MyClass* object )
{
  return typeid( *object ) ;
}

> what is: template< typename T >inline
if you can not make any sense of that, ignore it for now.
you will come to templates soon enough.

vijayan121 1,152 Posting Virtuoso

this will read in a name (which may contain spaces) and write it out to a file. fill in the TODO: part

#include <iostream>
#include <string>
#include <fstream>

int main()
{
  std::string name ;
  std::getline( std::cin, name ) ;
  
  // TODO:
  // add code here to transform each char in name 
  // eg. a => 1, b => 2 etc.

  std::ofstream file( "Encription.txt" ) ;
  file << name << '\n' ;
}

see if you can get this much done correctly.

once this is done, it would be time to look at the second program (which reads the file and displays the original name). don't even think about how you would do that right now. get this part to work first.

vijayan121 1,152 Posting Virtuoso

> i need to remove "//" this character from a char *str
isn't "//" two characters? did you mean "\\" ?

const char* cstr = "abc\\def\\gh" ;
  std::string str = cstr ;
  std::string::iterator where =
        std::find( str.begin(), str.end(), '\\' ) ;
  if( where != str.end() ) str.erase( where ) ;

to erase all occurrences, put the above in a loop (break when where == str.end() )

vijayan121 1,152 Posting Virtuoso
#include <iostream>
#include <string>
#include <sstream>

int main()
{
  bool validated = true ;
  do
  {
    std::string line ;
    std::getline( std::cin, line ) ; // read next line
    std::istringstream stm(line) ;
    // read each number/fraction seperately from stm
    // eg. int a ; stm >> a ; etc.
    // validate one by one
    // if the validation is successful,
    //     create the object of each type.
    //     set validated = true ;
    // else
    //     set validated = false ;
  } while( !validated ) ;
}
vijayan121 1,152 Posting Virtuoso

i'm sorry; i was under the impression that you were on C++/CLI
in standard C++, the type of an expression is given by the typeid operator which yields
a const std::type_info& eg.

#include <typeinfo>

template< typename T >inline 
const std::type_info& get_type( T& object )
{
  return typeid(object) ;
}
template< typename T >inline 
const std::type_info& get_type( const T& object )
{
  return typeid(object) ;
}
vijayan121 1,152 Posting Virtuoso

if you are using Dev C++, you would already have either Cygwin or MinGW installed.

vijayan121 1,152 Posting Virtuoso

are you on ISO C++ or C++/CLI (the .net variety)?

vijayan121 1,152 Posting Virtuoso
vijayan121 1,152 Posting Virtuoso

System::Type^ note: you do not need a method to do this; use System::Object::GetType() instead.
eg. System::Type^ type = obj->GetType() ;

vijayan121 1,152 Posting Virtuoso

> I am using Dev c++
Dev c++ is just an ide; the compiler would be GCC, but the environment could be either CygWin or MinGW.
if you are using CygWin, install arping as per instructions here: http://mathieu.carbou.free.fr/wiki/index.php?title=How_to_compile_arping_under_Cygwin#Under_Cygwin
if you are using MinGW these are the instructions: http://mathieu.carbou.free.fr/wiki/index.php?title=How_to_compile_arping_under_Cygwin#Under_MinGW

once this is installed, you could use arping to send arp or icmp pings to hosts on the network. icmp pings will work only on routed networks, arp ping will work everywhere.

for example, if you are on a routed network where the ip addresses are in the range 192.168.2.0 to 192.168.2.255 (look at your subnet mask to see what it is for you), you could run the following shell script to discover ip addresses that are in use on your subnet. (this works on the unix shell or any shell conforming to POSIX.2. you will instead have something called bash. you may have to modify the script accordingly)

#!/bin/sh

seq() { echo "for (i=$1; i<=$2; i++) i;" | bc | tr "\012" " "; }

#this example is for: host ip 192.168.2.x netmask 0xffffff00

for a in $(seq 192 192); do
  for b in $(seq 168 168); do
    for c in $(seq 2 2); do
      for d in $(seq 0 255); do
        sh -c "arping -rRc 1 $a.$b.$c.$d >> iplist.txt
        if [ \$? = 0 ]; then
          echo Got answer with address: $a.$b.$c.$d
        fi" &
      done
    wait …
vijayan121 1,152 Posting Virtuoso

> Does the isgood() function tell which chars are valid or not?
> or is there a similar function that i could use?
no, the closest you have is

template<typename char_type> 
  std::ispunct( const char_type&, const std::locale& ) ;

but you could easily write one of your own:

#include <string>

inline bool isgood( char c )
{
  static const std::string bad_chars = "!@#$><&" ; // etc
  return bad_chars.find( c, 0 ) == std::string::npos ;
}
vijayan121 1,152 Posting Virtuoso

to use glut wih visual studio, all you need to do is put the glut files in places where visual studio / windows loader will look for them.
glut32.dll in <windows home>/system or in a directory in your PATH
glut32.lib in one of the visual studio include directories
glut.h in one of the visual studio libraries directories

because glut is designed to be window system independent (it makes its own windows), it is better to use glut in a console application than in a native win32 or mfc application.

to use glut with mfc see: http://www.kencocomputers.com/tutorials/

vijayan121 1,152 Posting Virtuoso

you could just use ICMP ping on every possible ip on your lan.
(the subnet mask/broadcast address will give you this info. for example, use. >ifconfig -L http://www.freebsd.org/cgi/man.cgi?query=ifconfig&sektion=8 ).
even if the computer blocks ping (and everything else) you still get an entry in your arp cache.
you can then look at your arp cache ( with >arp -a ) to determine the ip addresses. http://www.freebsd.org/cgi/man.cgi?query=arp&sektion=8

if you are not on a routable net, this technique will not work. you would then need to use an ARP ping instead of an ICMP ping (and then dump your arp cache).
see http://www.habets.pp.se/synscan/programs.php?prog=arping (if you are on dev-c++ with minGW, download the windows version)

vijayan121 1,152 Posting Virtuoso

Form::ShowDialog() shows the form as a modal dialog box. (the code following it is not executed until after the dialog box is closed.)
to show two instances of the same form:
a. create two instances of the Form (you seem to already have one instance, so create one more)
b. call Form::Show() on each of them.

vijayan121 1,152 Posting Virtuoso
// ...
  if (length_one >= 0)
  {

  cout << "Enter the length of the side 2 in inches: ";
  cin >> length_two;

  cout << endl << endl << "Triangle: Side 1: " << length_one << " inches";
  cout << endl <<"\t  Side 2: " << length_two << " inches";

  hypotenuse = Calculate_Hypotenuse(length_one, length_two);
  cout << endl <<"Hypotenuse: " << hypotenuse << " inches";
  cout << endl << endl;

  }
  // ...
vijayan121 1,152 Posting Virtuoso
// ...
bool Y = true;
cout <<"Enter an assignment score and type 'Y' if it was late: ";
cin >> score >> Y;
// ...

the bool literals are true and false . when performing formatted input or output, the character sequences that bool values are translated from/to depends on the format flag std::ios::boolalpha . if set, the same literals true and false are used. if unset (this is the default), the literals used are 1 and 0 .

# include <iostream>
# include <sstream>
# include <string>

int main()
{
  std::string input = "0 false 1 true" ;
  std::istringstream stm(input) ;

  // make std::cin get it's input from the string 
  std::cin.rdbuf( stm.rdbuf() ) ; 

  bool flag ;

  std::cin >> flag ; // ios::boolalpha unset (default)
  std::cout << std::boolalpha << flag << ' '
            << std::noboolalpha << flag << '\n' ;

  std::cin >> std::boolalpha >> flag ;
  std::cout << std::boolalpha << flag << ' '
            << std::noboolalpha << flag << '\n' ;

  std::cin >> std::noboolalpha >> flag ;
  std::cout << std::boolalpha << flag << ' '
            << std::noboolalpha << flag << '\n' ;

  std::cin >> std::boolalpha >> flag ;
  std::cout << std::boolalpha << flag << ' '
            << std::noboolalpha << flag << '\n' ;

  std::cin.rdbuf(0) ; // restore default
}
vijayan121 1,152 Posting Virtuoso

> what is the problem
the problem is that string b; creates an empty string. b[j]=a[i]; is incorrect; there are no characters in b.

> i want the first character on string a to be the last char on string b
> up until the last on a is the first on b
this would suffice.

#include <iostream>
#include <string>
using namespace std;

int main()
{
  string a;
  cout<< "enter string" <<endl;
  cin>> a;

  string b( a.rbegin(), a.rend() ) ;
  cout<< b <<endl;
}
vijayan121 1,152 Posting Virtuoso
vijayan121 1,152 Posting Virtuoso

if you want to iterate through a sequence container erasing elements as you go along, you must use the return value of container::erase . the return value is a vald iterator that designates the first element remaining beyond element(s) erased, or end if no such element exists. for example, the following will erase all even numbers from the vector.

std::vector<int> vec(100) ;
  for( int i=0 ; i<vec.size() ; ++i ) vec[i] = i ;
  std::vector<int>::iterator iter = vec.begin() ; 
  while( iter != vec.end()  )
  {
    if( ( *iter % 2 ) == 0 ) iter = vec.erase( iter ) ;
    else ++iter ;
  }

to remove duplicate elements that are adjacent to each other in a specified range, you can use the algorithm std::unique . for example, this will erase adjacent duplicate elements from the vector.

std::vector<int> vec(100) ;
  for( int i=0 ; i<vec.size() ; ++i ) vec[i] = i/10 ;
  vec.erase( std::unique( vec.begin(), vec.end() ), vec.end() ) ;
Ancient Dragon commented: Gread explaination -- I learned something too today :) +25
vijayan121 1,152 Posting Virtuoso
// ...   
   val = 0;
   for (p=0; p<3; p++)
  {
      //val += (a[i][p]*b[p][i]);
      val += (a[i][p]*b[p][j]);
      cout << a[i][p] << " x " << b[p][j] << " + ";
   }
   // ...
vijayan121 1,152 Posting Virtuoso

> i am not quite sure how i would go about reading in the traverse and delete lines.
instead of trying to do everything in one place, break it up into smaller tasks (each of which is quite manageable).

> I want to set the first character of each traverse line which is denoted by a t ...
the first character on each line would tell you if it is a traverse action or a delete action.

enum action { TRAVERSE, DELETE, NOTHING } ;
action what( const std::string& line )
{
  switch( line[0] )
  {
     case 't' : case 'T' : return TRAVERSE ;
     case 'd' : case 'D' : return DELETE ;
     default : return NOTHING ;
  }
}

> i am not quite sure how to read in the traverse line with two directions
> and then go about reading in the traverse line with only 1 direction
each move in the traverse is given by a (direction,num_steps) tuple.
a traversal is a sequence of one or more of such tuples.
you would need a sequence container of these tuples to keep the moves in a traversal.

enum direction { UP, RIGHT, DOWN, LEFT, STAYPUT } ;
typedef std::vector< std::pair<direction,int> > traverse_data ;

for a traverse, read in these tuples, adding each one to traverse_data till end of line.

traverse_data get_traversal( const std::string& line )
{
  traverse_data traversal ;
  std::istringstream stm(line) ;
  char eat_it ; stm >> …
vijayan121 1,152 Posting Virtuoso

the input may not be a number due to several reasons:
a. it may be empty
b. it may have invalid characters
c. it may be too long
d. it may have an invalid format eg. 45.73.2 or +-42

the simplest way to validate is perhaps
a. read the input as a string.
b. try to convert the string to a number.
c. if the entire input is consumed in step b., the input is ok.

#include <iostream>
#include <string>
#include <sstream>
double get_it()
{
  std::string input ;
  while( true )
  {
    std::cout << "please enter a number followed by a newline: " ;
    std::getline( std::cin, input ) ;
    std::istringstream stm( input ) ;
    double value ;
    if( ( stm >> value )  && stm.eof() )  return value ;
    else std::cout << "*** error in input \'" << input << "\'\n" ;
  }
}