Narue 5,707 Bad Cop Team Colleague

>using namespace System
Just add a semicolon and you should be solid:

using namespace System;

How do i do an internal class in C++?

The private keyword at class level has the same semantics as internal in C#:

private ref class UserFeedback {
public:
    static void Error(Exception^ e)
    {
    }
};
Narue 5,707 Bad Cop Team Colleague

time(NULL) gives you the current date.

Narue 5,707 Bad Cop Team Colleague

Or, since now and bday are both in seconds, use subtraction rather than calling a function.

now and bday are both time_t. Arithmetic isn't portable, which is why difftime exists in the first place.

Narue 5,707 Bad Cop Team Colleague

difftime returns the difference in seconds:

#include <stdio.h>
#include <time.h>

time_t get_date(int year, int month, int day)
{
    time_t start = time(NULL);
    struct tm *date = localtime(&start);

    date->tm_year = year - 1900;
    date->tm_mon = month - 1;
    date->tm_mday = day;

    return mktime(date);
}

int main(void)
{
    time_t bday = get_date(1978, 11, 11);
    time_t now = time(NULL);

    if (bday != -1 && now != -1)
        printf("You are %.0f seconds old\n", difftime(now, bday));

    return 0;
}

That should get you close enough to tweak the result.

Narue 5,707 Bad Cop Team Colleague

Turbo C is a 16-bit compiler, which essentially means that the actual range of int is [32,767, -32,768] (16 bits, two's complement). It simply can't handle 800000, but long can. The fix in your case is to change your code to use long instead of int.

As a side note, for maximum portability it's best to use int only when the values are guaranteed to be within the range of [32,767, -32,767] (ie. 16 bits). For anything beyond that, use long (up to a range of 32 bits). The reason is that these are the minimum ranges specified in the C standard. You're absolutely guaranteed to have at least that much, but any more is implementation-dependent.

Narue 5,707 Bad Cop Team Colleague

A forward declaration does nothing but introduce the name of a type into the current scope. Headers typically provide full declarations, which include member information as well as the type name.

Narue 5,707 Bad Cop Team Colleague
for(i =0; i <= elgbotsections.size(); i++);

You have a rogue semicolon here that should be removed, it's causing the loop to run to completion without doing anything, then when you try to access the vector, i is out of bounds. Also, it should be i < elgbotsections.size() . You presently have an off-by-one error.

Narue 5,707 Bad Cop Team Colleague

Is this right to say Heap is RAM and Stack is Pagefile(HardDrive)?

No. Both are RAM-based and both can be swapped into virtual memory if they exceed limits. However, the stack size is not dynamic, so it's less likely to dip into virtual memory than the heap. You can set a stack size at build time that exceeds available physical memory, but that has to be an explicit choice as the defaults are typically more reasonable to avoid thrashing simply by executing a process.

Narue 5,707 Bad Cop Team Colleague

According to the book, it said "This is not guaranteed to remove the third element, even though that was intended. In most real-world STL implementations, it erases both the third and the sixth elements."

Beware lack of context. Your quote is actually quite wrong when taken out of context, but the book is using that as an example for an implementation that implements remove_if in terms of find_if and remove_copy_if.

Perhaps it would help if I gave you an alternative implementation of remove_if that matches the description in your book:

#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>

using namespace std;

namespace jsw {
    template <typename FwdIt, typename Pred>
    FwdIt remove_if(FwdIt first, FwdIt last, Pred pred)
    {
        first = std::find_if(first, last, pred);

        if (first == last)
            return first;

        // operator+ is not defined for forward iterators,
        // so operator++ on a dummy is necessary
        FwdIt src = first;

        return std::remove_copy_if(++src, last, first, pred);
    }
}

class FlagNth {
public:
    FlagNth( size_t n ) : current_(0), n_(n) {}

    template<typename T>
    bool operator()( const T& ) {return ++current_ == n_; }
private:
    size_t current_, n_;
};

int main()
{
    int init[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    vector<int> v(init, init + 10);

    copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
    cout<<'\n';

    vector<int>::iterator end = v.erase(jsw::remove_if(v.begin(), v.end(), FlagNth(3)));

    copy(v.begin(), end, ostream_iterator<int>(cout, " "));
    cout<<'\n';
}

This example will fail as described because the predicate is being copied to both find_if and remove_copy_if. Each of their versions of n_ will …

Narue 5,707 Bad Cop Team Colleague

You're getting into the realm of undefined behavior, which makes it harder to answer conclusively. In theory, once a pointer is deleted, the memory block it points to is no longer available, even if you have ten other pointers to the same block. All of those other pointers are silently invalidated. Deleting a pointer does not clear the memory, so if you dereference the pointer before the memory is reclaimed for other purposes, you'll likely get the same data that was originally stored there.

Does that answer your question?

Narue 5,707 Bad Cop Team Colleague

I can see several ways of answering that question depending on what you mean by "actually deleted" and the reason why you would want to check that. Can you elaborate a little bit?

Narue 5,707 Bad Cop Team Colleague

Is it mandatory to use operator= overloading every time i write a copy constructor?

Unless you're going to make operator= private, it's a good idea to include it as well as a copy constructor and a destructor. Because if you need a copy constructor, more often than not you need to deal with assignments and destruction as well (the so called "Big Three"). The defaults typically aren't sufficient if any one of the big three are needed.

Narue 5,707 Bad Cop Team Colleague

But when I run the release version it gives me an error after having run a while.

Not only is your bug dependent on debug vs. release, it's also intermittent (as I understand from "having run a while"). How large is the code base? Are you able and willing to post the code if it's not too big? These kinds of errors are somewhat difficult to troubleshoot with the ol' crystal ball.

I doubt it's a problem with your code since it runs properly in Debug mode.

Oh, if only that were the case. Sadly, some bugs only manifest outside the relative safety of debug mode. Equally sadly, those bugs are usually assigned to me...

Narue 5,707 Bad Cop Team Colleague

Amazing. You failed to provide both pieces of information I requested.

Narue 5,707 Bad Cop Team Colleague

What are the errors? Copy and paste them instead of paraphrasing, please. Also, how is Window2 defined?

Narue 5,707 Bad Cop Team Colleague

itk::Image<T, D>::Pointer is a dependent type, meaning it depends on template parameters for the specific type definition. In these cases you need to specify that it's a type rather than an object:

template <class T,int D>
typename itk::Image<T, D>::Pointer CreateITKImage(int *pnSize, double *pdSpacing = NULL)
{
    // ...
}
Narue 5,707 Bad Cop Team Colleague

rand()%2 is not guaranted to generate random number.

rand is a pseudorandom number generator, it's never guaranteed to be random. Your suggestion is really based on the history of rand, where implementations had a tendency to have very predictable low order bits. The division solution fixed the problem by using high order bits instead.

Further reading here and here.

Narue 5,707 Bad Cop Team Colleague

You're working with single characters rather than strings. Try changing the type of npcname and actid from char to std::string. Also note that the loop won't work too well with strings. You probably want to check all characters.

Narue 5,707 Bad Cop Team Colleague

If you're replacing every instance of a single character with another single character, std::replace from <algorithm> is better suited than any of the std::string::replace overloads:

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

int main(void)
{
    string test;
    test="this is a test";
    replace(test.begin(), test.end(), ' ', '~');
    cout << test <<'\n';
}

Your problem is trying to use an overload that doesn't exist.

Narue 5,707 Bad Cop Team Colleague

How would you use this?

It's less useful in C++ than C89, where all variables must be declared at the beginning of a block. Though unnamed blocks are useful if you want to force a shorter lifetime on resource intensive objects:

// Lots of code

{
    ResourceIntensive ri(...);

    ri.do_stuff();
} // ri is destroyed here rather than taking up resources unnecessarily

// Lots of code
Narue 5,707 Bad Cop Team Colleague
Why????

It might make more sense if you convert to a while loop:

i = 1;

while (i <= 20)
{
    b = i * 30;
    printf("b é uguale a: %d = %d * 30\n\n",b, i);
    ++i;
}

i is always incremented after the body of the loop runs, and when i becomes 21 to fails the condition. So after the loop, i retains the value that failed the condition (ie. 21).

kvprajapati commented: Happy new year ma'am :) +11
Narue 5,707 Bad Cop Team Colleague

Do it your damn self. If you have a specific problem with your code, we'll help, but don't expect anyone to do everything for you.

Narue 5,707 Bad Cop Team Colleague

I use that to study datastructures..

If you'd like to see anything in particular added, just send me a PM or email. I've had trouble updating the site due to time constrains these last couple of years though, so I can't make any guarantees. It takes quite a while to write a proper tutorial.

Whoever is the Author, I am greatly thankful to him/her....

:)

Narue 5,707 Bad Cop Team Colleague

You can make a public globals class with public static fields representing the "global" variables. Not exactly a good design, but it works.

Narue 5,707 Bad Cop Team Colleague

Start by getting a handle on socket programming: http://beej.us/guide/bgnet/

Narue 5,707 Bad Cop Team Colleague

Sure. Think of %[...] as %s where you define the recognized alphabet. Everything inside the brackets represents your custom alphabet, which in this case is {' ', '\t', '\n', '\r', '\v', '\f', ','} . The leading caret (^) alters the behavior to one of exclusion, so "%[^ \t\n\r\v\f,]" means read characters until one of the characters in the brackets is detected. Without the caret, it means that only those characters will be read, and every other character will be used as a delimiter.

Here are two common examples of scan set usage:

#include <stdio.h>

int main(void)
{
    char buf[BUFSIZ];
    int ch;

    /* Only accept digits */
    if (scanf("%[0123456789]", buf) == 1)
        printf("Successful read of the number '%s'\n", buf);

    /* Clear out leftovers */
    while ((ch = getchar()) != '\n' && ch != EOF)
        ;

    /* Read a full line */
    if (scanf("%[^\n]", buf) == 1)
        printf("Read a full line: '%s'\n", buf);

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

the "%s%*c" is adding the comma to the string I sent it to

Yes, because %s is delimited on whitespace. You need to use a scan set to exclude commas along with whitspace: "%[^ \t\n\r\v\f,]%*c".

Narue 5,707 Bad Cop Team Colleague

Perhaps:

public static void main(String[] a) {
    int[] test = new int[] {0, 1, 2, 3, 4};

    reverse(test);

    for (int i = 0; i < test.length; i++)
        System.out.print(test[i] + " ");
    System.out.println();
}

If all you're doing is testing the reverse method, there's really little need to do more. Though some edge cases like null and small/zero sized arrays couldn't hurt to really test it thoroughly.

Narue 5,707 Bad Cop Team Colleague

>Isn't an fscanf string assignment supposed to work on a char pointer (which is what orig_unit->name is)?
Yes, but pointers aren't magic. You can't simply define a pointer and expect it to point to an infinite amount of usable memory, you must first point it to a block of memory owned by your process for the purpose of storing a string:

orig_unit->name = malloc(MAX_NAME);
new_unit->name = malloc(MAX_NAME);

/* ... */

fscanf(stdin, "%f %s %s", &orig_quant, orig_unit->name, new_unit->name);

>Is there any way to manipulate the format characters after the format string?
I don't understand the question. Could you please rephrase it?

Narue 5,707 Bad Cop Team Colleague

>or does it depend on the surrounding environment in weird and unpredictable ways?
It does to a certain extent, but for the most part the conclusions from your simple test will hold in the real application.

Narue 5,707 Bad Cop Team Colleague

Actually, I'm curious if you've profiled having a, b, and c as non-static members of SIMULATION and verified that it's too slow for your needs. Writing off a viable option because you think it's not performant isn't the way to go about design.

Narue 5,707 Bad Cop Team Colleague

guard_rec is an array, not a struct instance. You need to index into the array before any member access can be done:

/* Where i is a suitable index */
guard_rec[i].first_name
Narue 5,707 Bad Cop Team Colleague

By all means try, but you strike me as the overly romantic type. Reality can be a harsh teacher, and I'd rather it not turn you off to professional development simply because there's a rather low chance of success at creating the "next big thing".

Narue 5,707 Bad Cop Team Colleague

You're a second year CIS student and don't even know where to start? I weep for this generation of programmers. How about starting with one account class and working out the credit/debit actions? From there you can add more accounts, set up branches, etc... This project practically slaps you in the face with the bottom-up progression of design, implementation, and testing.

Narue 5,707 Bad Cop Team Colleague

>void main() is not legal C (under a hosted environment).
Incorrect. Since you seem to be a standard reader, look it up and you'll see "or in some other implementation-defined manner". This is an escape clause that allows implementations to support extended functionality, such as not requiring a return value (ie. void main) or additional parameters such as the common envp third parameter.

>Your options are (under C99):
>int main(int argc, char *argv[], char *envp[]);

Incorrect. C99 only defines explicitly a zero parameter version:

int main(void) { /* ... */ }

And a two parameter version:

int main(int argc, char *argv[]) { /* ... */ }

Your three parameter version falls under the escape clause, though it is mentioned as a common extension in annex J section 5 of C99.

Narue 5,707 Bad Cop Team Colleague

main is a special function because it's the program's entry point. Thus, there's a certain measure of compiler magic involved. During compilation the compiler will determine which version of main to call based on your definition.

Narue 5,707 Bad Cop Team Colleague

>but still in ur opinion, what technologies are best?
In my opinion, if "best" were so easily determined, there wouldn't be so many options.

>pls do suggest then what type of licensing is available
Google "open source licenses".

and its still not clear to me, if just by coding using open source APIs and languages,does it automatically make my code "freely ditributable" or does it happen only if i make the code distributable after licensing under GNU GPL?

Unless you're using a restrictive library that forces derivatives to be open source, you decide how the code is licensed. However, open source libraries that are used may have provisions that require open distribution of those parts. For example, you might be required to include full source for an open source library that you use along with the binaries of your otherwise closed source application.

Then is the reason that most people don't use Microsoft technologies for coding that they run on windows only?

No, portable code can be written on Windows. The Microsoft hate stems from politics. People don't like Microsoft as a corporation for various reasons, and they project that dislike onto Microsoft products. Sometimes the haters will find legitimate flaws in the products to attack, while at the same time ignoring similar flaws in their preferred alternative. It's both sad and humorous.

logically i'd believe its their APIs and stuff so they would charge atleast (demand a piece of the pie) something if …

Narue 5,707 Bad Cop Team Colleague

>is there any getchar equivalent for ints ?
If you mean something super simple like int x = get_int(stdin); then no. A usable function along those lines can easily be written though:

#include <stdio.h>
#include <limits.h>
#include <ctype.h>
#include <errno.h>

int get_int(FILE *in, unsigned *n_digits)
{
    const unsigned base = 10;
    const unsigned safe = UINT_MAX / base;
    const unsigned lsd = UINT_MAX % base;

    unsigned overflow = 0;
    unsigned sign = 0;
    unsigned temp = 0;
    unsigned n = 0;
    int value = 0;
    int ch;

    if ((ch = getc(in)) == '-' || ch == '+') {
        sign = ch == '-';
        ch = getc(in);
    }

    for (; isdigit(ch); ch = getc(in), n++) {
        unsigned digit = ch - '0';

        overflow = temp > safe || (temp == safe && digit > lsd);

        if (overflow)
            break;

        temp = temp * base + digit;
    }

    ungetc(ch, in);

    if (n > 0) {
        if (overflow || (sign && temp > -INT_MIN) || (!sign && temp > INT_MAX))
            errno = ERANGE;

        value = sign ? -(int)temp : (int)temp;
    }

    if (n_digits != NULL)
        *n_digits = n;

    return value;
}

int main(void)
{
    unsigned n = 0;
    int x = get_int(stdin, &n);
    int ch;

    if (errno == ERANGE)
        fprintf(stderr, "Error: Number exceeds [%d, %d]\n", INT_MIN, INT_MAX);
    
    if (n == 0)
        fprintf(stderr, "Error: No integer detected\n");
    else
        printf("Detected %d with %u digits\n", x, n);

    fputs("Characters remaining: '", stdout);

    while ((ch = getchar()) != '\n')
        putchar(ch);

    puts("'");

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

>The reason I want to hide my strings is something you should not consider.
Then I won't consider helping you. Bye.

>The reasons can be paranoid, ineffective, stupid, ridiculous or what else, that's *not* the point.
The reason can be anything, but there must be a reason. If it turns out that your reason is stupid and you don't realize it, I can help you understand and save you a lot of wasted time.

Narue 5,707 Bad Cop Team Colleague

>How to avoid this?
Let's start by asking why you want to do it. I can think of a few reasons to obfuscate the executable, and for the most part those reasons are either paranoid and ineffective, or easily avoided with proper design.

Narue 5,707 Bad Cop Team Colleague

You realize that getchar is reading one character and returning the code value of that character, right? If you type "100", the values you get are likely to be 49, 48, and 48. Is this intended? Because it raises red flags when I read it.

Narue 5,707 Bad Cop Team Colleague

>Do me 1 favour...
Not only will I do you a favor, it'll be the single best favor you'll ever receive. I'm going to let you bomb this assignment. Then you'll learn that nobody is going to bail you out, that you should only rely on yourself, and you should expect the consequences of failure rather than a Hail Mary from people who couldn't give two shits about your marks.

You sat on your laurels for half a month (a timeframe that I commonly finish full projects in, much less synopses) and totally deserve to suffer because of it. Don't expect sympathy from any of us, because you're not likely to get it.

Jason Giggs commented: Now that's tough. :-) +1
Narue 5,707 Bad Cop Team Colleague

most of my friends advise me to go for open source development: use LAMP (linux,apache,mysql,php) combination if i ever develop a web app, or use OpenGL if developing desktop apps in c++ so that they can run on linux apart from windows only, or use java if ever getting thoughts of .net in my head.

Ignore them. A wise developer uses the best tool for the job; getting political only hurts the quality of software.

if i write some application in open source languages and environments, can i have the developer rights to it and use it commercially for my benefit?(provided i won't obviously use third party add ons)

Open source isn't synonymous with "non-commercial". Each license details it in a different way, but there's generally nothing stopping you from making a profit on the software provided the source remains available and royalty free.

is it correct that if i write desktop apps in vc++ or vb or c# or create pages in asp.net which are Microsoft's languages and technologies, then Microsoft is also the owner of that app because the technology used is Microsoft's , and i can't have any rights to it to use it for making money?

No, that's ridiculous.

Narue 5,707 Bad Cop Team Colleague

Okay, I can see what you were thinking now. It won't work, but at least it's logical from a beginner's perspective. ;) If you want to burn cards, simply remove them from the deck somehow (how depends completely on how your deck is designed). There's no need to explicitly call the destructor.

Narue 5,707 Bad Cop Team Colleague

I think the appropriate question in this case is why do you want to invoke the destructor explicitly? It's called automatically at the right time except in some extreme and obscure situations (which you're not likely to encounter for a while).

Narue 5,707 Bad Cop Team Colleague

You can signal EOF with a keyboard combination. On Windows it's Ctrl+Z, and on POSIX systems it's Ctrl+D.

Narue 5,707 Bad Cop Team Colleague

<broken record>
What have you tried so far? Do you have any code? If so, please post it. If not, go give the assignment an honest attempt before asking for help!
</broken record>

Narue 5,707 Bad Cop Team Colleague

The invocation would look something like this:

$ g++ file1.cpp file2.cpp file3.cpp
$ ./a.out

The header files need not be compiled as the preprocessor textually inserts them into your .cpp files. The resulting executable file is named a.out by default, but you can alter that with the -o switch:

$ g++ -o myprog file1.cpp file2.cpp file3.cpp
$ ./myprog
Narue 5,707 Bad Cop Team Colleague

>u = getchar() - 48;
If getchar returns '\n', you still subtract 48

>if ((u != '\n')&&(j<(n*n))&&(u != 0))
The only way u will ever match '\n' is if you type ':' (with a value of 58 in ASCII/Unicode). So even if you type a newline, this condition will never catch it properly. What you need to do is validate that the character is a digit before subtracting 48:

u = getchar();

if (u >= '0' && u <= '9')
    u -= '0';

/* Now the test for '\n' will work */
if (u != '\n' && j < n * n && u != 0)

However, you also need to consider other non-digits, but that's extraneous to the current problem of recognizing a newline character.

Narue 5,707 Bad Cop Team Colleague

Um, yes? I really have no idea what you're asking. Please be a little less vague.