Duoas 1,025 Postaholic Featured Poster

Good grief!

n.aggel, you had the right idea to begin with, only your syntax needs help. Don't change the definition of the function, but just pass the appropriate pointer.

void array_function(int rows, int cols, float after[rows][cols] , float before[rows][cols])
  {
  ...
  }

int main()
  {
  int n = 5;
  int m = 6;

  int before[n][m];
  int after[n][m];

  array_function( 5, 6, before, after );  // this works
  array_function( 5, 6, after, before );  // this works

  ...
  }

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

Please use code tags. It is easier on my poor eyeballs.

There are issues involved with reading and writing to the same file. To do what you want to do, it is best to make a temporary file, then replace the original.

#include <iostream>
#include <fstream>

#include <cstdio>  // for tmpnam(), remove(), and rename()

using namespace std;

int main()
  {
  string filename = "tempfile.txt";
  string tempname = tmpnam( NULL );

  ifstream source;
  ofstream destination;
  string   line;

  try {
    if (!source.open( filename.c_str() ))
      throw string( "Could not open " ) +filename;

    if (!destination.open( tempname.c_str )
      throw string( "Could not open " ) +tempname;

    // Skip the first three lines
    for (int counter = 0; counter < 3; counter++)
      getline( source, line );

    // Copy the remainder
    while (getline( source, line )) destination << line;

    destination.close();
    source.close();

    // Replace the source file with the destination
    if (remove( filename.c_str() ) != 0)
      {
      remove( tempname.c_str() );
      throw string( "Could not overwrite to " ) +filename;
      }

    if (rename( tempname.c_str(), filename.c_str() ) != 0)
      throw string( "Could not rename " ) +tempname +" to " +filename;

    cout << "Success!\n";

  catch (string s) {
    destination.close();
    source.close();
    cerr << s << endl;
    return EXIT_FAILURE;
    }

  return EXIT_SUCCESS;
  }

Also, don't use ASSERT() for runtime errors. I personally believe that the macro is over-used, but whether anyone agrees or not it is still a Bad Thing to simply crash the program if something that can be expected to happen (even if we would prefer it …

Duoas 1,025 Postaholic Featured Poster

No.

(If you want to get technical, it depends on the quality of the compiler. But the most difference it will cause is a couple cycles, which is infinitesimal.)

Duoas 1,025 Postaholic Featured Poster

[edit]
Argh. Sorry about leaving out <limits>. (I forgot and GCC doesn't complain.)

I come from a Pascal background so I tend to like things you can actually read... I didn't know that VC++ doesn't understand and or or. (Stupid VC++.)

You should be very pleased with the results. The algorithm is very quick, and typically needs to examine only a very few lines before finding the answer.

I also forgot to mention earlier that lines that do not begin with a valid date of the form MM/DD/YYYY or MM-DD-YYYY are completely ignored, so it is OK if your file contains blank lines or other information.

Enjoy. Thanks a ton AD. You rock!

Duoas 1,025 Postaholic Featured Poster

I think he wants to know how to access the struct/record elements.

As Micheus explained, both reference the same memory. The only difference is how they treat that memory.

bp thinks the memory is just a list of bytes.

pTcpHdr thinks the memory is a record containing information about a Tcp Packet (and it would be right). I was careful in the example not to change the pTcpHdr variable, so you can still access the packet header by dereferencing it: pTcpHdr^.Options := 42; Hope this helps.

Duoas 1,025 Postaholic Featured Poster

I don't know if you downloaded them correctly. They are plain-text files, not HTML or anything else. If you open them in notepad you shouldn't see anything funny.

Go to the download, click download. When you get the web page showing the file, select File --> Save Page As... and make sure the bottom-most drop-down box says the document type is "Text Document".

If you've done that (and I see that you put the include in the right spot), then you'll have to ask AD for more help. (Stupid MS VC++.)

Let me go dig out my old version of VC++ and see what weirdness it wants...

(Alas, I was looking forward to a happy you.)

Duoas 1,025 Postaholic Featured Poster

42

Duoas 1,025 Postaholic Featured Poster

It wasn't the problem but it is a problem.

No, you can't send your project to me. My mailbox is clogged and I just don't care that much. (Sorry)

The compiler is complaining because there is something wrong with the type of rooms[[B][I]n[/I][/B]].

In C++, the == operator typically requires a const reference to a known or unambiguously promotable type.

Make sure that the type of rooms doesn't change and is explicitly declared in all scopes. Also check that you haven't included some header that has messed around with the definition of NULL.

If you are using the GCC, make sure you are compiling with g++ and not gcc.

Good luck.

Duoas 1,025 Postaholic Featured Poster

guest7, you've already made several unseemly blunders, but now you are risking being completely ignored.

Please read up before becoming belligerent.
How To Ask Questions The Smart Way

Duoas 1,025 Postaholic Featured Poster

Yoink.

OK, one last one, just for the weird operator concerns:

procedure XOREncrypt( buffer: PByteArray; length: longword; key: PByteArray );
  var
    i, j: cardinal;
  begin
  for i := 0 to (length div 8) -1 do
  for j := 0 to 8 -1 do
    buffer[ (i *8) +j ] := buffer[ (i *8) +j ] xor key[ j ]
  end;
// Calculate padding length
padding := 8 -(pVpnHdr^.m_Length mod 8);

// Zero initialize padding
http://www.daniweb.com/forums/post608298-8.html

// Encrypt packet
bp := pVpnHdr;
inc( bp, sizeof( pVpnHdr ) );
XOREncrypt( bp, pVpnHdr^.m_Length +padding, pVpnHdr^.m_Key );

Enjoy!

Duoas 1,025 Postaholic Featured Poster

Messy.

var
  bp: PByte;
begin
  bp := pVpnHdr;
  inc( bp, sizeof( pVpnHdr ) +pVpnHdr^.m_Length );
  fillchar( bp^, padding, 0 )
end;

Micheus already gave you the basics of this above.

Duoas 1,025 Postaholic Featured Poster

That C code is a mess. And it is dangerous because it uses bitfields (which C++ does not guarantee to be packed in any specific order -- so results vary by compiler!).

Alas, why don't programmers stp usng stpd shrt nonsns nms.

Anyway:

uses
  SysUtils,  // for PByteArray
  Winsock;   // for htons()

const
  IP_DF = $4000;  // don't fragment flag
  IP_MF = $2000;  // more fragments flag

type
  in_addr = record ... end;  // you must fill this in

  iphdr_ptr = ^iphdr;
  iphdr = packed record
    ip_hl__ip_v: byte;      // header length, version: four bits each
    ip_tos:      byte;      // type of service
    ip_len:      shortint;  // total length
    ip_id:       word;      // identification
    ip_off:      shortint;  // fragment offset field
    ip_ttl:      byte;      // time to live
    ip_p:        byte;      // protocol
    ip_sum:      word;      // checksum
    ip_src:      in_addr;   // source address
    ip_dst:      in_addr    // destination address
    end;

procedure RecalculateIPChecksum( pIpHeader: iphdr_ptr );
  var
    sum:    longword;
    i:      longword;
    buff:   PByteArray;  // see note 1
  begin
  sum := 0;
  i   := 0;

  // Initialize checksum to zero
  pIpHeader^.ip_sum := 0;
  buff := pIpHeader;

  // Calculate IP header checksum
  while i < (pIpHeader^.ip_hl__ip_v and $F) *sizeof( longword ) do  // see note 2
    begin
    inc( sum, (buff[ i ] shl 8) +buff[ i +1 ] );
    inc( i, 2 )
    end;

  // Keep only the last 16 bits of the 32 bit calculated sum and add the carries
  while (sum shr 16) <> 0 do
    sum := (sum and $FFFF) +(sum shr 16);

  // Take the one's …
Duoas 1,025 Postaholic Featured Poster

Yes, it should be cast to a BytePtr. If you don't then anything you add to it is multiplied by the size of the object's elements.

type 
  pPoint = ^tPoint;
  tPoint: record x, y: integer end;

function get_point( points: pPoint; index: integer ): tPoint;
  begin
  inc( points, index );  // same as in C (points += index)
  result := points^
  end;

function get_point( points: pPoint; index: integer ): tPoint;
  var p: BytePtr;
  begin
  p := pointer( points );
  inc( p, index *sizeof( tPoint ) );
  points := pointer( p );
  result := points^
  end;

The first version works the same as in C and C++. If the size of the pointer's target is known, it is automatically multiplied into the index:

int *a;
a[ 5 ] == (a +5)

If, however, the offset calculation is in bytes you must first cast it to a byte pointer:

int *a;
a[ 5 ] == ((unsigned char*)a +(5 *sizeof( a[ 0 ] )))

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

Start here.

When you hit the wall post your code and we'll help further.

Duoas 1,025 Postaholic Featured Poster

It could be quite a number of things. I'll need to see a lot more of your code.

You haven't been overloading the == operator in funny ways or anything?

Duoas 1,025 Postaholic Featured Poster

OK, sorry for the public service... (I also lost my webspace a short while back...)

Don't forget to read my comments in the last post.

datefile.hpp
datefile.cpp
a.cc

Just copy the datefile files into your project folder and add them to your project.
Make sure to #include "datefile.hpp" in your program (in the files where you intend to use it).

Heh...

Duoas 1,025 Postaholic Featured Poster

Alright! :)

Sorry for the delay. There were a few more corner cases than I thought there would be when I started this. But anyway, here you go. Hope you find it useful.

The algorithm presumes that the dates in your file are more or less linearly distributed. If this is not the case by more than a standard deviation, open "datefile.cpp" and comment out the line #define LINEAR_DISTRIBUTION Sorry for the boilerplate, but companies get nervous without it. Basically it just says you can do anything you like with the files except claim anyone but I wrote the original versions or alter the boilerplate... (and excludes me from legal responsibility if someone manages to destroy something with it).

The file "a.cc" is what I used to test the datefile algorithm. You don't need it, but I've attached it anyway so you can play with it if you like. Its messy though...

Let me know if anything goes horribly wrong. ;)

So, here's a quick primer:

#include <iostream>
#include <fstream>

#include "datefile.hpp"

using namespace std;
using namespace datefile;

int main()
  {
  ifstream megafile( ... );

  time_t date = string_to_date( "4/28/1974" );
  streampos linepos = find_date( megafile, date );

  if (megafile)  // or (!megafile.eof())
    {
    string line;
    getline( megafile.seekg( linepos ), line );
    cout << "Found the line> " << line << endl;
    }
  else 
    {
    megafile.clear();
    cout << "No such date found.\n";
    }

  ...
  megafile.close();
  return 0;
  }

Enjoy! :)


Hmm. …

jephthah commented: very nice code +1
Duoas 1,025 Postaholic Featured Poster

It might be worth your interest to check out Boost RegExp (which is a lot easier to use than that scary-looking website might make you feel), or GNU RegExp.

Or, for a simple program you might want to check out Tcl/Tk, which excels at handling strings and has a powerful regexp.

If you are on *nix you can use utilities like gawk and grep to do the same.

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

You are looking to do something pretty tricky. I can help you on Windows, but on another OS you might be up the creek without a paddle...

What OS are you using? And how is the number displayed (in a window or on the console or in a line edit or what)?

Duoas 1,025 Postaholic Featured Poster

Text mode is good (makes things simpler).

Give me just a little longer to finish testing all the various corner cases where things can go wrong.

Duoas 1,025 Postaholic Featured Poster

Actually, you got me interested, so I'm writing a little library for you to do it... :)

My method accounts for the possibility that lines are different lengths.
As a matter of interest, do you open the file in textual or binary mode?

Duoas 1,025 Postaholic Featured Poster

If you know something about the distribution of dates in the file you can get the fastest time, but in any case you'll have to do a random-access search.

This should help, but may not if the date is in a worst-case position:

  1. read the first date in the file
  2. seek to the end of the file, backpedal until you have the last (non-empty) line of the file, and read that date.
  3. calculate the percentage that your target date falls between the first and last date of the file.
  4. jump to that position in the file and scan forward until you are at the beginning of a line.
  5. read the date.
  6. compare the date with the target date.
    1. if the date is what you want, you're done.
    2. if the date is too late, calculate the new percentage to be halfway between the new position and the current 'beginning position' (which will be the beginning of the file the very first time you do this).
    3. if the date is too early, calcuate the new percentage to be halfway between the new position and the current 'ending position' (which will be the end of the file the very first time you do this).
    4. reset your new beginning and ending positions.
  7. goto 4.

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

Argh. I'm so sorry. There is something wrong... Your base type is a pointer, and the iterator is a pointer, so you need to dereference it twice (and I had you doing it only once... cout << (*j)->getValue() << endl; Sorry... :$

Duoas 1,025 Postaholic Featured Poster

The problem is exactly what it is saying.
For example, you have a procedure prototyped as: [B]void[/B] GetTemperatures ([B]int[/B] Temperatures[], [B]int[/B] NumTemperatures); ...which clearly states that it requires two arguments.

but in main() you don't specify any arguments: GetTemperatures (); You must specify the arguments: GetTemperatures( HourlyTemperaturs, NumTemperatures ); I think the problem is that you are confusing multiple variables with the same name as a single variable. For example:

int answer = 42;

void print( int answer )
  {
  cout << "The answer is " << answer << endl;
  }

In this example, there are two variables named 'answer'. The first is a global variable and it has the value 42. The second is a temporary variable local to print(). In other words, it exists inside the function but nowhere else. Since it has the same name as the global variable, the local variable takes precedence (meaning you cannot use the global variable inside the function).

Hope this helps.

PS. You could use the global if you give its full name: cout << [B]::answer[/B] << endl;

Metalsiege commented: Key to helping me solve my problem. Thanks! +1
Duoas 1,025 Postaholic Featured Poster

Well, there's nothing wrong with what I've seen. Post a complete example.
(I think the most likely problem is that you have some dangling pointers in your vector.)

Duoas 1,025 Postaholic Featured Poster

Start it in the DPR file between Application.CreateForm(...) and Application.Run.

? best guess...

Duoas 1,025 Postaholic Featured Poster

It is a possibility. It occurs to me that there might be something happening with the paging file or somesuch...

I wrote the program to operate off of a CD (via autoplay). I got the display rate sufficient to use, and the time came for me to give it to my friend (as a birthday gift), so I burnt it to CD and tested the CD, and the display rate went up to about 13 FPS.

Alas.

Duoas 1,025 Postaholic Featured Poster

'Quux' is a metasyntactic variable. Replace it with whatever the name of your class. Just like 'whatever': replace it with the name of your list/vector/array/whatever.

[edit]Since you have no reason to keep the comparison function private, make it public.

Duoas 1,025 Postaholic Featured Poster

Er, sorry, I wasn't paying enough attention. Don't use at().

cout << j->getValue();

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

If you know your target value will fit in a smaller integer, you really don't need to worry about endianness. Do as tesuji suggests and let the compiler do the appropriate movement:

fputc( unsignedlongvar, file );  // bits 8..31 are lost!

Don't use fwrite() for size-specific I/O. The C and C++ standards require a char to be one byte (eight bits), but there is no requirement on internal representation. So it is possible to find some funky system/compiler that stores them in word values... and if the endianness isn't strictly little-endian, then using fwrite() won't work.

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

If you are sorting on private or protected class elements then you need the comparison function (whether it is an overloaded < or a normal function you pass to the sort() algorithm) to be a friend of the class.

Likewise, as to your explicit question, the comparison function itself must be visible to the scope in which it is used. If you really want to keep it private, make your class have a sort() function:

class Quux
  {
  private:
    vector<string> whatever;

    bool compare_on_ascending_value(
      const string& s1,
      const string& s2
      ) const
      {
      float f1, f2;
      stringstream str1(s1);
      stringstream str2(s2);
      str1 >> f1;
      str2 >> f2;
      return (f1 > f2);
      }

  public:
    void sort()
      {
      std::sort(
        whatever.begin(),
        whatever.end(),
        compare_on_ascending_value
        );
      }
  };

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

Get rid of the int.

You have two variables named 'j' there: your iterator, and an int local to the for loop.

vector<Number*>::iterator j;
for (j = v1.begin(); j<v1.end(); j++)
  cout<<v1.at(*j)->getValue();

Don't forget proper indentation!

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

You're playing with deep stuff.

Google "msdn task scheduler and take a look at the task scheduler scripting objects and interfaces (depending on how you want to do it).

You'll need admin rights to do it.

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

The pipe is like any other file/stream device. You can read at any time after the other end writes and you'll be OK.

Of course, for something like a plug-in for Arena you do want to minimize the time it takes to notice that there is input waiting to be read, but from your description it looks like that isn't a problem.

:)

Duoas 1,025 Postaholic Featured Poster

I'm confused at what you want.

"3A" > "20A" > "-3A" > "-20A"

The algorithm is working properly.

[edit]
Oh, I get it.

Remember, strings and numbers are not the same thing. You'll have to convert to numbers and sort on that.

Duoas 1,025 Postaholic Featured Poster

Perhaps he would like to use some of the power of STL streams to parse the string.

Use an istringstream.

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

int main() {
  using namespace std;

  string full_name;
  string first_name;

  cout << "Please enter your full name> ";
  getline( cin, full_name );

  // Get the first name out of the string
  {
    istringstream ss( full_name );
    ss >> ws;
    getline( ss, first_name, ' ' );
  }

  cout << "Hello " << first_name << endl;

  return EXIT_SUCCESS;
  }

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

Alas, I'm used to doing this in Delphi. Sorry for the syntax problems in C++.

Duoas 1,025 Postaholic Featured Poster

Ed is close. Jennifer is using C++Builder.

You can set Form2::[B]VertScrollBar[/B]->[B]Position[/B] = 0; My apologies on ClientOrigin. I just looked it up and it is a read-only property. Alas.

Keep in mind my capitalization might be a little off.

I admire your tenacity and efforts, but either your documentation really stinks or you still need to work on searching it. (I just got BDS 2006 and it's documentation really stinks.)

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

In Delphi (as in C++, etc.), a reference is really a veiled pointer. Hence, when you declare a variable of type var TMyObject: MyObject; space is not reserved for the entire object; rather, MyObject is a pointer to a TMyObject.

The compiler is smart enough to know the difference, but allows for the possibility that the programmer knows something it doesn't when you use the second syntax you illustrated. However, unless you really do know something funny about your variable, you should stick to the first syntax.

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

I'm not sure what is wrong. The read procedure should be using ReadFile behind the scenes. And an anonymous pipe doesn't have any significant caveats...

I think it is more likely that the new thread is crashing when you call ShowMessage. Typically you want to keep all GUI handling in the main thread. I haven't done much multi-threaded programming, and I don't remember all the details as to why this is so... but I think that's your problem and not the pipe.

Sorry I couldn't be of more help.

Duoas 1,025 Postaholic Featured Poster

I see no error. What exactly does the error message say?

Duoas 1,025 Postaholic Featured Poster

I think I missing something ?

Yes, you are missing all the words I put in bold in my first response.

Duoas 1,025 Postaholic Featured Poster

Typically all the forms you create in the IDE are automatically created when your application starts. (You can choose which forms are auto-created from the Project->Options menu.) You can see the code that does it by choosing Project->View Source (to see the WinMain() function).

What appears to be happening is that you are Free()ing the form when it is closed, and you can no longer Show() an object that has been destroyed.

From what I understand that you want to do, you should go to the project options and remove the Form2 from the auto-create list. (You can also delete the Form2 variable from the unit source file.)
Next, in your button click handler, (1) create a new instance of the form and (2) show it.

System::Void Form1::button5_Click(System::Object^  sender, System::EventArgs^  e)
{
  TForm2 newForm;
  Application->CreateForm( TForm2, newForm );  // (1)
  newForm.Show();  // (2)
}

Now you can click the button to get as many copies of TForm2 as you want, and closing each one should still delete the individual instance.

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

You've got the wrong forum.
Click the link at the top for "Web Development" and choose an appropriate forum there.

Good luck.

Duoas 1,025 Postaholic Featured Poster

Are you talking about making your program provide a scripting language for plug-ins?

Duoas 1,025 Postaholic Featured Poster

Yep.

You can set the form's ClientOrigin property (which is a tPoint)

or

You can set the position property of the form's HorzScrollBar and/or VertScrollBar.

Have fun!

Duoas 1,025 Postaholic Featured Poster

You can't overload the + operator to be const unless you change the operation to not modify *this. In other words, if you change *this, get rid of the second const.

The + operator typically is expected to not modify the b object (as vijayan121 explained with a = b.operator+(c) ; ).

Hence, the typical layout of such a function is:

Rectangle Rectangle::operator + (const Rectangle& rect) const
  {
  Rectangle result;
  result.x = this->x + rect.x;
  ...
  return result;
  }

Unless your object uses a lot of memory or other resources or has existential side-effects, I wouldn't really worry too much about temporaries. For something like your Rectangle, the difference is negligible: copying is fast and often optimized away.

Hope this helps.

[edit]
Oh yeah, often, to avoid duplicating code, the + operator is often written in terms of the += operator:

Rectangle Rectangle::operator + (const Rectangle& rect) const
  {
  return (Rectangle( *this ) += rect);
  }
Duoas 1,025 Postaholic Featured Poster

Heh, glad to have helped.

I know it takes a while to get your head around it, but the only way it will come is to struggle along... alas. But it will come, don't worry. :)

One of the most overlooked features in Delphi (IMHO) is the use of try...except blocks in data transformation. In simple terms, endeavor to do things one way. If the input doesn't match, raise an exception and catch that to try another way. Keep going until you either run out of ways to try or until the input matches.

Good luck.

Duoas 1,025 Postaholic Featured Poster

Can you tell us the type of pipe and the operational modes of each end?

I suspect that you have got an unstable combination and/or buffer lag.

Duoas 1,025 Postaholic Featured Poster