Hamrick 150 Posting Whiz

You can only access an object if it was declared in the same scope or an enclosing scope. That's the techno babble explanation. :) What it means is that if you declare something inside a loop, you can't use it outside the loop because the body of a loop counts as its own scope.

int i;

for ( i = 0; i < 10; ++i ) {
  int x = i;
}

printf( "%d\n", x ); /* Won't work! */

To get to x you have to declare it in at least the scope that you're using it.

int x;
int i;

for ( i = 0; i < 10; ++i ) {
  x = i; /* Still works! */
}

printf( "%d\n", x ); /* Works now! */

By at least the scope you're using it in, that means it can be declared in a higher enclosing scope and you can use it in the nested scope. That's why x = i; still works even though x is declared in the enclosing scope. :)

Hamrick 150 Posting Whiz

I think you have to do something like this because int** isn't a compatible type with void**.

int i = 123;
int *p = &i;

void pop( void **data ) {
  *data = p;
}

int main() {
  int *q;
  void *pv;

  pop( &pv );
  q = static_cast<int*>( pv );

  return 0;
}
Hamrick 150 Posting Whiz

i did but then i found out that c++ passes all arrays by reference by default.

You have to be careful here. 'By reference' could mean two things, and only one of them is right. ;) Thef irst by reference is an actual reference in C++ and arrays are not passed by that reference by default. To pass an array by actual reference in C++ you do it like Dave said.

void Pizza::set ( int x , int y , int (&a)[7] , int i )

The second by reference is a fake reference that you get by passing a pointer by value and being able to get to the thing that the pointer points to. That's the default for arrays and that's why all of these are the same prototype.

void Pizza::set ( int x , int y , int a[7] , int i )
void Pizza::set ( int x , int y , int a[] , int i )
void Pizza::set ( int x , int y , int *a , int i )

Woohoo! Finally something I know something about. :D

Hamrick 150 Posting Whiz
toppings[z] = a[z] ;

If a is declared as a plain int, why are you subscripting it like an array of int? I think you should make the whole thing an array and that'll fix your problem.

void Pizza::set ( int x , int y , int a[] , int i );
void Pizza::set ( int x , int y , int a[] , int i )
{
    type = x ;
    size = y ;
    for ( int z = 0 ; z < i ; z++ )
        toppings[z] = a[z] ;
}
pizza.set ( x , y , a, i ) ;
Duki commented: great suggetion! +4
Hamrick 150 Posting Whiz

Is there any way to put the Progress bar in the copying thread, Access the forms control from another thread or create a new form control that is on the Form but generated by the Copying Thread?

Yes, there is! :) What you need to do is access the controls through a dummy interface that calls Invoke on the control instead of manipulating it directly if it's not on the main thread.

Delegate Sub SetProgressPropertyCallback( ByVal property As String, ByVal value As Integer )

Private Sub SetProgressProperty( ByVal property As String, ByVal value As integer )
  If ProgressBar1.InvokeRequired Then
    Dim d As New SetProgressPropertyCallback( AddressOf SetProgressProperty )

    Me.Invoke( d, New Object() { property, value } )
  Else
    Select Case property
      Case "Value"
        ProgressBar1.Value = value
      Case "Max"
        ProgressBar1.Maximum = value
      Case "Min"
        ProgressBar1.Minimum = value
    End Select
  End If
End Sub
Hamrick 150 Posting Whiz

I see the datetimepicker does not have 'DataSource' and 'DisplayMember' properties.

I don't think they make sense for the date time picker. There's probably a few ways you can define them for the control, but I can't think of any that make a date time picker the better choice than a combo box or some other aggregate control except for what the date time picker does now without those two properties.

Would it very difficult to add these properties?

I don't think so, but you'd have to do a lot of micromanagement to make sure that only the data source dates can be selected by the calendar dropdown or spinners.

Hamrick 150 Posting Whiz

Did you try databinding?

DateTimePicker1.DataBindings.Add( "Value", MasterTable, "Dates" )

I don't think that'll work very well though. If you pick a date that isn't in the table, you'll probably get an exception. How about using a combo box instead of a date time picker? That makes more sense because you're picking from a subset of dates that exist in the table, but a date time picker lets you pick from all possible dates.

Hamrick 150 Posting Whiz

The data source part of the connection string refers to an mdb file, not a folder path. I think it should be like this.

Dim DBSPATH As String = "C:\Documents and Settings\rgibson.FOODGROUP\My Documents\Visual Studio 2005\Projects\AR Van Router\myDatabase.mdb" 
    
 Dim Testing_ODBC_ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DBSPATH & ";User ID =Admin;Password=;"

The error said index 33, and that's right where the data source part of the connection string starts.

Hamrick 150 Posting Whiz

You write a C callable wrapper that runs the method.

#include <cstring>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

class Split {
  vector<string> result;
public:
  Split( const string& s ) {
    stringstream sin( s );
    string temp;

    while ( sin >> temp ) {
      result.push_back( temp );
    }
  }

  vector<string> Result() const { return result; }
};

extern "C" {
  char **SplitString( const char *s ) {
    vector<string> temp = Split( s ).Result();
    char **result = new char*[temp.size() + 1];

    // Terminate the result array with a sentinel.
    result[temp.size()] = 0;

    for ( vector<string>::size_type i = 0; i < temp.size(); ++i ) {
      // Add a new C-string to the result array
      result[i] = new char[temp[i].length() + 1];

      // Initialize the new string from the temp result
      strcpy( result[i], temp[i].c_str() );
    }

    return result;
  }
}
#include <stdio.h>

extern char **SplitString( const char *s );

int main( void ) {
  char **p = SplitString( "this is a test" );

  while ( *p ) {
    printf( "%s\n", *p++ );
  }

  return 0;
}
Hamrick 150 Posting Whiz

That's usually an error you get with malformed connection strings for ado.net. You mentioned access, are you trying to connect to the database at all? If you are, what does the connection string look like?

Hamrick 150 Posting Whiz

What are you doing in the form_Load event or constructor?

Hamrick 150 Posting Whiz

Did you set the version number by going to Project/Properties/Assembly Information and typing it into the dialog? That's where My.Application.Info.Version gets it from.

Hamrick 150 Posting Whiz

Where did i do wrong?

I don't know. It looks like CreateRawDataBUFR is corrupting your object, but you didn't post the logic for CreateRawDataBUFR. Without the code I can't even guess where you went wrong.

Hamrick 150 Posting Whiz

Better than M$, why am I not surprised.

I hope you're not surprised that intel's compiler has better optimizations because they _make_ the processors, and not because you just don't like microsoft. ;)

Hamrick 150 Posting Whiz

I'm not sure what you mean, but you can write a unit test method that runs the event handlers for each of the buttons as if you clicked on them. That automates a sequence of button clicks...

Hamrick 150 Posting Whiz
system("pause"); //stdio.h

system is in stdlib.h, not stdio.h. It's not a good idea either because now you depend on an outside program that might be malicious. It's a big security no-no because you created a hole that hackers can use to break into your program. :(

iamthwee commented: good advice Hammie +11
Hamrick 150 Posting Whiz

Doesn't printing \n do the same thing as fflush?

Hamrick 150 Posting Whiz

The window gets destroyed when the program ends. If you call getchar so that it blocks for input, the program doesn't end and the window isn't destroyed until getchar returns.

Hamrick 150 Posting Whiz

Put it somewhere else without all of the extra nesting, like C:\. Make sure that the extension is right. If this is access, it should be *.mdb, not *.mdp, I think. When it tells you it can't find the file, it can't find the file. :) That means the connection string is wrong somehow.

Hamrick 150 Posting Whiz

What message does the exception give you?

Hamrick 150 Posting Whiz

because I am using express edition , I should set my project refrences to System.Xml.dll but here there is no such thing it is only system.xml

System.Xml and System.Xml.dll are the same thing in the reference picker. But that reference should have been added when you made a windows forms project...

Hamrick 150 Posting Whiz

You don't need to surround the database path in quotes inside a connection string. The key/value pairs are all delimited. It should look like this instead.

con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Documents and Settings\shassanzadeh\Desktop\Sara\extra\AddressBook.mdp"
Hamrick 150 Posting Whiz

That's what the link is for. :) Here's a super short program that does it.

Public Class MainForm
  Inherits System.Windows.Forms.Form

  Private _trayIcon As System.Windows.Forms.NotifyIcon

  Public Sub New()
    Me.InitializeComponent()
  End Sub

  Protected Overrides Sub OnResize(ByVal e As System.EventArgs)
    Me._trayIcon.Visible = (Me.WindowState = FormWindowState.Minimized)
    MyBase.OnResize(e)
  End Sub

  Private Sub InitializeComponent()
    Me.SuspendLayout()
    Me._trayIcon = New System.Windows.Forms.NotifyIcon()
    Me._trayIcon.Icon = New System.Drawing.Icon( _
      "C:\Program Files\Microsoft Visual Studio 8\Common7\VS2005" & _
      "ImageLibrary\VS2005ImageLibrary\VS2005ImageLibrary\icons\WinXP\app.ico")
    Me.ResumeLayout(False)
  End Sub
End Class
Hamrick 150 Posting Whiz

You can set the FormBorderStyle to None and that removes the border and toolbar completely. Is that what you wanted?

Hamrick 150 Posting Whiz

Add a NotifyIcon to your project that's only visible when the main form's window state is minimized.

Hamrick 150 Posting Whiz

I ran your code, Bench, but it doesn't do what gaggu82 asked for. It finds the search string even if it's embedded in another word, like "this is a string" prints "is is a string" instead of "is a string". Am I testing it the wrong way?

Hamrick 150 Posting Whiz

There might be a way in the STL to do it, but I don't know. I'd write a search function that finds every occurrence of "is" and then checks to see if the first and last letters are on a word boundary.

#include <string>
#include <cctype>

using namespace std;

// Check if a series of characters is on a word boundary
bool isWord( string source, int start, int length ) {
  bool isLeftBoundary = ( start == 0
    || isspace( source[start - 1] )
    || ispunct( source[start - 1] ) );

  bool isRightBoundary = ( start + length == source.length()
    || isspace( source[start + length] )
    || ispunct( source[start + length] ) );

  return isLeftBoundary && isRightBoundary;
}

// Find the starting index of a word in the string
int findWord( string source, string word ) {
  int index = 0;

  while ( ( index = source.find( word, index ) ) != string::npos ) {
    if ( isWord( source, index, word.length() ) ) {
      return index;
    }

    index += word.length();
  }

  return string::npos;
}
Hamrick 150 Posting Whiz

what does fall of means?

All of the memory for the stack is preallocated. The OS allocates it when the program starts and frees it when the program ends, but while the program is running, it's all still there. Going in and out of scopes will push and pop local variables without allocating or freeing. Here's a program that simulates what happens.

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

#define STACK_SIZE 10
#define STACK_OVERFLOW 0
#define STACK_UNDERFLOW 1
#define STACK_SUCCESS 2

static int stack[STACK_SIZE];
static int top = 0;

// Push a new integer onto the stack and return its address
static int new_int( int **value ) {
  if ( top == STACK_SIZE ) {
    return STACK_OVERFLOW;
  }

  *value = &stack[top++];

  return STACK_SUCCESS;
}

// Pop the first N integers off of the stack
static int free_ints( int n ) {
  if ( top - n < 0 ) {
    return STACK_UNDERFLOW;
  }

  top -= n;

  return STACK_SUCCESS;
}

void process_values( int **values, int size ) {
  int i;

  // Create a few new integers
  for ( i = 0; i < size; ++i ) {
    if ( new_int( &values[i] ) != STACK_OVERFLOW ) {
      *values[i] = ( i + 1 ) * 10;
    } else {
      fprintf( stderr, "Stack overflow!\n" );
      exit( EXIT_FAILURE );
    }
  }

  // Show their values
  for ( i = 0; i < size; ++i ) {
    printf( "Value #%d = %d\n", i + 1, *values[i] );
  }

  printf( "\n" );

  // Remove them from …
Hamrick 150 Posting Whiz

what i want to be sure is, after function statements does the allocation space dissapear?

No, it doesn't. But you _do_ lose the only reference to the space you had, and that's called a memory leak because now it's allocated and won't be freed until the program ends. If your OS doesn't free all memory allocated to a process when it's done, the memory won't be freed until you reboot.

In your program you'll get an exception on top of the memory leak because outside the function ucBufr will still be null. That's because when you passed the pointer to Allocation, it made a copy of the pointer to allocate memory to. When the function returns, that pointer is lost and the original ucBufr is still what it was before.

Your function just takes a pointer and allocates memory to it so you don't need to use a parameter at all. You should make the pointer in the function and then return it so you don't have to use double pointers to keep from losing the reference.

unsigned char *Allocation( void ) {
  unsigned char *ucBufr;

  /* nDataSize and ucData is got from a file here. */
  ucBufr = malloc( nDataSize );
  ucBufr[0] = 'B';
  ucBufr[1] = 'U';
  memcpy( ucBufr + 2, ucData, nDataSize );
  /* ... */

  return ucBufr;
}

after function statements local variables which are defined in the function are freed by the O/S.

Local variables aren't freed after a function returns. What happens …

Bench commented: Good explanation +4
Hamrick 150 Posting Whiz

---Will i have to implement my own allocator?

I don't know much about boost.pool, but unless it does all of the work for you then you will have to implement your own allocator.

---finaly what do you mean by saying if the destructor of a node is a trivial one..

If you need to override the default destructor, it's not trivial. :) Your nodes don't even use an overridden constructor because they don't manage resources. So they're trivial and you don't need to call the destructor. You can just free the memory.

but then the computer goes seriously slow{i guess it must be a memory leak}

You shouldn't guess. It could be that something is wrong with your algorithms or you're just doing too much for the machine to handle. If you think it's a memory leak, you should use some kind of memory leak detector on your program. Then if it's not a leak you should profile everything and try to find the bottleneck. Changing things around based on a guess just isn't a good idea. ;)

Hamrick 150 Posting Whiz

so basically the problem is if i can free the memory without visiting every node

You can handle memory by storing it all in one big block and passing out chunks just big enough for each node. Then freeing it turns into just one call to delete. But that doesn't call destructors for all of the nodes, and unless the nodes only contain built in types and no pointers that's why you need to visit every node.

or constructing a stack in advance...

You only run out of stack with the recursion. I posted a function that uses the STL stack adapter non-recursively. It uses dynamic memory so it can grow as much as you need it to.

like:: visit nodeA, delete nodeA, visit nodeB, delete NodeB, ...... so i start from the root and go down to the leaves....

That's what my clean function does. :)

Hamrick 150 Posting Whiz

What do you mean by making the data structure heavier? You have to free the memory and to free the memory you have to visit every node. The only heavy thing about the clean function is that it uses recursion. That's a pre-order tree walk so you can make it non-recursive pretty easily with a stack.

void clean( node *myNode )
{
  if ( myNode == NULL ) {
    return;
  }

  stack<node*> st;

  st.push( myNode );

  while ( !st.empty() ) {
    node *temp = st.top();

    st.pop();

    if ( temp->right != NULL ) {
      st.push( temp->right );
    }

    if ( temp->left != NULL ) {
      st.push( temp->left );
    }

    delete temp;
  }
}
Hamrick 150 Posting Whiz

Use the Thread.Sleep() method.

System.Threading.Thread.Sleep( 100 )
Hamrick 150 Posting Whiz

You can use the ToString() method of the value and it will turn DBNull into an empty string.

T7.Text = tmp.Fields("Street").Value.ToString()

Or you can test for DBNull and just not set the text property if it's true.

If tmp.Fields("Street").Value <> DBNull.Value Then 
  T7.Text = tmp.Fields("Street").Value
End If
Hamrick 150 Posting Whiz

GUIs in C++ aren't really that easy unless you use C++.NET and then you could use C# because it's designed with .NET in mind while C++.NET is a patch job. Visual C# 2005 Express is a free compiler with drag and drop UI features that make it really easy to make a GUI. If you want to use C++ still, Visual C++ 2005 Express is free too. You can find them all here.

Hamrick 150 Posting Whiz

I think you'd be better off trying to separate the common stuff that both apps use into a dll and then having both apps reference the dll. What exactly do you need to do this for anyway?

Hamrick 150 Posting Whiz

What language do you need the compiler for? :)

Hamrick 150 Posting Whiz

i think it would be easier and the same results will be reached if hi truncates the number by dividing it

That's the same thing I suggested except it's too specific to the problem. If this is homework then a good next project is to generalize the problem into a number of any length or to change the intervals that commas are added. If you use magic numbers, those next projects are even harder, but if you do it one of the ways I mentioned, the next projects just fall right out of the current solution. :)

Easier now usually means harder later. ;) If you do it right the first time, you don't have to undo it and then do it right the second time.

Hamrick 150 Posting Whiz

I just finished an app that lets you open a data connection and test stored procedures by using an XML config file to specify the stored procedures and corresponding parameter information. If I understand, that's close to what you want to do. There are a few things you need to think about.

  • If you want to run any script or program, you can't hard code anything. You have to use some kind of config file to say what program is being run and what parameters it takes. I did that with an XML file that I deserialize into an object hierarchy.
  • You need to come up with a way to display the output in a universal way. With program output, that means you're going to end up using just a text box and letting the program format the output. What I did was use a data grid for the result set and a multiline text box for the output parameters.
  • The UI needs to be able to adjust to different numbers and types of parameters that you send to the program you're going to run. What I did was make a user control with a name and a text box value that the user can just type freeform parameter data into. There's no validation since it's a utility for people who know what they're doing. ;)

It's harder to write an application that's universal than one that only allows a few different script types. You need to …

Hamrick 150 Posting Whiz

I don't want you to do my homework, I want you to point me in the right direction.

Ok, here are a few hints that you can bring together to do the program.

1: You can find the right side (least significant) digit for a number in any base by saying number % base. If the number is 123 and the base is 10, 123 % 10 gives you 3.

2: You can trim the least significant digit for a number in any base by saying number / base. I the number is 123 and the base is 10, 123 / 10 gives you 12.

3: You can find the number of digits in an integer by adding 1 to the floor of the base 10 logarithm of the absolute value of the integer.

int digits = 1;

if ( number != 0 ) {
  digits = 1 + (int)floor( log10( abs( number ) ) );
}

4: You can find the left side (most significant) digit for a number of any base by saying number / (int)pow( (double)base, digits ) .

5: You can trim the most significant digit for a number of any base by saying number % (int)pow( (double)base, digits ) .

6: A rotating counter is easy to write. Start at 0 and when it gets to the limit, set it back to 0.

int counter = 0;

cout<<"Starting count\n";
for ( int i = 0; i < 20; …
Hamrick 150 Posting Whiz

It says line 300. That's probably right where the offending catch is and you can look back up to find the matching try. I'd help, but I don't have a way of matching the error's line 300 to the line numbers in the code you posted.

Hamrick 150 Posting Whiz

It's no biggie. But I'm sure that there are a few arrogant people around here that love to flame anyone who makes an innocent mistake. Better me helping you out than them driving you away, right? :)

im thinking its somewhere between lines 163-185..

Don't you get a line number with the error? Can you point it out? Lines 163-185 don't contain a catch at all. This is a syntax error so it should be pretty easy to locate with help from the compiler.

Hamrick 150 Posting Whiz

I don't think you're in the right forum. It looks like you want this one. And when you need help it's best to be specific about what problem you're having. Just asking for suggestions and pasting reams of code won't get you much. I'd probably be able to help you, but I have no idea what you want... :(

Hamrick 150 Posting Whiz

Make a shortcut to your exe and from the properties of the shortcut you can set a shortcut key. Pick any key you want, like y, and the shortcut to run the link will be ctrl+alt+y.

Hamrick 150 Posting Whiz

You need to include stdlib.h. The error cryptically tells you that malloc wasn't declared and the compiler just assumes it's a function that returns int and casting from int to char* isn't friendly.

Salem commented: Excellent diagnosis of the problem +9
Hamrick 150 Posting Whiz

For example if I have a program called program.exe and it is stored on the hard-drive is it possible to start the execution of that program by pressing a sequence of keys on the keyboard instead of going onto the hard-drive with windows explorer for instance and double-clicking with the mouse?

If you want to use the exe itself, no. If you're ok with making a shortcut to the exe then you can set a shortcut key that lets you type ctrl+alt+<your key> to open the program.

Hamrick 150 Posting Whiz

Probably because I don't know how to put it into perfect mathematical notation... Honestly, I have no idea what you're talking about.

Hamrick 150 Posting Whiz

What? How can the height of a tree be a non-integral value?

I can remove the unused precision if it bothers you. ;)

Hamrick 150 Posting Whiz

X are my node of the trees which gets visited twice ,but what are y?

x represents internal nodes, that's nodes with one or more children. y represents the rest of the nodes with no children, and they're called external nodes.

6
       /   \
     5       9
    / \     /
   1   4   8
  /   /     \
 0   3       7

[1,4,5,6,8,9] are the internal nodes because they have at least one child. [0,3,7] are the external nodes because they have no children. Actually I was wrong about the 2x because if an internal node has 2 children it's visited 3 times. I guess the exact time complexity would be O(3x + 2y + z) where x represents nodes with 2 children, y represents nodes with 1 child, z represents nodes with no children and x + y + z = n. Big O throws away the stuff that doesn't grow so you get O(x + y + z) which when added together gives O(n). I think that's a tight bound too so you can really say \theta(3x + 2y + z) and be right. :) I'm not sure if you can get away with \theta(n) though.

I'd like to nitpick the statement that the height is \log n.

You're right. I should have said 2\log(n+1).

Hamrick 150 Posting Whiz

To traverse a binary tree you have to touch every node in the tree once. From that you infer that the time complexity is [TEX]O(n)[/TEX]. You can take it further by postulating that because the recursive algorithm actually visits every internal node 2 times** to complete the traversal the time complexity is [TEX]O(2x + y)[/TEX] where x is the number of internal nodes and y is the number of external nodes and [TEX]x + y[/TEX] gives you n.

The space complexity is the same [TEX]O(n)[/TEX] for the same reason. If you take it further, the most space that's ever going to be maintained at any time is the space for the longest branch because that's the maximum number of stack frames that will be pushed at any given time by the recursion. If the tree isn't balanced then the longest branch could be n nodes which makes the space complexity [TEX]O(n)[/TEX]. If the tree is balanced, the space complexity can't be more than the height of the tree. The height of a balanced tree is always going to be [TEX]logn[/TEX] so the space complexity if you assume sufficient balance is [TEX]O(logn)[/TEX].


** because it recurses in and then back out