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

>>The language used in the book is so difficult tat i cud'nt understand tat..

Sounds like you are not yet quite read to study pointers. Yes, pointers can get very very messy and complicated very quickly. My suggestion for you is to stick the the simple pointers -- those with only one * -- practice writing programs with them until you understand these one start pointers thoroughly.

You can create pointers with as many stars as possible -- there really is no limit to the number of stars. But practically anything beyond three stars is very rare, and very difficult to comprehend. One and two stars is the most common.

Also remember that most of those ebooks were written by people with Ph.D. and written for college level students (18 years and older).

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

Who the hell wants to read all that crap???

Passwords code snippet here

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

You sould completly replace cmd.exe with your own version of that program. For example Microsoft has already done it with PowerShell. Another example here

Shinedevil commented: You should totally give me rep, so I can give you rep right back. :P +0
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

>>anyone knows how to use clearscreen with dev system("cls"); If you dont' want to use system() then here is how to do it with win32 api functions.

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

You can't insall turbo c on Windows 7.

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
bool isFree() //are we free to service new customer?
      {
	 if(free)
	    return TRUE;
	 else if(customer.done())
	    free = TRUE;
	 return free;
      }

Rewrite it like this: There is a difference between TRUE and true; bool variable requires true, not TRUE

bool isFree() //are we free to service new customer?
 {
	  if(customer.done())
	    free = true;
	 return free;
      }
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

line 26: The value of argc will not be less than 1. The first argument to the program is the name of the program itself (on most operating systems).

line 27 and 28: What are you trying to do there? cout knows nothing about %d format string. You need to study how to use cout before continuing with that 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

aint array must be allocated with new operator. The way you tried to do it is not yet supported by most c++ compilers.

linesize = line.size();

  int *aint = new int[linesize];               // The arrays satisfying the condition
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

>>so you say there's problem with header files?
No. The program didn't like that constructor for some reason.

Success!

class WCS
{
public:
    WCS(const wstring& str)
    {
        s = new wchar_t[str.length()+1];
        wcscpy_s(s,str.length()+1, str.c_str());
    }
    ~WCS()
    {
        delete[] s;
    }
	operator wchar_t*&()
    {
		return this->s;
	}
protected:
    wchar_t* s;
};

int main()
{
    WCS m(L"Hello");
    const wchar_t* s = m.operator wchar_t *&();
    wcout << s << '\n';
    std::cin.get();
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I commented out everything but the constructor/destructor and it still crashes. And changing string to just s didn't help either. Replaced wchar_t* with char*t and it works.

class WCS
{
public:
    wchar_t* s;

    WCS(const std::wstring& wstr)
	{
        s = new wchar_t[(wstr.size()+1) * sizeof(wchar_t)];
        wcscpy_s(s,wstr.size(), wstr.c_str() );
	}
	~WCS()
	{
		delete [] this->s;
	}
//	operator wchar_t*&()
//    {
//		return this->s;
//	}
};

int main()
{
    std::wstring wstr(L"Hello");
        
    WCS w(wstr);
    //wchar_t* const& c = w.operator wchar_t *&();
    //std::wcout << c;
    std::cin.get();
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I'm not about to write the program for you, but just show you how to build the list of keywords from the command line arguments.

Lets say you have a structure like this:

struct keywords
{
    std::string word;
    int count;
};

Now all you have to do is build a list of those structures from the command line arguments

#include <vector>
#include <string>
#include <fstream>
using std::vector;
using std::string;
using std::ifstream;

int main(int argc, char* argv[])
{
    vector<keywords> keywordList;
    for(int i = 3; i < argc; i++)
    {
            keywords k;
            k.word = argv[i];
            k.count = 0;
            keywordList.push_back(keywords);
   }
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

A couple ways to do it
1) create a structure that contains the keyword and count of the number of times it appears. Then create an array of those structures, one element for each keyword on the command line. Then read each word of the file, for each word loop through the array of keywords and increment the count when a match is found.

2) similar to above but use a <map> instead of an array of structures.

Note: Since you posted this in c++ forum you might want to use ifstream instead of FILE*.

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

Of course there is

int a,b,c,d,e,f,g,h;
total(a,b,c,d,e,f,g,h,0);

Or you can just pass an array of integers, which doesn't require va_args at all.

const int MAX = 15;
int array[MAX];
total(array, MAX);
mrnutty commented: Happiness, eh? What does that mean? +4
Salem commented: Ah an array, so simple and clear :) +19
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I don't understand what you are asking. That function will sum up however many integers you want to pass to it as long as the last parameter is 0 so that the function knows when to stop.

You might want to recode that while loop because the first time through the value of arg is undefined.

while( (arg = va_arg(listpointer,int)) != 0)
{
    sum += arg;
}

int main()
{
    // Note that the lastg parameter is 0
    // which tells total() when to stop summing.
    // The first parameter is just ignored by total()
    int x = total(0,1,2,3,4,5,6,7,9,10,11,12,0);
}
sumitg979 commented: Good Reply +0
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

system() is a c and c++ function. Otherwise you have to use os-specific functions such as the spawn family of functions on *nix, of ShellExecute() and CreateProcess() on MS-Windows.

Dave Sinkula commented: Echo? +13
Nick Evan commented: Here's some rep, because "you haven't got enough yet" :icon_wink: +12
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Sorry, I can't help you because I don't know the first thing about it.

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

Looks ok to me -- just remember that srand() is executed only once during the entire lifetime of the program while rand() is executed as many times as you need it.

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

1) post *.cpp, *.c and *h files, not *.docx files. MS-Word files can contain viruses that we don't like.

2) your program contains a couple missing semicolons and include files. When you using cin and cout you need to include <iostream>

3) A misspelled function name -- watch the capatilization because cin.getLine(... is not the same as cin.getline(... .

4) TraverseList(pHead); as it appears in main() is not a valid function. You are trying to call a method of a class without a class object. If you want to call that class method then you can make it a static method and call it like this: CNode::TraverseList(pHead);

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

The variable name on line 3 is the same as the function name on line 1 -- bad idea. Read this article. Your program has to call one of the checkboxes methods to find out whether it was checked or not.

_siren2 commented: pointed out a few things and the users post helped me a lot +0
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

'm sorry to say that, but i'm not here to joke. I'm going thru a family emergency and thats the reason need some help. Otherwise i never ask someone else to do my assignment.
please let me know if someone will be able to help.

thanks a lot in advance

raj

Then drop the course and take it again. Or ... deposts lots of $$$ in my PayPal account.

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

Not really.. The links on my sig shouldn't count as spammy at all. They are not selling anything, i don't get a profit from anything, and they are all very programming related.

First one sends you to my site i made, a site *solely* devoted to wxPython tutorials. Very relevant to people who visit the python forum where i mainly post.

The others are links to a chat client that will connect the user to the daniweb irc channel.. again, i am failing to see in any way how that could be spam.

I think here that you are i have different definitions of spam.

According to DaniWeb Rules, the definition of spam is:

Do not spam, advertise, plug your website, ...

So if Dani made signature links adhere to the Rules just like normal posts then your signature links would be considered spam.

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

you mean something like this? You can either provide a file name on the command line or redirect a file into the program like this: myprog <main.cpp

void foo(istream &in)
{
    string line;
    while( getline(in, line) )
        cout << line << '\n';
}

int main(int argc, char* argv[])
{
    if( argc == 2)
    {
        ifstream in(argv[1]);
        if( in.is_open() )
            foo(in);
    }
    else
        foo(cin);
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The rule is -- one free() call for each malloc() or calloc() call. You can not free() something that was never malloced. In the case of the linked list, each node of the list was allocated by calling malloc(), therefore each node must be destroyed by calling free(). In the case of the array, there was only one call to malloc(), therefore there need be only one call to free().

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

>>it doesn't matter that the variable that was returned goes out of scope.
Of course it does indeed matter. Returning a reference as in your second code snippet is very similar to returning a pointer like this:

int* foo()
{
   int a = 10;
   return &a;
}

Both the reference you posted and the pointer I posted above are wrong and will most likely cause problems for the calling function.

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

You don't use strlen() to get the length of std::string object -- use its length() method

if( f.length() < s.length() )
{
    swap(f,s);
}

Now do that for all combinations if the strings f, s, t, and fo;

Also, a much better way to format your code is to align all the if statements up, unless they are nested, because it makes the code much easier to read and understand.

if( a < b )
   swap(a,b);
if( b < c )
   swap(b,c);
if( c < d )
   swap(c,d);
// nested if statements are like this
if( a < b)
{
    if( b < c)
    {
        if( c < d)
        {
             swap(a,d);
        }
    }
}
i_luv_c++ commented: you are awsome :) +1
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

i want the answer for a program wap to accept data dere we have to count number of characters,words,lines
i am waiting for reply

We don't do homework. You do the work, You post the code, You ask questions about what You do not understand. We will help with that -- but We will not write the program for you.

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

Welcome to 32-bit programming :) Read this article.

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

call clock() before the loop starts and again after the loop ends, then subtract the two times. When the program runs fast enough the difference might be 0. In that case, put the loops into a function and call that function 1,000 times.

clock_t t1, t2;
t1 = clock();
for(int i = 0; i < 1000; i++)
     RunLoops();
t2 = clock();
clock_t diff = t2 - t1;
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

One possibility is to learn SQL so that you can use SQL databases (such as MS SQL, Oracle, and Sybase) in your C and C++ programs.

Another is to learn how to use sockets (WinSock on MS-Windows) so that your program can communicate with other programs running on either the same computer or another computer across a network.

The possibilities are endless.

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

AD, you always have the answer to the questions, AND THEY ARE RIGHT!! Thanks so much, it compiled. But now a new issue has showed up, in Puzzle.cpp, the overloaded operator<< is declared as a friend in the .h file but the .cpp is saying it is not able to access the private variables. Any suggestions?

Yes -- you have to make that parameter const reference too in the *.cpp implementation file.

Also, AD, can you please tell me why it did not work the way, why it needs to have the namespace declared, it might be of use to people like me.

namepsaces always have to be declared. There are at least three ways to do that, which you probably already know.

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

Weeeeeeelllllllccccccoooooommmmmeeeee! Hope you enjoy your stay here.

nav33n commented: Heh! +0
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

This works, even if you later want to change the type of the array, say from char to int:

length_of_array = sizeof(array)/sizeof(array[0]);

That doesn't work when the array is a parameter to a function

void foo(int ay[5])
{
int length_of_array = sizeof(ay)/sizeof(ay[0]);
printf("%d\n", length_of_array);
printf("sizeof(ay) = %d\n" ,sizeof(ay) );
printf("sizeof(ay[0]) = %d\n",sizeof(ay[0]));

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

Will type casts work?
i.e. [b](const char *)[/b]results.st_size This should enable it to compile, but honestly, I suspect you'll get invalid results.

Of course typecase will NOT work. You can not typecast an int into char* because it has to be converted. Very similar to the original post but use results.sz_size instead of the result value from stat() function.

struct stat results;
char buf[40];
stat(pname, &results);
sprintf(buf,"%d", results.st_size);
write(1,buf, strlen(buf));
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Dragonmeister!?!?! getche() ?!?!? getchar() please. It's at least standard...

There is very little about Turbo C that is standard, so it doesn't make any difference whether he uses getche() or not. Could also do this without a problem

while( !kbhit() )
    delay(100);

That's not standard either, but works in that old crappy compiler.

nbaztec commented: Couldn't agree more. Wish, could +rep > 1 :) +1
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

> this is not homework but rather my work
I'm shocked that someone with a programming job can't solve this trivial problem :-O

I'm not after reading this thread.