Tom Gunn 1,164 Practically a Master Poster

There is not a short and easy way to do that. If you need to search your variables for matching values, then they're related enough to put in an array:

int a[] = {4, 2, 4, 4};

And if you need to keep the names, it's only slightly more complex:

struct Variable
{
    char *name;
    int value;
};

struct Variable a[] =
{
    {"a", 4},
    {"b", 2},
    {"c", 4},
    {"d", 4},
};

An array makes the whole process of looking for matches much easier because you can throw everything in a loop:

for (int x = 0; x < sz; ++x)
{
    if (a[x] == key) ++matches;
}
Tom Gunn 1,164 Practically a Master Poster

Shouldn't the compiler be reporting that?

How is the compiler supposed to know? All it sees is a char pointer, not a char pointer to read only memory. The compiler assumes you know what you're doing and doesn't slow down compilation with expensive checks to protect you from something like that.

If you want that kind of protection, you need to help the compiler along by using const char *p instead of char *p when pointing to something that shouldn't be modified.

Tom Gunn 1,164 Practically a Master Poster

It's worked for other programs I don't know why it wouldn't work here.

In the other programs you weren't working with a template class. The whole definition of the template class has to be in one file, you can't split it up without using the export keyword. Unfortunately, export is not supported by many compilers. Check your compiler's documentation to see if it allows export, otherwise you need to stuff everything in the header.

Tom Gunn 1,164 Practically a Master Poster

You didn't implement the destructor or any of the other methods in the DynStack class. The only method with a body is the default constructor.

Tom Gunn 1,164 Practically a Master Poster
DynStack<double> doubleStack();
DynStack<float> floatStack();

C++ syntax is kind of weird here. The compiler thinks you are trying to declare two functions, not instantiate two objects. For named objects, the default constructor does not need parentheses, and the syntax no longer looks like a function:

DynStack<double> doubleStack;
DynStack<float> floatStack;
Tom Gunn 1,164 Practically a Master Poster

I want to not see any warnings at all when I build.

I haven't seen many warnings that were so asinine they could be completely ignored. Disabling warnings just so you can get a clean compile is not a recommended practice because fixing them almost always makes your code better.

Separating warnings and errors makes sense, and that's the only time I can think of where you might want to use -Wno. But a better way to split up the errors and warnings is redirecting the output of the compiler to a file and then grepping the file on warnings and then errors. That way you don't lose any output and still have a way of focusing on one or the other.

Tom Gunn 1,164 Practically a Master Poster

It looks like you want a 2D array:

int P[] =
{
    {0, 1, 1, 2},
    {0, 3, 1, 0}
};

for (int x = 0; x < 2; ++x)
{
    for (int y = 0; y < 4; ++y)
        cout << P[x][y] << ' ';
    cout << '\n';
}

Is that right? Or am I missing something that makes your problem not so simple? :(

Tom Gunn 1,164 Practically a Master Poster

The method is marked as const, so calling find on DoubleValues will use the overload that returns a const_iterator. You can't assign a const_iterator to an iterator, but because the method is const, it should be safe enough to change the iterator like this:

bool OrientedPoint::getDoubleValue(const std::string &ValueName, double &Value) const
{
    std::map<std::string, double>::const_iterator MyIter;
    MyIter = DoubleValues.find(ValueName);

By the way, it's cleaner and usually more efficient to initialize objects in the constructor instead of through assignment:

bool OrientedPoint::getDoubleValue(const std::string &ValueName, double &Value) const
{
    std::map<std::string, double>::const_iterator MyIter = DoubleValues.find(ValueName);
Tom Gunn 1,164 Practically a Master Poster

The overload is not needed. As long as the function parameter is a const reference to string, you can pass a string constant and it will become a string object. Libraries usually have the overload to avoid the cost of constructing a new string object, but it is not required.

Tom Gunn 1,164 Practically a Master Poster

You are not allocating any memory that needs to be freed. You are using FILE pointers, so you need to call fclose() on each one, but that's all. Can you be more specific about what the problem is?

Tom Gunn 1,164 Practically a Master Poster

How about a list of objects that represent the data:

class Cost // needs a better name
{
    public string Code { get; private set; }
    public string Description { get; private set; }
    public double Amount { get; private set; }
    public double ExchangeRate { get; private set; }
    public  DateTime Date { get; private set; }

    public Cost(DataRow row)
    {
        // populate the object with data
    }
}
DataTable table = GetData(); // simulate reading from a DB
List<Cost> costs = new List<Cost>();

foreach (DataRow row in table.Rows)
{
    costs.Add(new Cost(row));
}

Data tables and sets are fine for what they do, but working with them directly is tedious. Code reads better when you abstract the tedious stuff away. :)

Tom Gunn 1,164 Practically a Master Poster
Tom Gunn 1,164 Practically a Master Poster

A little addition on:

Array names become pointers

Yes, an array name is a constant pointer to the first element of that array.

An array name is the name of an array object, nothing more, nothing less. When used as a value, it is converted to a pointer to the first element. When used as an object, there is no conversion. It's a subtle difference, but important when you get to object usage like char a[] = "string" , &a , and sizeof(a) . All of those work the right way because the array is not converted to a pointer.

tux4life commented: Yes you're right :) +15
Tom Gunn 1,164 Practically a Master Poster

i would like to ask if this method should be called after ever cin statement?

For anyone else reading, the method in question is here. This thread used to be a post in that thread.

operator>>() is the problem child here. It works something like this:

// beware: pseudo-code
ostream& operator>>(ostream& os, T obj)
{
    SkipWhiteSpace(os);
    ReadObject<T>(obj);
    return os;
}

Any white space after the object will not be touched until the next call to operator>>(). If the stream has " abcd\nefgh" , here is what happens:

cin >> s1;        // s1 == "abcd"
getline(cin, s2); // s2 == ""
getline(cin, s3); // s3 == "efgh"

operator>>() does its thing by skipping leading white space and reading the object. It stops on white space thinking that the next call to operator<<() will skip it. But getline does not skip leading white space, and it even uses '\n' as a stopping character by default.

You only need to use the method from that thread in cases where the next input call will give you the finger. ;) A good guideline is clear the stream after the formatted input whenever you mix formatted and unformatted input.

i was thinking of trying a loop to eat everything but i don't know if that would cause problems.

A loop is good, but it needs to stop on '\n' too, and using eof() in the loop condition should be avoided because it has the wrong behavior and there …

Tom Gunn 1,164 Practically a Master Poster

yes, you're right.

even though it works for this trivial example, not casting it properly is a bad habit to get into.

the correct way to initialize it is:

char q[16];
char *p = [b](char *)[/b]&q;
strcpy(q,"abc");

So you get the wrong type, then cast it to the right type and rely on the starting address being the same for both to make it all work out. Why not use the right type from the start and not need to cast at all?

/* better */
char q[16];
char *p = q;
strcpy(q,"abc");

Array names become pointers when used as a value. But you don't need to do any of that because q can be passed to strcpy directly, or even initialized to a string constant:

/* easier */
char q[16];
strcpy(q, "abc");
/* even easier */
char q[16] = "abc";

I can't figure out why you have a magic number of 16, but there's probably a reason. Otherwise you don't need any size if you initialize to a string constant:

/* easiest */
char q[] = "abc";

You can't get much shorter than that for a modifiable string. ;)

Salem commented: Quite so +36
tuse commented: super cool post +2
Tom Gunn 1,164 Practically a Master Poster

The read() function returns the stream, so you should be able to signal EOF to stop the first loop. On Linux it's a Ctrl+D key combination and on Windows it's Ctrl+Z.

theashman88 commented: Quick help +1
Tom Gunn 1,164 Practically a Master Poster

That's a lot of stuff to look at. Can you boil it down to something small enough to post in code tags?

Tom Gunn 1,164 Practically a Master Poster

This is just copied from an example, so I don't know exactly what it means. The only thing I actually know is that the word 'export' is defined.

__declspec(dllexport) is the magic incantation to add a name to the export table of a DLL. extern "C" is C++'s way of disabling the usual name mangling that makes overloading and such easier for compilers to manage. They're both really just boilerplate unless you care to learn more about the details of compilers and DLLs. ;)

The #define part says that after that line, everywhere you say export , you mean extern "C" __declspec (dllexport) . These two function tags are the same:

export
double sound_load(char *filename, double loop)
extern "C" __declspec (dllexport)
double sound_load(char *filename, double loop)

'#include' means that a list of functions is included [a library it's called I believe].

You include headers, and headers contain declarations for types, objects, and functions. When you say #include [I]{header}[/I] , the contents of the header are pasted into the source file just like if you had written it all out yourself.

As I said before, I don't get a thing at what they say at MSDN, so I was hoping someone could clearly explain what PlaySound needs [which and what type of arguments].

The first argument is a string, a file name for example. The second argument will probably be NULL because MSDN says it's NULL unless the third argument is SND_RESOURCE. The third argument is whatever …

Tom Gunn 1,164 Practically a Master Poster
while(!infile.eof()){

The problem with this condition is it is out of order. eof() does not return true until after an unsuccessful call has been made to read from the stream. You should do this test after the input call:

while (true)
{
    infile.getline(temp, 1024);

    if (infile.eof()) break;

    // ...
}

But the powers that be already figured out a better way. getline() returns a reference to the stream, and the stream has a conversion to a type that can be used as a loop condition:

while (infile.getline(temp, 1024))
{
    // ...
}
some_variable = atoi(temp);

Two things. First, to use atoi you should include <cstdlib> or <stdlib.h>. That's the only header it is required to be declared in. Second, atoi is a dangerous function because there's no way to check for failure before doing damage.

For C, strtol is a better choice, and for C++ string streams or Boost::lexical_cast are much safer and more flexible.

Tom Gunn 1,164 Practically a Master Poster

I see your point, but why add new rules that could break a lot of existing code when RAII and libraries already cleanly solve the memory leak problem? Is there something else you're worried about besides memory leaks that makes you want to avoid pointers?

Tom Gunn 1,164 Practically a Master Poster

it seems to me if you don't use pointers then you never have memory leak type problems...

I wish that were so. :( Raw pointers are dangerous, and that's why smart pointers and RAII are so important. Even if you use the troublesome auto_ptr, your leak problem can be fixed:

std::auto_ptr<Employee> emp;

if (condition1)
    emp.reset(new Employee());
else if (condition2)
    emp.reset(new Manager());

The reason your second example does not work is because the new object goes out of scope at the end of the conditional block. If you use new then the object is not bound by scope restrictions as long as you have a pointer to it.

Tom Gunn 1,164 Practically a Master Poster

2D array arguments need a size for all but the first dimension. If you don't know the size until run time, or the function has to take different sized 2D arrays, you can use a pointer to a pointer instead. Then the calling code can either allocate memory to a pointer to a pointer or convert the array somehow. Note that a 2D array type is not directly convertible to a pointer to a pointer, so you have to do some kind of conversion. Here is a way that does not use malloc:

#include <stdio.h>

void f(int **p, int m, int n)
{
    int x, y;

    for (x = 0; x < m; ++x)
    {
        for (y = 0; y < n; ++y)
        {
            printf("%-4d", p[x][y]);
        }

        puts("");
    }
}

int main()
{
    int array[3][3] = 
    {
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}
    };
    int *p[3];
    int x;

    for (x = 0; x < 3; ++x) p[x] = array[x];

    f(p, 3, 3);

    return 0;
}

Another way is passing just a 1D pointer and calculating the size of each dimension manually. It's not portable, and I don't recommend the trick, but I haven't seen any systems yet that it doesn't work on:

#include <stdio.h>

void f(int *p, int m, int n)
{
    int x, y;

    for (x = 0; x < m; ++x)
    {
        for (y = 0; y < n; ++y)
        {
            printf("%-4d", p[x * m + y]);
        }

        puts("");
    }
}

int main() …
Tom Gunn 1,164 Practically a Master Poster

I agree that this style is not officially declared 'deprecated'. But when an authority such as one of the standard makers (Marshall and Stroustrup) deprecates it, you surely have less choice.

If it's not officially deprecated, then whatever individual 'deprecates' it is only expressing a personal opinion on programming style. You should not propagate misinformation, because 'deprecated' is well known and understood in C++ circles to mean exactly what the standard says it means and nothing else.

If you just wan't to prove my words wrong..... I really can't help it.
Fine?

Fine. I will just keep correcting you when try to mislead people into believing your biased opinions.

Tom Gunn 1,164 Practically a Master Poster

@Tom:
http://en.wikipedia.org/wiki/Deprecation
http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.4
http://www.research.att.com/~bs/sibling_rivalry.pdf point no. 2.5

I won't accept those links. I want you to quote the standard, the only authority on what is or is not deprecated, where it says a parameter list of (void) is deprecated.

It is wrong to say something is deprecated when you really mean 'unconventional'. Deprecated means that the future of that thing is questionable and code that uses it could break anytime in the near future without any changes. That is a real argument for not using a feature, while 'unconventional' just means that readers might stumble over the code.

Tom Gunn 1,164 Practically a Master Poster

Wouldn't a simple If Not Defined wrapped around the include solve the "conflict"?

Yes. Even better is the usual convention of wrapping everything in the header with a conditional compile test:

#ifndef HEADER_H
#define HEADER_H

// header contents

#endif

Then it doesn't matter how many times the header is included, or if it is included recursively. The translation unit will only have one copy of the contents and you don't have to worry about conditionally compiling the include directives one by one to fix ambiguities.

Tom Gunn 1,164 Practically a Master Poster

I want to add a string of numbers(e.g 9999) which are stored in a text file.

Are you just adding the digits? If so then for each character that is a digit, you can subtract '0' from it to get the integer value:

'0' - '0' = 0
'1' - '0' = 1
'2' - '0' = 2
'3' - '0' = 3
'4' - '0' = 4
'5' - '0' = 5
'6' - '0' = 6
'7' - '0' = 7
'8' - '0' = 8
'9' - '0' = 9

But make sure that you only do this and add to the sum if the character is a digit. There might be whitespace in your file that can skew the sum. The best way to check for a digit is with the isdigit() function from <ctype.h> or <cctype> on newer C++ compilers.

One big problem with your code is the loop condition. First, ch is not initialized, and accessing an uninitialized variable on the first iteration invokes undefined behavior. Next, ch is a char type, and the char type is not guaranteed to be signed, but EOF is always a negative value. The loop might run forever. This is the idiomatic way to read characters in a loop from C:

int ch; // fgetc returns int

while ((ch = fgetc(fptr)) != EOF)
{
    /* ... */
}

And the obligatory portability comments. main returns int, but returning void won't work everywhere. clrscr() isn't a …

Tom Gunn 1,164 Practically a Master Poster

>I just thought syntactically that it was incorrect
No it isn't. Syntactically it is fine but it is deprecated.

Just because you don't like a feature or it was inherited from C doesn't make it deprecated. ;) To the best of my knowledge, (void) parameter lists are not deprecated. Please quote chapter and verse from the standard that says it is, because I can't find it and you seem pretty sure of yourself.

Tom Gunn 1,164 Practically a Master Poster

1) This is exactly what I'm encountering. I googled for that but none of those posts talked how to do this:
member function B::fb makes a call to _beginthreadex with thread function which is a member function ( A::fa ).

Google probably doesn't talk about it because that is not possible directly. _beginthreadex takes a function pointer as the argument, not a member function pointer. The two pointer types are not compatible because function pointers are real pointers while member function pointers are just an offset into a class' member function table with pointer-like syntax. To call a member function through a function pointer callback you need to wrap the member function call in a regular function:

#include <iostream>

namespace Daniweb
{
    class Test
    {
    public:
        void f() { std::cout << "member function call\n"; }
    };

    void f(void *obj)
    {
        ((Test*)obj)->f();
    }

    void callback(void (*fp)(void *), void *obj)
    {
        (*fp)(obj);
    }
}

int main()
{
    callback(&Daniweb::f, new Daniweb::Test());
}

2) It generates error C3867: 'A::fa': function call missing argument list; use '&A::fa' to create a pointer to member.
But I don't know how to add &A::

The way member function pointers work, you need an object and the pointer. The pointer is &<class>::<function> and the object is whatever instance of <class> you have. Then they're put together with a special indirection operator:

#include <iostream>

namespace Daniweb
{
    class Test
    {
    public:
        void f() { std::cout << "member function call\n"; }
    };

    void callback(void (Test::*fp)(), Test *obj) …
Tom Gunn 1,164 Practically a Master Poster

Any tutorial on the basic language will cover loops and arrays. Do you have more specific requirements for the program? It is hard to help with something vague like 'loops and arrays'.

Tom Gunn 1,164 Practically a Master Poster

Try this code:

#include <iostream>
#include <string>

using namespace std;

void Menu();

double money = 50.00;

void Mcdonalds()
{
    cout << "Welcome\n";
    cout << "How can i help you?\n\n";
    int mcdonalds;
    cout << "1.Eat \t £2.50\n";
    cout << "2.Drink \t £1.00\n";
    cout << "3.Buy 1/6 of Mcdonalds \t £20.000\n";
    cout << "4.Leave\n";
    cin >> mcdonalds;

    switch (mcdonalds)
    {
    case 1:
        if (money >= 2.50)
        {
            cout << "You have eaten and are no longer hungery\t";
            money = money - 2.50;
            cout << "your money is: £" , money, "\n";
            Mcdonalds();
        }
        else {
            cout << "You can not afford this";
            Mcdonalds();
        }
        break;
    case 2:
        if (money >= 1.00)
        {
            cout << "You are no longer thirsty";
            money = money - 1.00;
            cout << "your money is: £" , money, "\n";
            Mcdonalds();
        }
        else {
            cout << "you can not afford this";
            Mcdonalds();
        }
        break;
    case 3:
        Mcdonalds();
        break;
    case 4:
        Menu();
        break;
    }
}

void Menu()
{
    int menu;
    cout << "You are outside\n";
    cout << "What do you want to do?\n";
    cout << "\n\n";
    cout << "1. Mcdonalds\n";
    cout << "2. School\n";
    cout << "3. Work\n";
    cout << "4. Gym\n";
    cout << "5. JD\n";
    cin >> menu;

    switch (menu)
    {
    case 1:
        Mcdonalds();
        break;
    }    
}

// home
void Home()
{
    cout << "You are at home\n";
    cout << "What do you want to do?\n";
    cout << "type either 1 or 2. numbers above 2 will also count as 2.\n";
    int home;
    cout << "1. sleep\n";
    cout …
Tom Gunn 1,164 Practically a Master Poster

then what??? looool what's the criteria??

Don't ask me, I just joined Daniweb. :(

Tom Gunn 1,164 Practically a Master Poster

Find out what processes are taking up all of your physical memory and kill them or increase your physical memory. Dipping into virtual memory too much is a bad thing because it is much slower than real memory. You can increase the virtual memory space, but that's probably just a band-aid for the symptom of the problem.

What operating system are you using?

Tom Gunn 1,164 Practically a Master Poster

You can declare a function without defining it if you replace the curly brackets and code with a semicolon:

void Menu();

void McDonalds()
{
    // ...
}

void Menu()
{
    // ...
}
rtwister commented: thanks +1
Tom Gunn 1,164 Practically a Master Poster

Just explain what is special about "essential" then?

Everyone is special. :D It's not like I pick the featured people, but if there are tons who are active, smart, and well liked, it makes sense to look for the ones that stand out somehow.

Maybe the 'every second reply is supported by helpful coding' part of the nomination made essential stand out from the list of clones.

Tom Gunn 1,164 Practically a Master Poster

if i compare number of his posts or reputation points or solved threads, i think Ramy or Scott(sknake) must be granted these before than him.

If the criteria for being featured are post count, solved thread count, and reputation points then there are a lot of people in line before essential, or the two you mentioned. There has to be some other criteria, because look at people like Salem who aren't featured but blow everyone else away.

I dont know but this is not fair, and this will demotivate our valuable posters

I think the valuable posters are more interested in being valuable than being recognized as valuable.

ddanbe commented: Nice:I think the valuable posters are more interested in being valuable than being recognized as valuable. +7
Tom Gunn 1,164 Practically a Master Poster

Hash functions can handle more than just integers. The generic algorithms can hash any collection of bytes you give them:

unsigned int hash(void *key, int sz)
{
    unsigned char *p = (unsigned char*)key;
    unsigned hval = 0;

    for (int x = 0; x < sz; ++x)
    {
        hval = 31 * hval ^ p[x];
    }

    return hval;
}

A good place to start is here.

Tom Gunn 1,164 Practically a Master Poster

*Implementation of the Traveling Salesman Algorithm on Networking Systems.
*Graph Traversal System Using Best Path Method .
*Analysis on the Different Advanced Algorithms
*Implementation of the Closest Pair Algorithm on Networking Systems
*Comparative Study on the Efficiency of Sorting Algorithms

These have been done to death. If you are lazy you can search for stuff other people have done, but if you want a thesis that stands out among the crowd, coming up with something more unique is recommended. ;)

*Comparative Study on the Structure of Java and C Sharp Languages

Yawn. How about something challenging and useful like a comparative study on the structure of C# and Haskell throughout their evolution, with an eye toward the benefits of functional programming over imperative programming and vice versa?

Salem commented: Yeah, I thought "thesis" was supposed to be a unique effort, not some rehash +36
Tom Gunn 1,164 Practically a Master Poster
iTemp = inFile >> day[numRecords];
iNew  = inFile >> score[numRecords];

iTemp and iNew are being assigned references to inFile, those values are not meaningful to you. I guess you wanted iTemp to match day[numRecords] and iNew to match score[numRecords]:

inFile >> day[numRecords];
inFile >> score[numRecords];

iTemp = day[numRecords];
iNew = score[numRecords];

But even with that, your loop condition isn't going to stop at the right place and the program will keep trying to go past the 19th index of day and score. What is this code supposed to do?

Tom Gunn 1,164 Practically a Master Poster

Without more code to look at, I can only guess. But my guess is that he didn't include the right header for strdup and casted the result to silence his compiler's warnings about it.

Tom Gunn 1,164 Practically a Master Poster

In that example you assume that an integer is 4 bytes.

No assumptions, we already established that sizeof(int) is 4 on his machine. I went ahead and used 4 instead of sizeof(int) for the maths because it would just add unnecessary complexity where hard numbers simplify everything.

I do not think it's relevant to this thread, but it is good to re-emphasize that type sizes are not fixed whenever you can. It confuses a lot of people who only work with PCs. ;)

Tom Gunn 1,164 Practically a Master Poster

I assumed that _C was unique, but that was wrong. Regular expressions are perfect for this, but you can do it manually too:

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

int MatchCvsmPattern(const char *str, int values[4])
{
    /* find the starting value */
    while (str = strstr(str, "_C"))
    {
        const char *fmt = "_C%d_V%d_S%d_M%d%n";
        int c, v, s, m;
        int end = 0;

        /* match the pattern */
        if (sscanf(str, fmt, &c, &v, &s, &m, &end) == 4)
        {
            values[0] = c;
            values[1] = v;
            values[2] = s;
            values[3] = m;

            /* check for a pattern at the end of the string */
            if (str[end] == '\0') return 1;
        }

        str += 2; /* skip "_C" */
    }

    return 0;
}

int main()
{
    const char *samples[] = 
    {
        "ABC_DEF_GHI_JKL_C123_V45_S67_M89",
        "A11_BCDEF_CADT1_C123_V45_S67_M89",
        "TAG_NAME_C123_V45_S67_M89",
        "SEFDF_CC_C444_QWEEEE123124234_AC_C123_V45_S67_M89"
    };
    int x;

    for (x = 0; x < sizeof samples / sizeof *samples; ++x)
    {
        int values[4] = {0};

        if (MatchCvsmPattern(samples[x], values))
        {
            printf("%d: C=%-4dV=%-4dS=%-4dM=%-4d\n", 
                x, values[0], values[1], values[2], values[3]);
        }
    }

    return 0;
}

You said that the values are at the end, so I put extra tests in there to make sure any matching pattern is the last thing in the string. If your way works then it works. Mine is just another way. :)

Tom Gunn 1,164 Practically a Master Poster

But what you are saying is exactly the opposite of what the output is. The output for (char *) is 200 and for (int *) is 50.

You are right. Sorry, I was explaining how the sizes added up instead of explaining your output.

Subtracting pointers will always count the number of objects between address A and address B, not the number of bytes. The size of an object is dependent on the type of the pointers being subtracted. If you use char pointers to get the difference, each object will be a byte. If you use int pointers, each object will be 4 bytes:

#include <stdio.h>

int main()
{
    int a[10];

    printf("%d\n", (int*)(a + 10) - (int*)a);
    printf("%d\n", (char*)(a + 10) - (char*)a);

    return 0;
}

The type of the array is int, so the size of the array is 10 * 4. If you subtract the first address and the last address using an int pointer, the result will be 10 because the size of each object is 4. If you subtract the same addresses with char pointers, the view changes so that each object is only 1 byte, and the result will be 40 because 10 * 4 bytes is 40 bytes.

You can do the same thing in reverse:

#include <stdio.h>

int main()
{
    char a[40];

    printf("%d\n", (int*)(a + 40) - (int*)a);
    printf("%d\n", (char*)(a + 40) - (char*)a);

    return 0;
}

Instead of an int array of 10, it's a char array …

Tom Gunn 1,164 Practically a Master Poster

This is what sscanf was born to do:

#include <stdio.h>

int main()
{
    const char *p = "TAGNAME_C123_V45_S67_M89";
    int c, v, s, m;

    if (sscanf(p, "%*[^_]_C%d_V%d_S%d_M%d", &c, &v, &s, &m) == 4)
    {
        printf("C=%d\nV=%d\nS=%d\nM=%d\n", c, v, s, m);
    }

    return 0;
}
Tom Gunn 1,164 Practically a Master Poster

You could probably dig around and figure out what the problem is, but an easy first step is to reinstall/repair Visual Studio. Make sure the installation is clean before doing any crazy troubleshooting. It could just be a corrupt DLL or two. :)

Tom Gunn 1,164 Practically a Master Poster

So q+1 is address immediately after the 50 elements but how does the type casting affects the output.

Pointer subtraction takes the size of the type into account. If it's a pointer to char, the size is 1 and with 50 chars the result will be 50. If it's a pointer to int, the size is sizeof(int), and with 50 ints the result will be 50 * sizeof(int).

On your system sizeof(int) looks to be 4, and 50 * 4 = 200.

Tom Gunn 1,164 Practically a Master Poster

2. In the above code snippet, when I define class Sampler as a local class, g++ shoots an error. Why is it that compiler works fine with vector vec3's declaration when class Sampler is global and shoots an error when Sampler is local to main?

It is just one of those restrictions in the standard. Template type arguments cannot refer to a type without linkage, or a pointer to a type without linkage, and local class definitions match that restriction.

You can read the details in the draft, section 14.3.1.2.

Tom Gunn 1,164 Practically a Master Poster

That looks like generated C++/CLI code. None of that fancy stuff will work under the oldSyntax setting. You have to start with an empty project and build it manually, I think. Or find a tutorial that covers the new syntax. The MSDN library has example code in multiple langauges for all of the classes in System::IO and they are up to date for the framework version. C++/CLI is always included from what I've seen. For example.

Tom Gunn 1,164 Practically a Master Poster

The tutorial tells you what to do to compile as Managed C++ instead of C++/CLI:

Note In Visual C++ 2005, you must add the common language runtime support compiler option (/clr:oldSyntax) to successfully compile the previous code sample as Managed C++. To add the common language runtime support compiler option, follow these steps:

  1. Click Project, and then click ProjectName Properties.
    Note ProjectName is a placeholder for the name of the project.
  2. Expand Configuration Properties, and then click General.
  3. In the right pane, click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the Common Language Runtime support project settings.
  4. Click Apply, and then click OK.

For more information about common language runtime support compiler options, visit the following Microsoft Developer Network (MSDN) Web site:
http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx (http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx)

Tom Gunn 1,164 Practically a Master Poster

Basically it comes down to this: If you don't either (1) prototype the function or (2) fully define the function prior to its use

Simplify, my friend. :) A function definition is also a prototype, so you don't need to split them up. As long as there is a prototype before the first use, you are safe. If there is an old style declaration or no a declaration at all before the first use, you are not so safe.

Tom Gunn 1,164 Practically a Master Poster

What is your canceling function supposed to do?