Narue 5,707 Bad Cop Team Colleague

You probably want to use opendir, readdir, and closedir. Ask man for more details.

Narue 5,707 Bad Cop Team Colleague

>there is no spaces and all of the structure is gone.
That's because you ignored the multiple notifications and instructions the forum automatically gives you on using code tags. I'll add code tags for you this time, but feel free to consider this your warning to use them next time. We specify this in the rules, announcements, and stickies. I suggest you read them before posting again.

Narue 5,707 Bad Cop Team Colleague

This is a forum in which you can get help for writing your own code, not a service that provides code to you.

Narue 5,707 Bad Cop Team Colleague

>i am just asking for the basic code for the communication
>betweens client and server which is already present.
If it's already present then you can find it. If you can't find it then you can't expect us to find it, which means that you expect us to write it.

>which wil make me more comfortable 2 concentrate on fuzzy logic .
I'm curious how you came to this conclusion. Assuming you're bright and experienced enough to incorporate fuzzy logic into TCP/IP data transport, it's mildly humorous that you're unable to crank out some basic client/server code without breaking a sweat.

Narue 5,707 Bad Cop Team Colleague

>p<p+n
That's a moving target. Perhaps you meant p < a + n .

Narue 5,707 Bad Cop Team Colleague

>So if I don't need to use multithreading how can I get
>around having to wait for an input from the user?
If you need to do something that would be accomplished by multithreading, why do you think you don't need multithreading? You could spawn multiple processes, but I get the feeling you don't "need" that either. This leaves you with polling for input at regular intervals (ala. kbhit) and actually pulling the input with a non-blocking read (ala. getch). But that's some seriously convoluted logic in all but the simplest of cases, and in the end you're essentially simulating threads.

Narue 5,707 Bad Cop Team Colleague

So, you want us to give you code for your project so that you can get credit for it? Is that what you're saying?

Narue 5,707 Bad Cop Team Colleague

>the stuff you can do in C can be done in C++, except build an O/S.
Nope, you can do that in C++ too. C++ can be used anywhere C can, provided there's a compiler available for the target machine.

Narue 5,707 Bad Cop Team Colleague

>am i right?
Uh, no. Read up on references.

Narue 5,707 Bad Cop Team Colleague

>Ho can I get around this?
You'll have to learn about working with multiple threads or multiple processes. That's a rather broad and complicated topic for a forum post though, try google.

CodeBoy101 commented: Very helpful, he gave the perfect solution! +1
Narue 5,707 Bad Cop Team Colleague

>Why he need bitwise operator on the end and know what mean on begin?
That's not the bitwise AND, it's the syntax for a reference type.

>So were is the difference between normal call?
There's no difference. The inline function might execute faster, or it might not. inline is really something you should forget about for now.

Narue 5,707 Bad Cop Team Colleague

>i add a ustring to class basic_string who all ready exist with headers.
No, you add an instantiation of basic_string that holds TCHARs and call it ustring. basic_string already exists in the <string> header.

Narue 5,707 Bad Cop Team Colleague

>s.c_str() how it can be this ?
c_str is a member function of the basic_string class. Perhaps it would make more sense to you if I said that the std::string class is actually a typedef:

typedef std::basic_string<char> string;
Narue 5,707 Bad Cop Team Colleague

>printf("Number is %d", argv[1]);
argv is a collection if strings. If the program is called whatnumber and you type whatnumber 4 to the command line, argv[1] will be the string "4". If you want to get an integer from that, you need to perform a conversion:

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

int main ( int argc, char *argv[] )
{
  if ( argc > 1 ) {
    int value = (int)strtol ( argv[1], NULL, 0 );

    printf ( "The string is \"%s\"\n", argv[1] );
    printf ( "The number is %d\n", value );
  }

  return 0;
}
Narue 5,707 Bad Cop Team Colleague

>how can I give it a body if it's pure virtual?
The same way you give it a body if it's not:

class base {
public:
    virtual ~base() = 0;
};

base::~base()
{
}

Or if you like to be concise:

class base {
public:
    virtual ~base() = 0 {}
};

This is assuming that the destructor doesn't do anything, of course.

Narue 5,707 Bad Cop Team Colleague

>This seems like a bad idea..
Yea, that's a really bad idea.

>So should I redesign the class?
No, this is a common problem and there's a common solution. Make the destructor pure virtual, but you still have to give it a body unless you want all of the child classes to not be able to be instantiated as well. ;)

Narue 5,707 Bad Cop Team Colleague

>Could you tell me more about "aliasing" please, if possible...
http://en.wikipedia.org/wiki/Aliasing_%28computing%29

Narue 5,707 Bad Cop Team Colleague

>Will the Stack "array" take "ar" without any problems from push(ar)?
No, but it creates an aliasing problem where if you change ar, the data in the stack will change too. It's better to make a copy of the string for storage in the stack.

Narue 5,707 Bad Cop Team Colleague

>Stack<pop> Sf;
What do you expect pop to be?

>char* ar2 = new char(strlen(ar));
>Scptr.push(ar2);
You're allocating memory but not filling it. Try this instead:

Stack<char*> Scptr;
char ar[] = "abhi";
char* ar2 = new char(std::strlen(ar));
std::strcpy ( ar2, ar );
Scptr.push(ar);
cout<<Scptr.pop();

And be sure to include <cstring>.

Narue 5,707 Bad Cop Team Colleague

>Disentangling new programmers from bad code is more
>difficult and confusing than just going through the steps
>of writing the code correctly to begin with...
Writing code correctly is all well and good, but learning to debug broken code is just as important (if not more so).

Narue 5,707 Bad Cop Team Colleague

When working with linked data structures, the best thing you can do is take out a piece of paper and draw the logic. Don't draw what you think is happening, draw exactly what your code does, and you'll find the bugs.

Narue 5,707 Bad Cop Team Colleague

Sleep from windows.h also takes milliseconds, not seconds. Your call would change to Sleep(5000) .

Narue 5,707 Bad Cop Team Colleague

>I want to throw the exception, the user will see the msg, press return and then terminate.
Set your own custom termination handler. Look in your reference for the <exception> header to get more details.

Narue 5,707 Bad Cop Team Colleague

Note to nitpickers: This article is intentionally simplified.

>could you please explain how and why it works?
cout is a buffered stream, which means that instead of being written individually to the screen right away, the characters are basically being saved in an array called a buffer. This is strictly for performance reasons because it's faster to collect a bunch of things together and push them all out to a device (such as the screen) at once than it is to push them out individually. Think of buffering like dealing with a slow function. The function is slow, so it makes sense to avoid calling it often. The solution is to organize the data so that you don't have to call it often.

When you say cout.put(ch); , you're not actually writing ch to the screen, you're essentially copying ch to an array inside the cout object. That array is then written to the screen when it gets filled up or when some outside force tells it to do so[1]. There are only three times when the buffer is flushed:

  1. The buffer fills up.
  2. The flush member function is called.
  3. A tied stream calls for input.

The first two are simple. If the buffer is full, the whole mechanism would stop working, so cout knows to flush itself automatically. If the flush member function is called, it's like saying "even if the buffer isn't full, flush it anyway". The flush member function is an …

Narue 5,707 Bad Cop Team Colleague

Try flushing the stream after the file is printed. cout isn't tied to your file stream, so a read isn't going to force a flush, and I'm willing to bet that the file isn't large enough to fill the stream buffer:

temp.close();
cout.flush();
getch();
Karkaroff commented: that works!!! +1
Narue 5,707 Bad Cop Team Colleague

>can anyone tell me why ?
swapNode doesn't do anything. When you want to change a value, you pass a pointer to that value, right?

/* Changing the original objects that a and b point to */
void swap ( int *a, int *b )
{
  int save = *a;
  *a = *b;
  *b = save;
}

You do that because not using a pointer to get the the value you want to modify will result in changing just the local variables:

/* Just changing a and b, not the original objects */
void swap ( int a, int b )
{
  int save = a;
  a = b;
  b = save;
}

The principle is the same when you want to modify a pointer value:

/* Just changing a and b, not the original pointers */
void swap ( int *a, int *b )
{
  int *save = a;
  a = b;
  b = save;
}

Just as with the integers, you have to pass a pointer to the value you want to modify:

/* Changing the original pointers that a and b point to */
void swap ( int **a, int **b )
{
  int *save = *a;
  *a = *b;
  *b = save;
}
Narue 5,707 Bad Cop Team Colleague

>Wow 0.o huge right
Eh, I've seen worse.

>What language are you using?
Well, C++ does support those alternative tokens. C does as well if you include <iso646.h>.

>Oh my god! Why not initialize this way:
>char board[ROWS][COLUMNS] = {'.'};
Probably because that wouldn't work. It only initializes the first element to '.' and all of the rest to 0. If you want to slim down the initialization, you're pretty much stuck with something equivalent to this:

#include <cstring>

//...

for ( int i = 0; i < ROWS; i++ )
  std::memset ( board, '.', COLUMNS );

However, there's nothing wrong with a big table initialization, though it's even better if you generate it automatically to avoid manual mistakes:

// Table generator
#include <cstddef>
#include <fstream>
#include <utility>

namespace {
    // Convert an array of T to an array of char
    // for calculating the number of elements
    // at compile-time
    template <typename T, size_t N>
    char (&array(T(&)[N]))[N];

    const int ROWS = 19;
    const int COLS = 19;

    // Change this to match your project
    const char *filename = "tabledefs.cpp";

    // Magic numbers assuming this definition is standalone
    const char *object_type = "char";
    const char *object_name = "board";

    // Change this to change the table values
    const char *set_val = "'o'";
    const char *unset_val = "'.'";

    // Add or remove x,y pairs to set different cells
    const std::pair<int, int> set_indices[] = {
        std::make_pair ( 3, 3 ),
        std::make_pair ( 3, 9 ), …
Narue 5,707 Bad Cop Team Colleague

>Because that would require work
Good tutorials take work. If you don't do the work, your tutorials don't get accepted. Simple. :)

Narue 5,707 Bad Cop Team Colleague

>It would be good to have a tutorial of how to make a tutorial so to speak.
Along with a tutorial on how to be a world class tennis player? Or perhaps a tutorial on how to corner the stock market? The only way to get good at writing tutorials is to write tutorials, as we were discussing on the IRC channel this morning. If you want to learn how to write a good tutorial, why not read good tutorials first and imitate them until you can develop your own style?

Narue 5,707 Bad Cop Team Colleague

>My question is do I have to add all the include files to the command line?
You don't add headers (include files) on the command line. They're strictly for the compiler so that it has declarations for the names that will be linked in later. What you're doing is telling the linker to link with a specific library; in this case that library is the math library. For the most part the standard libraries are linked automagically, with the math library being an exception.

>Does anyone know of a good online resource that
>would walk me through using the gcc complier.
http://gcc.gnu.org/onlinedocs/

Narue 5,707 Bad Cop Team Colleague

>warning C4013: 'strlen' undefined; assuming extern returning int
Make sure to add #include <string.h> to your code.

Narue 5,707 Bad Cop Team Colleague

>I didn't know that Davey was his real name.
If you're interested, you can find the real name of some of the higher ups here.

Narue 5,707 Bad Cop Team Colleague

>Should I understand that he is an admin/staff writer?
Davey is happygeek, the second admin aside from Dani.

Narue 5,707 Bad Cop Team Colleague

>1. "telephone test"
Read your code to someone over the phone. If they don't understand, you need to rewrite it.

>2. off-by-one error
Also known as a fencepost error, based on the simple logic question "If you build a fence 100 feet long with posts 10 feet apart, how many posts do you need?".

Narue 5,707 Bad Cop Team Colleague

>I'll take a guess that the text wraps around to the beginning, in a sense.
Excellent. Now prove your theory[1]. :)

[1] Note that proof doesn't consist only of "it works on my computer". Real proof means you can quote the C++ standard and/or prove that this is predictable behavior everywhere. For that I'd recommend shimmying over to Google groups and looking around comp.lang.c++, comp.lang.c++.moderated, and comp.std.c++. You're likely to get the full story there.

Narue 5,707 Bad Cop Team Colleague

>It looks like he's declaring variables, but there is no type.
He is declaring a variable, and the type of the variable is enum Days . DayOff is an instance of the Days enumeration.

>In line 11, what does days(x) mean?
As an example, Days(1) is the same thing as Monday. It's giving you the Days constant that corresponds to the integer value in x. A much more interesting question is what happens if x holds a value outside of the range 0-6? ;)

>For example, could I have written if (DayOff == 0 || DayOff == 6)?
Yes.

Narue 5,707 Bad Cop Team Colleague

>what does it this mean Initialize the array so that the first 25
>components are equal to the square of the index variable ?
I would assume something like this:

for ( int i = 0; i < 25; i++ )
  alpha[i] = i * i;
Narue 5,707 Bad Cop Team Colleague

>I will be thankful if u help me solving it..
What kind of "help" were you expecting? It looks to me like you want us to solve the problems for you and then give you the completed programs with no effort on your part, which isn't going to happen. Ever.

Narue 5,707 Bad Cop Team Colleague

>and the comments look nicer in gray colour than in green colour.
I use purple for my comments. On a black background it's easier to focus on them if I need to but ignore them for the most part.

iamthwee commented: Purple, eww that's so girly. -2
Narue 5,707 Bad Cop Team Colleague

>however it is very self-documented.
I can't disagree with that. I can only assume that a wall of green text is "very" self-documented. It encourages me to read the code about as much as zero formatting, but certainly very self-documented. :icon_rolleyes:

Narue 5,707 Bad Cop Team Colleague

>To give you an idea of how to comment, I write a small example of code with comments.
That's overkill, of course. If you find yourself commenting one line of code with more than one line of commenting, it's probably too much. Exceptionally obtuse code that requires lots of comments probably needs to be rewritten. You don't want to hide the code in reams of comments because that's just as bad for readability as having no comments. Here's something better:

// param "num": The number we want to check
// returns: true if the number is prime, false if not
bool isprime(unsigned int num)
{
    // Handle the simple cases
    if (num == 0) return false;
    if (num < 3) return true;
    if (num % 2 == 0 || num % 3 == 0) return false;

    // Filter out numbers not of the form 6k+1 or 6k-1.
    // Reference: http://primes.utm.edu/notes/faq/six.html
    for (int i = 6; i < num/2; i+=6) {
        if (num%(i-1)==0 || num%(i+1)==0)
            return false;
    }

    // num wasn't filtered out, so it's prime
    return true;
}

Notice the clever use of external references for more detailed information. You don't need to write a technical paper to describe your algorithm, especially with such a well understood fact as this.

Narue 5,707 Bad Cop Team Colleague

>could you please illustrate the difference between
>the "why" comment and the "what" comment!
Gladly. This is a "what" comment:

/***********Main Function***********/
void main()

All it does is waste space because even the most basic reader will know that this is the main function. You don't need to tell me that, but you do need to tell me why you chose to use void main instead of int main. This is a "why" comment (edited slightly from your code):

// if more left parantheses, balance with
// right parantheses in the end of expression
if(ctr>0)
  while(ctr)

You're not telling me what's going on (pushing closing parens until the counter reaches 0), you're telling me why you're doing it (balancing opening parens with closing parens).

Narue 5,707 Bad Cop Team Colleague

>If you have any difficulty in understanding(at a quick glance that is!)
Most of your comments are pointless. At a glance I can tell what the code is doing; any reasonably experienced programmer can do this easily. What I really want to know is why you're doing it. I can't extract your thought process from reading the code, and that's where comments really shine.

Narue 5,707 Bad Cop Team Colleague

apvector isn't supported directly by Visual Studio. Do you have the actual header, or are you getting an error that it can't be opened (because it doesn't exist)?

Narue 5,707 Bad Cop Team Colleague

>why did i need the std:: before i cout
Because every standard name is now placed inside the std namespace. To access one of those names, you have to get into the std namespace in one of three ways:

The using directive to open up all names within the scope:

#include <iostream>

int main()
{
  using namespace std;

  cout<<"Hello, world!\n";
}

The using declaration to open up only one name within the scope:

#include <iostream>

int main()
{
  using std::cout;

  cout<<"Hello, world!\n";
}

And explicit qualification:

#include <iostream>

int main()
{
  std::cout<<"Hello, world!\n";
}

>why did i need the float before when i already had declared
>celsius_to_fahrenheit as a float in the beginning before the main function
You need to specify a return type when declaring or defining a function. You did it when you declared celsius_to_fahrenheit, but you failed to do it when you defined celsius_to_fahrenheit.

Narue 5,707 Bad Cop Team Colleague

>i always mix up a NULL and '\0' and 0.
The short answer is that you use NULL with pointers, '\0' with characters, and 0 with everything else.

The long answer is that '\0' and 0 are identical. In C they're both integers with the value 0. You can use them interchangeably, but it's best to only use '\0' in a character context, because it's a character literal. You can use 0 at any time because it's so multi-functional and that fact is well known. You're less likely to confuse people.

NULL is not interchangeable with '\0' and 0 because it may be cast into a pointer type[1]:

#define NULL ((void*)0)

Therefore you can only safely use NULL in a pointer context because if it casts the expression to void*, you'll get mysterious warnings and errors when using NULL in a non-pointer context.

[1] In other words, the standard doesn't require this. It says that "an integer constant expression with the value 0, or such an expression cast to type void*, is a null pointer constant". So you can use 0 instead of NULL in a pointer context as well. 0 always works. NULL may also be defined just as 0, but you can't be sure of it.

Narue 5,707 Bad Cop Team Colleague

The answer is yes. If the function is virtual, you can redefine it in Pigeon to do something different:

#include <iostream>

class Bird {
public:
    virtual void Amethod() {
        std::cout<<"Amethod from Bird\n";
    }
};

class Pigeon: public Bird {
    virtual void Amethod() {
        std::cout<<"Amethod from Pigeon\n";
    }
};

void DoSomething ( Bird *p )
{
    p->Amethod();
}

int main()
{
    Bird B1;
    Pigeon P1;

    DoSomething ( &B1 );
    DoSomething ( &P1 );
}

Or you can leave it non-virtual and it'll do the same thing for both classes:

#include <iostream>

class Bird {
public:
    void Amethod() {
        std::cout<<"Amethod from Bird\n";
    }
};

class Pigeon: public Bird {
};

void DoSomething ( Bird *p )
{
    p->Amethod();
}

int main()
{
    Bird B1;
    Pigeon P1;

    DoSomething ( &B1 );
    DoSomething ( &P1 );
}

Either way, you're relying on polymorphism to allow the DoSomething function to take pointers to objects of both classes.

Narue 5,707 Bad Cop Team Colleague

The documentation will be your best bet for learning your particular implementation. For learning C++, you would be wise to stick to books that follow the standard language closely. "Accelerated C++" is a good choice.

Narue 5,707 Bad Cop Team Colleague

>But, can we just add a feature so that after a certain
>amount of time, the thread is not deleted, but it is closed?
There's really nothing wrong with re-opening a discussion as long as it's relevant. The problem is bumping ancient threads with a "me too!" post, or to ask an unrelated question. However, I don't think that happens often enough to warrant forum logic to stop it. I've also been a member of forums that closed threads after a certain amount of time, and it was surprisingly annoying.

Even though it results in more work on my part as a moderator, I'd still recommend against an automatic solution.

Narue 5,707 Bad Cop Team Colleague

>Of course the user could always use Notepad to view the file.
Oddly enough, my test call for that function was:

showFile ( "notepad.exe", "C:\\narue_is_kewl.txt" );

;)