Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

you could just use '\r' to make the cursor return back to the beginning of the line

int cookTime(int time) {
int second, minute;
    for (minute = time; minute > -1; minute--) {
       for (second = 59; second > -1; second--) {
	sleep(1);
	printf ("\r%2i : %2i", minute, second); 
        }
     }
  }
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The main problem with that class is that the constructor with only one constructor does not initialize the value of both class member variables mdim_ and ndim_.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>but feel that would cause me to change a lot of code

Oh yea -- one line is a lot of code to change.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

you might get better shuffling results if you use std::random_shuffle() found in <algorithm>.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

This is one way to do it

void mem_alloc_main(int ind,float ***u_trn)
{
    float** ay;
    ay = (float **)malloc(10 * sizeof(float*));
    for(int i = 0; i < 10; i++)
    {
        ay[i] = (float*)malloc(10*sizeof(float));
        for(int j = 0; j < 10; j++)
            ay[i][j] = (float)rand();
    }
    *u_trn = ay;
}


int main()
{
    srand((unsigned int)time(0));
    float** arry = 0;
    mem_alloc_main(0, &arry);
    for(int i = 0; i < 10; i++)
    {
        for(int j = 0; j < 10; j++)
        {
            printf("%0.2f ", arry[i][j]);
        }
        printf("\n");
    }

}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

If you want mem_alloc_main() to allocate a two-dimensional array of floats, then you have to declare the parameter with three stars, not two. The rule is that mem_alloc_main() needs to have a pointer to a two-dimensional pointer. What you have done is to pass a pointer to a one-dimensional pointer, but allocating it as if it were a two dimensional pointer.

Also: C programs do not require the void* return values to be typecast.

void mem_alloc_main( float ***ptr)
{
    *ptr = calloc((kf_trn+max_del+1),sizeof(float*));
}

int main()
{
   float **arry = 0;
   mem_alloc_main( &arry );
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

You need to use double pointer there so that foo() can allocate memory for the pointer in main(). You can do the same with as many other pointers as you like, it is not limited to just one pointer. This solves the problem is returning more than one string at the same time.

void foo( char ** ptr)
{
     *ptr = malloc(255); // allocate some memory

     strcpy( *ptr, "Hello World");
.
}
'

int main()

{
    char *ptr = 0;

    foo( &ptr ); // <<< pointer to a pointer

    <snipped>
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

you can use sprintf() to format the string then write() to write that string to the file.

line 18: if the file can not be opened the program needs to do two things, not one.

if( id < 0)
{
   perror(...);
   return 1; // abort the program
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Maybe you will find sometime a little more interesting in the field of artificial intelligence and handwriting recognition.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

A real-life project: Write a computer program that reads data from a serial port that contains UPC product codes from an external scanner device. Use that UPC code as a key to look up product information in an SQL database (such as MS-Access, MS-SQL Server, Oracle, Sybase, etc) then display the information on the screen and transmit it out another serial port to a Large Character Printer (you need not know technical specs of the printer, just send the product data.)

Shinedevil commented: Even though it means nothing, I like your post so here is some (0) rep. +0
Salem commented: You should have said "Large Hadron Collider" :D +19
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

line 9: that will not compile with most compilers because that feature is new to c99 standards and most compilers older than that. If you want to use charcter arrays then you will have to allocate them at runtime

int n;
char ** a;
cin >> n;
a = new char*[n];
for (int i=0; i<n; i++)
{
    a[i] = new char[15];
    cin.getline(a[i], 14);
}
restrictment commented: Nice code, I did not know this. =) +1
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Post the code you tried.You could probably use the same character array for all n number of strings because you didn't say there was a requirement to save all those strings.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Now tell us what is wrong with the code you posted? Compiler errors? Yes, then post some of them.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>I've been playing with the code to get the things I need but nothing is working.

That's nice -- now post the code you tried because we are not clairvoyant.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

That's basically the idea. On Intel based PCs the compiler might actually implement it by using assembly instructions such as the scansb instruction, which greatly speeds up the operation because there is no software loop involved.

NathanOliver commented: thanks for your help +1
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

No while( *str++ ) size++;

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The static_cast<> lets the c++ compiler do a better job of ensuring the cast is valid. For example: this will produce a compiler error (vc++ 2008 express)

int  main()
{
    int x = 123;
    char* p = static_cast<char*>(&x);  // << ERROR
    char* p = (char*)&x;  // << NO ERROR
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
#include <string>
#include <sstring>
using std::string;
using std::stringstream;

int main()
{
   int n = 1234567890;
   stringstream str;
   // insert int into stringstream
   str << n;
   // convert to std::string
   string s = str.str();
   // now just insert commas in appropriate places
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

convert the integer to std::string (use stringstream for that), insert the commas, then display the string. There is no function that will do it for you.

If you use C#, VB.NET or C++/CLR you're in luck because WriteLine() can do it.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

pass it the same as anything else

void foo(char *c)
{

}

int main()
{
   char c = 'A'
   foo(&c);
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

What language did you use to write that program? In C and C++ there are a few win32 api function calls you need to make in order to make the program run with admin privileges. See this article and the links at the bottom.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
void fillCities(WeatherInfo Rain [], int &NumCities)
{	
    ifstream infile;
	infile.open("prog1.txt");
	string CityName;
	int i = 0;

	while ( getline(infile, CityName) )
	{
		Rain[i].city = CityName;
		infile >> Rain[i].TotRain;
		infile >> Rain[i].TempHi;
		infile >> Rain[i].TempLo;
        infile.ignore();
		i++;
	}
    infile.close();
	NumCities = i;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>As for the end of file, i have to use it because i'm not supposed to know how much data is being presented

My comment still stands. Re-read it again to understand what I said about it and how to correct it.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

First move lines 15, 24 and 26 down into fillcities() function. There is no reason for ifstream to be global.

You can combine lines 38 and 39 like this: while( getline( infile, CityName ) ) . There is no need to use eof() like that because it doesn't work the way you think it does. eof() is only detected after an attempt to read something, not before.

Post a few lines of that data file.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

If you know the buffer will need 8 seconds of data then why not just allocate it all at the same time? Calling realloc() will work too, but will be more time consuming than allocating it all at the same time.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>> setw( 10 ) << 3

That, in C would be printf("%10d", 3); >> setprecision( 2 )
That's the same as printf("%.2f", sales[ i ][ j ]);

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

So what's the big deal about writing that operator overload ? I'm guessing those containers need that operator so that they can make binary searches. If the data is not ordered then they would have to resort to simple sequential linear searches.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

In C and C++ languages array subscripts begin with 0, not 1. So line 24 should be t[0] = ... . Other lines in your program need to be adjusted accordingly, such as use t[0] and t[1].

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>(I will also post this thread at c++ area, hope you don´t mind )
Yes we do so don't do that. Pick a forum and stick with it.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>Im using vs 2010 beta 2,
Well it is in beta and probably still has a few bugs. Compile with 2008 and see if that problem still persists.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The problem might be compiler specific. I used VC++ 2008 Express and had no errors with the code snippet you posted. What compiler are you using?

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Check to see that <cstdio> was included and not <stdio.h>

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Thank you very much, so that mean I need to remove line 30, 38, 43.

Yes

Just tested and I give error message if I enter the day's more than 31 and month more that 12.

Good :) I see a small bug in the assert statement I posted. The last test should use <= operator, not just < so that the last day of the month will be considered value. day <= daysPerMonth[mon-1])

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

lines 74-80. Its not necessary to actually perform that check. Just subtract the two numbers and get their absolute value diff = abs(f - g);

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The asserts on lines 30 and 38 will not work because the value of t is the julian date so it will equal the specified value in the assert statement only on the very first iteration of the loop.

you could do something like this on line 17 of the code you posted, and then don't use assert at all in the remainder of the code:

assert(mon > 0 && mon < 13 && day > 0 && day < daysPerMonth[mon-1]);
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Thanks everyone for your input,

As I am using XP there is a 4Gb physical memory limit and
I suspect this is restricting the application to ~2GB max.

I was completely unaware that this limit existed as most windows classes
appear to have been built with longs I figured since memory address registers
are dealing 100GB that this issue wasn't applicaible

The problem is you are probably compiling with a 32-bit compiler which produces code that has a 2 gig limit, even if you are running 64-bit operating system. Get a 64-bit compiler and that limitation will go away, although there will be another, but much larger, limit.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
LoadLibrary((LPCTSTR)_T("test.dll"));

You need to also #include <tchar.h>

You posted the wrong typcast -- should have been LPWSTR, not LPCTSTR. But with _T macro the typecast is not even needed.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

C programs can not use any of the c++ stuff, so if you are expecting a c function to call a c++ function which returns std::string, then forget it. The best you can hope for is that the c++ program returns const char*. Then you will have to declare that c++ function as extern "C" , which essentelly tells the c++ compiler to treat the function as a C function calling conventions, and not mangle the function name. That means the c++ function can not be a method of a class or an overloaded function.

Since you are doing that much recoding, it will probably be easier to just convert everything to c++ and not use any of the *.c files at all. If there are C functions that can be used without change then just copy/paste them into a *.cpp file.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

What about the locations LoadLibrary tried? Is the file "test.dll" a library?

In this case, yes, all DLLs are considered libraries for the purpose of LoadLibrary(). That function name could probably have been better named as LoadDLL(), but it wasn't.

i think you should do it in this way.

Nice code and link, but the problem isn't with GetProcAddress(), as the op previously stated.

@Nicholas: If you do not specify the DLL's location then it must be either in the current working directory or in one of the directories in your PATH environment variable. If there are more than one copy of the DLL on your computer then I'd suggest you delete all but one of them so that your program doesn't pick up the wrong (possibly out-of-date) version (been there myself, and done that too.)

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The best way to approach that problem is to use <vector>, not C-style arrays. vectors are arrays which will expand as needed to contain however may data objects you want to put into it. Just include <vector> and use std::vector<int> firstArray; . When you want to add an integer just call it's push_back method like this: firstArray.push_back(10); (or replace the 10 with an int variable).


Second-best approach is to use a pointer to the array, and reallocate it as needed (duplicating the efforts of the std::vector class): int* firstArray;

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

When LoadLibrary() fails, call GetLastError() to get the error number, then FormatMessage() to format the error message string.

DWORD dwError = GetLastError();
char buf[255];
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, dwError,0,
    buf, sizeof(buf));
cout << buf << '\n';
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

You are right -- but that may not be the problem.
This works

int main()
{
     unsigned int sz(1073741825);
     std::string str;
     str.resize(sz);
     cout << "Done\n";
     cin.get();

}

But this does not

int main()
{
     unsigned int sz(1073741825);
     unsigned int i = 0;
     std::string str;
     for(i = 0; i < sz; i+= 10)
         str += "xxxxxxxxxx";
     cout << "Done\n";
     cin.get();

}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Actually the OP tries to get ~2 GBs .. I think you missed ...

std::string str(memblock, sz); //too much for visio :(

Unless I'm missing something line 12 sets the value of sz to 1 Gig. All that line does is initialize str to the first sz bytes of memblock.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>Since you are trying to allocate rather huge amounts of memory (2GB),

Nope. Its only 1 Gig which is less than what st::string.max_size() returns.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I get the same behavior. I have a Windows 7 64-bit computer with 5 Gig RAM. I also just tried the resize() method and that doesn't work either. Possibly the program can not find enough consecutive memory for that size.

Code::Blocks doesn't like that either -- it throws an error.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

There are two main problems:
1) >> friend ostream &operator<<(ostream &os, const Puzzle& p);
The last parameter should be passed by reference, not by value. Also change that in the *.cpp implementation file.

2) in Puzzle.h you need to declare the namespaces, such as

#include <fstream>
using std::fstream;
using std::ifstream;
using std::ofstream;
using std::ostream;

or just add std::fstream before each occurence of the c++ class.

sid78669 commented: If there is an answer, Ancient Dragon has it!! +2
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Why do you have to convert those C functions at all? Just call them norally in any c++ code you want to add to the project and leave the C code in tact.

In the c++ files you just have to add this: probably add it in the header file that contains the function prototypes of all those C functions. This is the same method that compiler writers use in standard header files so that the same header files can be used in both C and C++ programs.

#ifdef _cplusplus
extern "C"
{
#endif
    // now prototype all those C functions
#ifdef _cplusplus
}
#endif
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Is the ptr a NULL value?? No. Well just like any other invalid pointer the program doesn't know its invalid until delete attempts to delete it. You will get the same behavior if you try to delete any pointer, whether its a c++ class or not. You wrote the delete ptr; code, not the compiler. So if its wrong its your fault, not the compiler's. There is nothing about that pointer that would indicate the object has already been destroyed. When a c++ class is deleted only the data section of the class is destroyed, not the code section. As long as the code does not attempt to reference any of the class's data it should still work (undefined behavior though).

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

vc++ 2008 Express produces a runtime error on that delete ptr; line because the class instance was already deleted. I've seen a few class objects delete themselves -- such as Microsoft's COM functions.