vijayan121 1,152 Posting Virtuoso

in c++, ^ is the bitwise exclusive OR operator.
to raise an integer to a power, use std::pow ( #include <cmath> )
eg: double result = std::pow( double(3), 3 ) ;

vijayan121 1,152 Posting Virtuoso

> do you know of any good books or sites that extensively cover WIN32 API odd ball type code
i don't program very much in the win32 environment. the only one i can think of on the spur of the moment is sysinternals
http://forum.sysinternals.com/forum_topics.asp?FID=9
there would be many others which i am not aware of.

> I am in need of an advance C++ code reference? Advance being the key word.
do you mean as a language/library reference? if it is *one* book you are looking for, i don't think that Stroustrup's The C++ Programming Language (Special Edition) can be bettered. a c++ programmer can use this as a reference. It is also a pretty good tutorial (if you are not a beginner).
if you were looking for more than one book, these are some of the the books (which come to mind right now) that i've liked.
1. Ruminations on C++: A Decade of Programming Insight and Experience by Andrew Koenig, Barbara E. Moo
2. The Design and Evolution of C++ by Bjarne Stroustrup
3. Advanced C++ Programming Styles and Idioms by James O. Coplien
4. Inside the C++ Object Model by Stanley B. Lippman
5. C++ Templates: The Complete Guide by David Vandevoorde, Nicolai M. Josuttis
6. Modern C++ Design: Generic Programming and Design Patterns Applied by Andrei Alexandrescu
7. C++ Template Metaprogramming: Concepts, Tools, and Techniques from Boost and Beyond by David Abrahams, …

vijayan121 1,152 Posting Virtuoso

> ... but it doesn't really pose a problem here ...

this

ofstream newOccupant;

if(AuthorizeEntry(IDcard,lot))
{
  cout<<"Lot Accessed." <<endl;
  
  if(lot == 'A')
    ofstream newOccupant("lot_A.txt", ios::out);
  if(lot == 'B')
    ofstream newOccupant("lot_B.txt",  ios::out);
  if(lot == 'C')
    ofstream newOccupant("lot_C.txt",  ios::out);
  if(lot == 'D')
    ofstream newOccupant("lot_D.txt",  ios::out);
  if(lot == 'E')
    ofstream newOccupant("lot_E.txt",  ios::out);
  
  if(!newOccupant)
  // ...

is equivalent to this

ofstream newOccupant;

if(AuthorizeEntry(IDcard,lot))
{
  cout<<"Lot Accessed." <<endl;
  
  if(lot == 'A')
  {
    ofstream newOccupant("lot_A.txt", ios::out);
  }
  if(lot == 'B')
  {
    ofstream newOccupant("lot_B.txt",  ios::out);
  }
  if(lot == 'C')
  {
    ofstream newOccupant("lot_C.txt",  ios::out);
  }
  if(lot == 'D')
  {
    ofstream newOccupant("lot_D.txt",  ios::out);
  }
  if(lot == 'E')
  {
    ofstream newOccupant("lot_E.txt",  ios::out);
  }
  
  if(!newOccupant)
  // ...

> ... as the only use of newOccupant occurs within that if block ..
the only thing that happens in the each of the if blocks are that an ofstream is constructed and immediately destroyed. these ofstreams are not visible and do not exist at the place where an ofstream is used. the ofstream that is used is the one in the outer scope, to which a file has never been associated.

6.4 - Selection statements [stmt.select]
.... If the substatement in a selection-statement is a single statement and not a ompound-statement, it is as if it was rewritten to be a compound-statement containing the original substatement. [Example:
if (x)
int i;
can be equivalently rewritten as
if (x) {
int i;
}
Thus after the if statement, i is no …

vmanes commented: Good catch on subtle aspect +3
vijayan121 1,152 Posting Virtuoso

this is what Pietrek suggests:

#define _WIN32_WINNT  0x0501
#define NO_STRICT
#include <windows.h>
#include <winternl.h>
#include <iostream>
#include <cassert>

wchar_t*  GetEnvironmentStringsW( HANDLE  hproc )
{
  void* p_ntdll = GetModuleHandle( L"ntdll.dll" ) ;
  typedef NTSTATUS (__stdcall* tfn_qip ) ( HANDLE,
                    PROCESSINFOCLASS, void*, ULONG, PULONG ) ;
  tfn_qip pfn_qip = tfn_qip( GetProcAddress( p_ntdll, 
                     "NtQueryInformationProcess" ) ) ;
  assert( pfn_qip ) ;
  PROCESS_BASIC_INFORMATION pbi ;
  ULONG res_len = 0 ;
  NTSTATUS status = pfn_qip( hproc,  ProcessBasicInformation,
                     &pbi, sizeof(pbi), &res_len ) ;
  assert( res_len == sizeof(pbi) ) ;
  size_t ppeb = size_t( pbi.PebBaseAddress ) ;

  char peb[ sizeof(PEB) ] ;
  DWORD read ;
  ReadProcessMemory( hproc, pbi.PebBaseAddress, 
                           peb, sizeof(peb), &read ) ; 
  assert( read == sizeof(peb) ) ;

  enum { OFFSET_PEB = 0x10, OFFSET_X = 0x48 };
  
  void* ptr = (void*) *(INT_PTR*)(peb + OFFSET_PEB ) ;
  char buffer[ OFFSET_X + sizeof(void*) ] ;
  ReadProcessMemory( hproc, ptr, buffer, sizeof(buffer), &read ) ; 
  assert( read == sizeof(buffer) ) ;
  
  void* penv = (void*) *(INT_PTR*)( buffer + OFFSET_X ) ;
  enum { MAX_ENV_SIZE = 4096 }; 
  wchar_t* env = new wchar_t[ MAX_ENV_SIZE ] ;
  ReadProcessMemory( hproc, penv, env, MAX_ENV_SIZE, &read ) ; 
  assert( read > 0 ) ;
  
  return env ;
}

// small test driver
int main()
{
  HWND hwnd = FindWindow( 0, L"Solitaire" ) ;
  assert( hwnd ) ;
  DWORD procid = 0 ;
  GetWindowThreadProcessId( hwnd, &procid ) ;
  assert( procid ) ;
  DWORD amask = PROCESS_QUERY_INFORMATION | PROCESS_VM_READ ;
  HANDLE hproc = OpenProcess( amask, 0, procid ) ;
  assert( hproc ) ;
  
  wchar_t* env = …
vijayan121 1,152 Posting Virtuoso

there is a blunder in the last post (which unfortunately will not get caught by the compiler).

// ...
if( AuthorizeEntry(IDcard,lot) )
{
  cout<<"Lot Accessed." <<endl;
  string file_name = "lot_" ;
  // file_name += lot + ".txt" ; 
  file_name += lot ; file_name += ".txt" ;

  // append? see vmanes' post
  ofstream newOccupant( file_name.c_str(), /* ios::app ? */ ) ;

  if(!newOccupant)
    cout << "Unable to create active lot file";
  else
    addToLot(newOccupant);
}
else
// ...
vijayan121 1,152 Posting Virtuoso
vijayan121 1,152 Posting Virtuoso

the ofstream newOccupant inside the local scope of the if statement is hiding the ofstream newOccupant that you had declared earlier.

// ...
  ofstream newOccupant;
  if(AuthorizeEntry(IDcard,lot))
  {
    cout<<"Lot Accessed." <<endl;
		
    if(lot == 'A')
       ofstream newOccupant("lot_A.txt", ios::out);
       // this newOccupant is a different variable inside
       // the local scope of the if statement. it hides 
       // the variable newOccupant declared on line 2 
     // etc
		
    // here the newOccupant referred to is the one 
    // declared on line 2 ; no file has been opened for this.
    // the newOccupant inside the local scope of the 
    // if statement has already been destroyed by now.
    if(!newOccupant) 
      // ...

instead, you need to do something like this

// ...
if( AuthorizeEntry(IDcard,lot) )
{
  cout<<"Lot Accessed." <<endl;
  string file_name = "lot_" ;
  file_name += lot + ".txt" ; 

  // append? see vmanes' post
  ofstream newOccupant( file_name.c_str(), /* ios::app ? */ ) ;

  if(!newOccupant)
    cout << "Unable to create active lot file";
  else
    addToLot(newOccupant);
}
else
// ...
vijayan121 1,152 Posting Virtuoso

> Would still be interested in knowing why that was causing so much trouble...
the c++ standard requires that before any input operation on a streambuf is performed, a sentry object must be constructed. sentry defines a class that is responsible for doing exception safe prefix and suffix operations on a streambuf. it is a (commonly found) technical error to access the streambuf for extraction operations without a sentry being constructed first.
vc++ 6.0 pre-dates the c++ standard. my guess is that it is trying to access the streambuf without a sentry; in case the state is not good, tellg returns an incorrect result (there is no sentry to catch the error). and the string::subst throws std::out_of_range.
either skipping over the erraneous line or using a modern (conforming) compiler would make this particular error go away.

vijayan121 1,152 Posting Virtuoso

> the program didn't like my data file header, so I deleted it.

int main()
{
  int a =0, i=0,j=0;
  string DataStream1;
  string DataStream2;
  string DataStream3;
  vector<Weather> Earlier;
  vector<Weather> Later;
  vector <Weather> Search;

  cout<<"Pulling in data...Please wait...."<<endl;
  ifstream DataFile1("Earlier.txt");
  while(getline(DataFile1, DataStream1))
  {
    cout<<"ok"<<endl;
    istringstream Incoming(DataStream1);
    Weather w;
    Incoming >> w.years >> w.months >> w.days 
             >> w.hours >> w.windr >> w.windsp 
             >> w.gust >> w.waveht >> w.waveperiod;

    // this would be a little more robust;
    // skip the badly formatted lines in the file.
    // you might want to print out a diagnostic here
    if( !Incoming ) continue ; // skip the current line

    w.otherstuff = DataStream1.substr(Incoming.tellg());
    Earlier.push_back(w);

    // remove the return. you need the loop to execute for each 
    // line in the file; not quit after the first line is read 
    // return 0; 
  }	  
}

> .I am using Microsoft VC++ 6.0
consider switching to a modern compiler.
you can download VC++ 9.0 from http://www.microsoft.com/express/download/

vijayan121 1,152 Posting Virtuoso
//HERE IS THE ERROR BELOW...	  
w.otherstuff = DataStream1.substr(Incoming.tellg());
//****************/

is it a compiler error? if yes, what is the error diagnostic (message)? and which compiler are you using?

if no, what is the error? what do you expect will happen? and what is actually happening?

vijayan121 1,152 Posting Virtuoso
std::cout << 1.0/3 << '\t'
  
            << std::fixed << std::setprecision(2) 
            << 1.0/3 << '\t'
            
            << std::resetiosflags(std::ios::fixed)
            << std::setprecision(-1) << 1.0/3 << '\n' ;
vijayan121 1,152 Posting Virtuoso

this will change the third character of every line in infile.txt to 4 and put the result in outfile.txt sed 's/^\(..\)\(.\)/\14/' < infile.txt > outfile.txt

vijayan121 1,152 Posting Virtuoso

> I was creating the derived class method definitions in a cpp file
> and couldn't get the project to progressively compile correctly.
> At my first method definition, a default constructor,
> the compiler kept spitting out this error message:

this is perhaps the most obscure error message that gcc (actually the linker) spits out, but the reason is simple:

the compiler has to put the (one and only one) vtable for a class into some object file or the other. it puts it into the object file for the translation unit where the definition of the first non-pure-virtual out-of-line virtual member function is present. if there is no such definition, you get this rather unhelpful linker error message.

you would be able to progressively compile the code even if a non-pure virtual function is not defined, but to be able to link without errors, every such function must have a definition (at least a stub). and to prevent the non-creation of a v-table by the compiler, at least one of the non-pure virtual functions will have to be defined out-of-line.

gcc has a faq about this: http://gcc.gnu.org/faq.html#vtables


> Am I to assume that the example in the book, as regards the inline feature
> of a derived class destructor, is in error.
i think it is not a particularly good book, but in this case neither the book nor the compiler is in error.

superjacent commented: To the point and relevant advice, thank you. +1
vijayan121 1,152 Posting Virtuoso

the missing argument appears to be the one to initialize the style member. based on comments in the base class, i guess that you are expected to pass "vintage" as the style for VintagePort .

vijayan121 1,152 Posting Virtuoso

in the worst case, search() does take quadratic time, but on the average, it runs in linear time.
and for transform and find to work in all cases, both strings have to be transformed.
i agree that if both strings are mutable, transform, transform and find could be a little faster than search with a predicate.

vijayan121 1,152 Posting Virtuoso

if you use the algorithm std::search with a predicate, the string does not have to be transformed. will also work on const strings (with const_iterator).

#include <iostream>
#include <algorithm>
#include <cctype>

struct cmpnocase
{
  bool operator() ( char a, char b ) const
  { return std::toupper(a) == std::toupper(b) ; }
};

int main()
{
  std::string stf = "number[";
  std::string sts = "something NumBer[1]";
  std::string::iterator f = std::search( sts.begin(), sts.end(),
                       stf.begin(), stf.end(), cmpnocase() ) ;
  if( f != sts.end() ) 
    std::cout << "found: " << &*f << '\n' ;
}
vijayan121 1,152 Posting Virtuoso

you would also need to seed the linear congruential generator using srand .
a beginner's tutorial: http://www.cs.auckland.ac.nz/~jli023/c/RandomNumbers.html

vijayan121 1,152 Posting Virtuoso

> when i compile no errors but wen i build it i get 4 errors!
what are the errors? and which compiler are you using?

> if i want to hav the letter D il have to do the whole "*********\n", "** **\n", ..... thing is it??
yes, you need one pattern for each letter. and a look-up mechanism to get to the right pattern given a particular letter.

> im a super beginner!
perhaps you could attempt something simpler. eg. display a name like this:

***********************
 *                     *
 *  Aureliano Buendia  *
 *                     *
 ***********************
vijayan121 1,152 Posting Virtuoso

> how to display a letter with '*'s? for example, say the letter C
something like this, perhaps?

#include <iostream>

enum { HEIGHT = 8, WIDTH = 8 };

const char* const C_bitmap[HEIGHT] =
{
  "********\n", "********\n",
  "**\n", "**\n", "**\n", "**\n",
  "********\n", "********"
};

int main()
{
  for( int i=0 ; i<HEIGHT ; ++i )
    std::cout << C_bitmap[i] ;
  std::cout << '\n' ;
}
vijayan121 1,152 Posting Virtuoso

> need to know if it "acts like a record" for a single user.
yes, it does. a struct is an aggregate that collects related pieces of information together in one place.

> At the end users[userdb] is like an array for 6 struct user records? #define userdb 5 userdb is a manifest constant equivalent to a literal 5 ). users[userdb] is an array of 5 user records. users[0], users[1], users[2], users[3], users[4] .

> I need to have a simple database that stores 5 records ...
> ... search or over write certain records and or a field within that record.
> ... found a program that does pretty much what i want to do
just a glance at the #include statements would tell you that it is not a program that you should be using as an example.
perhaps you could have a look at http://www.lysator.liu.se/c/bwk-tutor.html#structure
note: if you decide to peruse more of kernighan's C tutorial, scroll all the way to the top and read the disclaimer first.

vijayan121 1,152 Posting Virtuoso

in the end, any program is just a file stored in some file system. compilers, linkers etc. are programs that take one or more files as input and give out file(s) as output. you can modify these files like you modify any other files.

#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
#include <iterator>
#include <vector>

int main( int argc, char** argv )
{
  std::system( "/usr/bin/g++ --version" ) ;
  std::string original = "This is free software; see the source "
                         "for copying conditions.  There is NO" ;
  std::string modified( original.rbegin(), original.rend() ) ;

  std::vector<char> bytes ;
  {
    std::ifstream file( "/usr/bin/g++", std::ios::binary ) ;
    file >> std::noskipws ;
    std::istream_iterator<char> begin(file), end ;
    bytes.assign( begin, end ) ;
  }

  std::vector<char>::iterator found =
       std::search( bytes.begin(), bytes.end(),
                    original.begin(), original.end() ) ;
  if( found != bytes.end() )
  {
    std::copy( modified.begin(), modified.end(), found ) ;
    {
      std::ofstream file( "/tmp/g++" ) ;
      std::copy( bytes.begin(), bytes.end(),
                 std::ostream_iterator<char>(file) ) ;
    }
    std::system( "/bin/chmod +x /tmp/g++ && /tmp/g++ --version" ) ;
  }
}
vijayan121 1,152 Posting Virtuoso

you may also find this (c++ annotations from groningen univ) useful.
http://www.icce.rug.nl/documents/cplusplus/cplusplus.html

and don't even think about looking at the code snippets in daniweb.

vijayan121 1,152 Posting Virtuoso

> why do you have to 'state' the <<n1+n2 if it has already been 'stated' before in that line?
in your c++ program, you have to say precisely what is it that you want done. cout << n1 << " + " << n2 << " = " 'states' that you want the value of n1, a + sign, the value of n2 and an = sign to be sent to stdout.
you also need to 'state' that the value of n1+n2 and a newline are to be sent to stdout after the = sign.

vijayan121 1,152 Posting Virtuoso

the standard c++ library exposes only one name std to the global namespace. everything else is put inside the namespace std .

change #include <iostream.h> to

#include <iostream>
using namespace std ;

without the using directive, you need to qualify names with std:: ie. std::cout etc.

vijayan121 1,152 Posting Virtuoso

even if you do not have a store for all primes < 100 (you do need an array or vector for that), you can use the knowledge that even numbers greater than 2 are not prime. and cut the number of times through the loop by roughly half:

bool prime (int n)
  {
      if( n < 2 ) return false ; // all versions need this 
      if( n == 2 ) return true ;
      if( n%2 == 0 ) return false ;
      int root = sqrt( double(n) ) + 1 ;

      for ( int z = 3 ; z<= root; z += 2 )
          if( n%z == 0 ) return false;

      return true;
}
Moporho commented: Brilliant! +1
vijayan121 1,152 Posting Virtuoso

hint:

char* punchmeinthehead( const char* first_name, 
                        const char* middle_name, 
                        const char* last_name )
{
   // ...
   // delete[] fullname ; // instead
   return fullname ;
}
vijayan121 1,152 Posting Virtuoso

> Sadly I have only C++ available to me
cheer up, i/o is much (much) easier in C++ than in C#.
something like this should get all the data in.

#include <fstream>
#include <string>
#include <sstream>
#include <vector>
using namespace std ;

struct Weather
{
    int years;
    int months;
    int days;
    int hours;
    int windr;
    double windsp;
    double gust;
    double waveht;
    double waveperiod;
    string otherstuff; // rest of line?
};


int main()
{

  string stuff;
  vector<Weather> Later ;

  ifstream DataFile("2007.txt");
  while( getline( DataFile, stuff ) )
  {
      istringstream stm(stuff) ;
      Weather w ;
      stm >> w.years >> w.months >> w.days >> w.windr
          >> w.windsp >> w.gust >> w.waveht >> w.waveperiod ;
      w.otherstuff = stuff.substr( stm.tellg() ) ;
      Later.push_back(w) ;
  }
  // no of lines read is Later.size()
  // Weather structures are stored in Later[0], Later[1] etc.
}

some of the variables in your struct must be double instead of int . they are real numbers in the file.
also prefer using a vector to an array. that way, you are not limited by hard-coded limits like 75000 .the vector resizes itself as required.

vijayan121 1,152 Posting Virtuoso

O( N log N ) would be greater than O(M) for *any* N and M
if M/N < log N

vijayan121 1,152 Posting Virtuoso

> The problem is that it's too exact. Randomly chosen combinations
> are not likely to have their numbers lined up so perfectly.
true enough.

> The total cost is then O(n log n)
so, it means for large N, if M/N < log N, the tree is the way to go.
otherwise go for the old O(M) algorithm.

vijayan121 1,152 Posting Virtuoso

> but you need to go as high as the square root of n.
you need to test for divisibility only by *prime* numbers up to the root of n.
if you precompute and store all primes upto 100, testing if a number less than 10000 is prime would be even faster.

vijayan121 1,152 Posting Virtuoso

> You need to either select the number with respect to the probability distribution
> of the slot you're going to fill,
yeah. it is not exact. for example, if M==1000 and N==100, if the random number chosen is 253, place it in position 25 of the array. 0-252 is now the range for 25 positions(0-24) and 254-999 is the range for 74 positions. it is approximate, but 253/25 is roughly equal to 746/74.

vijayan121 1,152 Posting Virtuoso

> My algorithm is a little bit different.

>> The trouble with that algorithm is that it doesn't uniformly select
>> all the possible combinations of numbers; it's biased.

actually it is a very good algorithm. with a simple adjustment, the bias can be (approximately) removed. instead of placing the selected number into the middle of the array, place it in a location such that the remaining numbers are (approximately) equally probable. there would be a slight skew because we need to map a larger integer range to a smaller one; but performance could not be bettered ( O(N) instead of O(M) for M>N ).

void fill_with_unique_rand( int* array, int N,
                            int min_value, int max_value )
{
   int M =  max_value - min_value + 1 ;
   int r = M==0 ? 0 : rand()%M ;
   int v = min_value + r ;
   if( N == 1 ) array[0] = v ;
   else
   {
     int i = double(r)/M * N + 0.5 ;
     array[ i ] = v ;
     if( i>0 ) 
      fill_with_unique_rand( array, i, min_value, v-1 ) ;
     if( i<(N-1) ) 
      fill_with_unique_rand( array+i+1, N-i-1, v+1, max_value ) ;
   }
}
vijayan121 1,152 Posting Virtuoso

> That's boring though.
yeah. it also doesn't have enough meat for submission as a solution to an assignment.

vijayan121 1,152 Posting Virtuoso

> What does 'M' represent?
M is the cardinality of the set of numbers out of which we have to choose N *unique* numbers. (M>=N)

> Any chance you could give us more explanations?
a. probability that the first number is chosen is N/M b. probability that the second number is chosen is (N-1)/(M-1) if the first was chosen, N/(M-1) if it was not.
ie. N/M * (N-1)/(M-1) + (M-N)/M * N/(M-1) == N/M and so on.

here is a java transliteration of the alogorithm (caveat: java is a foreign language to me, there may be syntax errors. but the logic is sound).

public class unique_rand_generator 
{
	// fill up array with unique random integers 
	// from the range min_value .. max_value
        // invariant: max_value - min_value + 1 (M) 
        //      >= array.length (N)
    public static void fill( int[] array, int min_value, 
                             int max_value )
	{
	  int available = max_value - min_value + 1 ;
	  int required = array.length ;
          Random rng = new Random() ;
	  for( int i=0 ; i<available ; ++i )
	  {
		// we have to choose required
                // out of the remaining (available-i)
	    if( ( rng.nextInt(available-i) ) < required )   
	    {
	      --required ;
	      array[required] = min_value + i ;
	    }
	  }
	}
}
vijayan121 1,152 Posting Virtuoso

std::partition with a predicate could be used to split the sequence.

#include <iostream>
#include <algorithm>
#include <functional>
#include <iterator>
#include <vector>
#include <cstdlib>
#include <ctime>
using namespace std ;

template< typename ITER > inline
void column_print( ITER beginc1, ITER endc1,
                   ITER beginc2, ITER endc2 )
{
   while( (beginc1!= endc1) || (beginc2!= endc2) )
   {
      if( beginc1 != endc1 ) cout << *beginc1++ ;
      cout << "\t\t" ;
      if( beginc2 != endc2 ) cout << *beginc2++ ;
      cout << '\n' ;
   }
}

int main()
{
   srand( unsigned( time(0) ) ) ;
   std::vector<int> v;
   for ( int i = 0; i < 20; i++ )
         v.push_back ( std::rand() % 100 ) ;
   vector<int>::iterator part = partition( v.begin(), v.end(),
                       not1( bind2nd( modulus<int>(), 2 ) ) ) ;
   column_print( v.begin(), part, part, v.end() ) ;
}
vijayan121 1,152 Posting Virtuoso

> Is there a WM_XXXXX event or something that I can call my handleData with?
> something like a WM_SOCKET that works with a PPC?
unfortunately no. the win32 WSAAsyncSelect function http://msdn2.microsoft.com/en-us/library/ms741540.aspx does this, but AFAIK, it isn't available for Windows Mobile.

> handleData - which will process reads and closes, etc looks like it will work,
> but I still don't know how to call this function.
easiest way is to create another thread using CreateThread http://msdn2.microsoft.com/en-us/library/bb202727.aspx and in that thread, issue a WSAWaitForMultipleEvents http://msdn2.microsoft.com/en-us/library/aa922422.aspx
to wait for a network event to occur. when the event arrives, call handleData to handle it.

vijayan121 1,152 Posting Virtuoso

you are encountering the strange behaviour because of this function (ideally it should not have compiled at all):

bool validity_check(int level,int n, int current[])
{
        for(int i=0;i<level;i++)
        {
                if(current[i]==n) return false;
        }
        //Level 0 has always full validity.
        if((level>0)&&(level<order-1))
        {
                if((current[level-1]==n)|(current[level+1]==n)) return false;
        }
        if(level==order-1)
        {
                if(current[level-1]==n) return false;
        }
        // warning: control reaches end of non-void function
        //return true ; // add this and the strange behaviour will dissappear
}

at the place where the function is called,

while(true)
        {
                //cout<<"Entered the while loop"<<endl; //This is the line which can change the whole output when uncommented
                for(int i=nextjump[level];i<order;i++)
                {
                        if(validity_check(level,i,position))
                        // the contents of the eax register at this 
                        // point determines wheateher the result 
                        // is true or false
                        // since the function does not return a value
                        // what eax will be on return is influenced 
                        // the state of the registers before the call
                        // apparently, the cout << ... 
                        // causes eax to be non-zero on 
                        // exit from the function. 
                        // cout << ... returns a reference to cout (in eax)
                        // non-zero; the if condition evaluates to true
                        // without the cout << ... eax is zero 
                        // and the if condition evaluates to false.

                        {
                            // ...

moral of the story: compile with standard conformance and all warnings enabled. and pay attention to the warnings.
use something like: > g++ -Wall -std=c++98 -pedantic -Werror myprogram.cc (g++ is really bad if you just settle for the defaults.)

note: this …

agrawalashishku commented: Very Very helpful. I am grateful +2
vijayan121 1,152 Posting Virtuoso

WSAEventSelect http://msdn2.microsoft.com/en-us/library/aa922328.aspx is probably what you are looking for.
googling on WSAEventSelect tutorial/sample should fetch you quite a few hits. here are two of them
http://www.codersource.net/winsock_tutorial_server_event_model.html
http://www.codeproject.com/KB/IP/networkevents.aspx

vijayan121 1,152 Posting Virtuoso

not > g++ functions.o * .cpp but > g++ functions.o *.cpp (no space after *)
better: > g++ -std=c++98 -Wall functions.o *.cpp

vijayan121 1,152 Posting Virtuoso

were you looking for a standard library function?
like std::accumulate ( in header <numeric> )

// vec is a std::vector<int> and N <= vec.size()
int sum = std::accumulate( vec.begin(), vec.begin()+N, 0 ) ;
// average == sum / double(N)
VernonDozier commented: Useful function. +1
hammerhead commented: nice one +2
vijayan121 1,152 Posting Virtuoso

to get the effect you want (not to uppercase after preceding words) i think it should be

if( ( is_ws_delimited(line,pos,noun.size()) &&
    [B]![/B]has_preceding_word( line, pos, prec_words )))
vijayan121 1,152 Posting Virtuoso

EBCDIC is by no means dead and buried. IBM's flagship operating systems z/OS http://www-03.ibm.com/systems/z/os/zos/index.html and i5/OS http://www-03.ibm.com/systems/i/os/i5os/ use EBCDIC
IBM's older mainframe operating syatems like OS/390, VM and VSE, and minicomputer operating systems like OS/400 also use EBCDIC.
non-IBM operating systems that use EBCDIC are rare; Fujitsu-Siemens' BS2000/OSD, Unisys MCP, HP MPE/iX etc.

vijayan121 1,152 Posting Virtuoso

here it checks that noun appears in line as a complete word starting at pos

void process_noun( string& line, const string& noun,
                   const vector<string>& prec_words )
{
  size_type pos = 0;
  while( ( pos = line.find( noun, pos ) ) != string::npos )
  {
    if( is_ws_delimited(line,pos,noun.size()) &&
        has_preceding_word( line, pos, prec_words ) )
            line[pos] = toupper( line[pos] ) ;
    pos += noun.size() ;
  }

and here it checks that precede appears in line as a complete word starting at loc

bool does_precede( const string& line, size_type pos,
                   const string& precede )
{
  size_type loc = line.rfind ( precede, pos );
  if( ( loc != string::npos ) &&
      is_ws_delimited(line,loc,precede.size()) )
  {
    loc += precede.size() ;
    if( loc >= pos ) return false ;
    for( size_type i = loc ; i<pos ; ++i )
    {
      if( !isspace( line[i] ) )
         return false ;
    }
    return true ;
  }
  return false ;
}
vijayan121 1,152 Posting Virtuoso
[B]std::complex<float> Z[N][P][M] ;[/B]
[B]Z[/B] is an array of [B]N[/B] elements,
    each element of which is an array of [B]P[/B] elements,
        each element of which is an array of [B]M[/B] elements,
            each element of which is a [B]std::complex<float>[/B]

std::complex<float> (*W)[M] = &Z[k][0] ;

W is a pointer to an array of M elements, each of which is a std::complex<float>

&Z[k][0] is the address of an array of M elements, each element being a std::complex<float>. this address is used to initialize W.

that is the meaning of the statement (incidentally, int i = 100 ; is *not* an expression.)
what its significance is, or why someone would want to do this, i have no idea.

vijayan121 1,152 Posting Virtuoso

> i dont understand the purpose of "nchars" bool is_ws_delimited( const string& line, size_type pos, size_type nchars ) returns true if the sequence of nchars characters starting at position pos in string line is a complete word (ie. delimited by whitespace at either end).
the idea is that if the noun is heart and a preceding word is lion , then abcd efghi lion heart jkl mnop is a match, but abcd efghi vermilion hearthrug jkl mnop is not.

vijayan121 1,152 Posting Virtuoso

define a string literal

const char* const a_to_z = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;

generate a random integer ri in the range 0 to 25 (inclusive).
use a_to_z[ri] as the random letter.
note: *do not* do this: char( 'A' + ri )

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

> which one is better?
usually const char* const OUT_STRING = "hello world" ; > why?
more efficient.
exception: you need to pass it often to functions which expect a const std::string&

vijayan121 1,152 Posting Virtuoso

> Its true that a function would simplify a lot.
> I just have no idea how start it.
breaking it up into many small functions would simplify it even more. if the functions have meaningful names, the code would also be somewhat self-documenting.
this is one way of doing it (caveat: untested).

#include <string>
#include <iostream>
#include <fstream>
#include <vector>
#include <ctype.h>
using namespace std;

typedef string::size_type size_type ;

void fill_vector( const char* file_name, vector<string>& vec )
{
  ifstream file( file_name ) ;
  string str ;
  while( getline( file, str ) ) vec.push_back(str) ;
}

bool is_ws_delimited( const string& line, size_type pos,
                      size_type nchars )
{
  if( (pos!=0) && !isspace( line[pos-1] ) ) return false ;
  if( ( pos+nchars < line.size() ) &&
        !isspace( line[pos+nchars] ) ) return false ;
  return true ;
}

bool does_precede( const string& line, size_type pos,
                   const string& precede )
{
  size_type loc = line.rfind ( precede, pos );
  if( ( loc != string::npos ) &&
      is_ws_delimited(line,loc,precede.size()) )
  {
    loc += precede.size() ;
    if( loc >= pos ) return false ;
    for( size_type i = loc ; i<pos ; ++i )
    {
      if( !isspace( line[i] ) )
         return false ;
    }
    return true ;
  }
  return false ;
}

bool has_preceding_word( const string& line, size_type pos,
                         const vector<string>& prec_words )
{
  for( size_t i = 0 ; i < prec_words.size() ; ++i )
  {
    if( does_precede( line, pos, prec_words[i] ) )
       return true ;
  }
  return false ;
}

void …
vijayan121 1,152 Posting Virtuoso

you may find this link interesting. http://msdn2.microsoft.com/en-us/library/ms724423(VS.85).aspx