deceptikon 1,790 Code Sniper Team Colleague Featured Poster

how to overload bitwise i.e << and >> operators ?

The same way you overload any other binary operator. There's no magic to the shift operators, though if you're not careful then precedence rules might bite you in the ass when you use them.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

What compiler and OS are you using? What's the runtime error?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Just for the record, when someone talks about occurrences they almost never mean combinations. As for how this works, it recursively builds a tree that breaks down the string, swaps elements, and recombines them to produce combinations.

When you have an algorithm you don't understand, the best approach is to either step through a small data set in a debugger or on paper and draw out what happens to the data. For example, you can get a good test run of this algorithm by typing "abc".

Don't expect someone to be around to explain everything to you, a good programmer's best weapon is the ability to disect code, trace through it, and methodically figure out how it works without any help.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

You can change the overall font size of the console, but changing it at will for different parts of text the way you can change color isn't possible. You'd need an output window that supports some form of rich text.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Daniweb is not a homework service. Please make an honest attempt to solve the problem yourself, then ask specific questions if you need help.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

It Worked.

For 1 and 3, but one of your test cases for rejecting the range suggestion was 1 and 4 being "given". This suggests that the numbers are unknown input from a user or stream. If you'll only ever be given two numbers then this approach is valid, but if you have an unknown amount of unknown numbers in an unknown range, it gets tedious very quickly. In fact, this is a manual version of my suggestion to collect the numbers in an array and randomly select from the array.

But is there any way I can do this within rand() ?

Not unless you have control over the source code of rand(), which is extremely unlikely. I say unlikely rather than impossible because it's possible to write your own version of the standard library and replace the one that came with your compiler. I do this myself, but it's not a trivial project.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Nobody is going to do your homework for you, and calling it a competition isn't going to fool the vast majority of us. Please read our rules, provide proof of effort, ask some questions, and we'll help you do it.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

You have little choice but to either collect the numbers into an array (or other collection) and then randomly select from the array, or filter out any generated numbers that aren't the ones you want.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Use the difference of the two numbers as the upper end of the range, then add the lower number:

x = lo + rand() % (hi - lo);

This assumes that lo isn't greater than hi. Also, strange things happen with negative numbers, so ideally the range would be positive.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster
#include <stdio.h>

#define MAX 5

void store(int a[], int i, int n);

int main(void)
{
    int a[MAX];
    int i;

    store(a, 0, MAX);

    /* Test the result */
    for (i = 0; i < MAX; ++i)
        printf("%d ", a[i]);
    puts("");

    return 0;
}

void store(int a[], int i, int n)
{
    if (i == n)
        return;

    do {
        fflush(stdin); /* Placeholder for a better solution */
        printf("Enter number #%d: ", i + 1);
        fflush(stdout);
    } while (scanf("%d", &a[i]) != 1);

    store(a, i + 1, n);
}
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I was referring to the code you posted

I can rename the variables to be perfectly in line with the OP if it makes my point easier to digest. :rolleyes:

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Yes, but that was not the question.

I fail to see how the question couldn't be interpreted as such. In fact, I can think of several perfectly valid interpretations. Reread the OP, this time without making assumptions.

there is no such index as a[-1]

You don't know that; the definition of a was not provided. None of the OP's statements were invalid as written.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Not in C or C++. Index values must be positive.

As long as the index refers to some location in the array's boundaries, indices may be positive or negative. For example:

int a[] = {1, 2, 3, 4, 5};
int *p = a + 2;

printf("%d\n", p[-1]); /* Perfectly legal and portable! */

Whether this is useful or not is up for debate. ;)

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Then unless you already have a strong foundation in compiler development, I'd suggest starting with something like the dragon book. You can also search google for tutorials on writing a compiler, and focus on the tokenizing/parsing parts.

But since you're doing that, I'd wager you'll also need to interpret the language at some point, and the end result will be a fairly complete compiler. Such a project is far too large for anyone to simply tell you how the code will be, by the way.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Do you just need to read it line by line, or do you need to actually tokenize and parse the verilog language?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I'm not sure I can fix that one without first changing the way we handle last post information for the article upon deletion. Well, at least not in an efficient way, and given how often we check read status, implementing it would be a very bad idea.

But if Dani decides to change the article structure in the future to maintain latest information, the read status will automatically work as expected.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Are you posting code to show others, or do you have problems with that program that you need help with? If it's the latter, please ask a question. If it's the former, let me know and I'll turn this thread into a code snippet.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

As a measure against spammers and scammers, we disallow sending PMs until you've posted enough, gained enough reputation, or been a member long enough to reach the rank of Community Member.

The requirements aren't stringent at all (15 rep points or 5 posts + 5 days), but it ensures that one can't simply join and send PMs right away.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

There seems to be some question as to whether the article read status logic is working properly, so if anybody sees a status that doesn't look right, please let me know. Excluding the one below, of course. ;)

A thread with me as the last poster is now marked as "new". Will you change that, or leave it as it is now?

Thanks for catching that, I fixed it not 5 minutes ago and hopefully Dani will publish to production within the day.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I'm no longer hip on Turbo C stuff, but if textcolor() works in any sane manner, it should set the color to whatever you chose until the next call. So for one word:

textcolor(WHITE);
printf("You chose: ");
fflush(stdout);
textcolor(RED);
printf("RED\n");
textcolor(WHITE);
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Because 999 is ultimately being broken down into 3 calls of fun(9), which when added together produce 24. The recursive tree looks like this:

fun(999)
 |
 +-> fun(99)
 |    |
 |    +-> fun(9)
 |    |    |
 |    |    +-> return 8
 |    |
 |    +->fun(9)
 |        |
 |        +-> return 8
 |     
 +-> fun(9)
      |
      +-> return 8
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I'm not sure when to use long vs int.

Use long when the value stored in the variable may exceed 16 bits. int is only guaranteed to be at least 16 bits and long is only guaranteed to be at least 32 bits. C++11 supports the long long data type, which is guaranteed to be at least 64 bits.

This goes the same for long double and double.

long double is only required to be at least as large as double.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Daniweb is not a homework service. Please provide some proof that you've attempted to write this program on your own.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

While it may be all in good fun, I don't see any reason to hide the numbers or the titles. Especially given that it's public knowledge anyway, if you're willing to search through our members to figure it all out (which would be tedious and annoying). I suspect we have someone at each tier.

So here are the post counts and titles as they stand right now:

25:    Light Poster
50:    Junior Poster in Training
100:   Junior Poster
200:   Posting Whiz in Training
300:   Posting Whiz
400:   Posting Pro in Training
500:   Posting Pro
600:   Practically a Master Poster
700:   Master Poster
800:   Practically a Posting Shark
900:   Posting Shark
1000:  Veteran Poster
1200:  Nearly a Posting Virtuoso
1500:  Posting Virtuoso
2000:  Postaholic
2200:  Nearly a Posting Maven
2500:  Posting Maven
3000:  Posting Sensei
3200:  Nearly a Senior Poster
3500:  Senior Poster
4000:  Industrious Poster
5000:  Posting Expert
6000:  Posting Genius
7000:  Posting Sage
8000:  Posting Guru
9000:  Posting Prodigy
10000: Most Valuable Poster

These numbers and the titles given at each number are subject to change at any time, which makes the list less helpful than you might think because by the time you reach the next tier everything may have changed. Also note that these are the default titles. If you choose to apply a custom title, all bets are off. ;)

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

As constructive critisicm is always welcome, nobody should be ridiculed like that.

Ridiculed? Insulted? I think you're being overly sensitive here. Anyway, I call it like I see it, and if you didn't want it seen that way then the onus is on you to post more carefully. You posted insufficient code to show proof of effort in the first place, and immediately shot back with "So how would I do this?" after being given two excellent resources. How are we to interpret this as anything but an unwillingness to think and work?

I apologize if my words are too harsh, and I'm more than happy to help you if you ask a specific question about hashmap implementation. However, you need to understand that this isn't a simple task, and nobody can tell you how to do it without essentially writing the same thing from either my first link or the book rubberman recommended.

Your question is akin to asking how one would perform brain surgery; it's not something that can be answered without a significant amount of prerequisite information that most people will be unwilling to give you when you appear as if you're too lazy to acquire it on your own. Further, even with the prerequisite information, the answer is so complex, verbose, and varied that nobody will be willing to provide detailed instructions from start to finish.

So go forth and do some research. Try things on your own. When you have a specific difficulty …

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

So how would I do this?

Start by turning on your brain, reading the provided links and references, and then doing some actual work to apply what you learn. Seriously, I'm sensing a complete lack of effort and unwillingness to think on your part. Nobody is going to do this for you or hold your hand, especially if you act like you're somehow entitled to it.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Do you know anything about hash tables? That's the first step, as a hashmap as it's usually seen is really just a pretty interface over a hash table.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

At least we're closer to completion of this feature, if the read status is only inaccurate from the homepage but works from forums and categories. I'll be doing training next week, but will definitely make some time to look into this (hopefully!) final piece.

~s.o.s~ commented: Good job! :) +0
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I think you may have posted prematurely. The referenced "following" statements aren't present.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

You don't have a prototype for gem(), you're not passing the required arguments, getch() requires conio.h to be included, and relying in implicit int for main()'s return type is poor practice give that this feature was removed in C99.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Your copy constructor doesn't do anything meaningful, which means that precedes() will always get a garbage initialized Date object. You also forgot to increment day_ in advance(), which means the loop in main() will run forever.

I find it odd that you say only "Monday" is ever printed when the code you've posted first and foremost doesn't compile, and second, clearly prints "Mysteryday" in all cases. When you fix the copy constructor bug, it's an infinite loop that prints nothing.

Is the code you posted a copy paste from your most current project?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Okay, I'll rephrase, is it ok to inline empty constructors/destructors?

At the risk of being overly concise, yes, that's fine.

Is it ok to inline small length constructors/destructors?
Is it ok to inline long length constructors/destructors?

Now you're getting into the situational aspect. The usual recommendation for inlining is to keep it to small length functions because if it's actually inlined by the compiler, every call of that function will essentially be replaced with the body of the function. This can add up in terms of code size and ultimately increase the size of your executable file.

a general question such as "Is it good to inline constructors/destructors?" can be answered with examples that illustrate when inlining is usefull and when not

I don't think inlining is useful in practice. I never use the inline keyword explicitly and would prefer an equivalent noinline hint for functions defined inside the class definition. Pity it's not available in standard C++.

The topic you linked to mentioned output profiles, is there no other way?

The point of inlining is performance, right? So it's reasonable to assume that the only way to determine when inlining is viable is to measure performance. As the article also mentioned, programmers are notoriously bad at guessing where inlining could be beneficial.

Also it doesn't say anything about constructors/destructors (yes they are usual methods, however a bit more special).

That's largely irrelevant. The only consideration with constructors and …

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I'll start from the top to avoid confusion.

tell me why only 'abcd' is printed and not 'abc'?

Because abcd is a global object and abc is a local object. Due to C's initialization rules, global objects are default initialized to the type's equivalent of zero, which in the case of a pointer is comparable to NULL.

This initialization rule doesn't apply to local objects, and those will contain whatever value was already in that location in memory, which may be invalid in any number of ways, especially for a pointer.

A good practice is to initialize everything explicitly:

struct node *abcd = NULL;

...

int main()
{
    struct node *abc = NULL;

    ...
}

However, you can get away with not initializing global objects (or objects with static duration in general) if the desired initialization value is the type's equivalent of zero:

struct node *abcd; // Default initialized to null

...

int main()
{
    struct node *abc = NULL; // Still needs explicit initialization

    ...
}
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

In all honesty, you'd be better off looking for a job in IT and then transitioning into development. That's what I did, and the IT experience has proven invaluable as a developer.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

so, how do I put it into my current working directory

I'd do it with trial and error since there aren't many options, but an excellent start is the same directory as the executable file. Was the rogue semicolon not present in your actual code?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

You can get more information by printing the error message with perror():

if ((fp = fopen("new.txt", "r")) == NULL) {
    perror("Error opening new.txt");

    ...
}

But I notice that you have a rogue semicolon after the condition of your if statement, which means that the printf and return will always execute. If you copy pasted your exact code then that's the problem. Otherwise, a common issue is that the file isn't in your current working directory, which is where fopen() looks.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

except instead of "redirect stdin", it should read "redirect stdout"... :-)

I have an excuse for that brain fart this time. I spent a large portion of the afternoon working on the input half of a standard C library implementation, so I had stdin on the brain. Give it a few days and I'll type stdout when I mean stdin. ;)

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Open a file stream using fopen(), then call fprintf() instead of printf(). Alternatively, you could redirect stdin into a file, but I don't get the impression that this is what you want.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

it will hardly take 10 min to you to get the concept

I think you overestimate my ability. I find this data structure neither interesting nor important to learn to the point where I'm qualified to teach about it, so I have no intention of spending my free time doing so. Sorry.

I_m_rude commented: :'( +0
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Here is another link. I can't really help you too much as I'm not familiar with this data structure. It seems fairly specialized.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Did you read the referenced tutorial as well?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

This was the first hit on google.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Like that opening apostrophe on the key with the tilde. Nobody uses that....

We already use that for inline code.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I think as programmers we have a tendency to read our own code with a mental filter and see what it should do as opposed to what it actually does. It's kind of hard to objectively read one's own code, that's why pair programming and code reviews are so effective.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

but what actually happens internally while compiling it.?

I don't know, I've never looked into that. But I do know about the PE (Portable Executable) format that Windows uses for .EXE files, and it has a resources section. I'd be shocked if that's not where the icon is stored.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

But for text passages cannot it be verbatim, slash for slash, * for *, a for apple?

It could, yes. But then how would you propose we support bold and italic, or the other formatting features using a character that would need to be escaped? Before we had to do this:

[b]this is bold text[/b]

And now we do this:

*this is bold text*

In exactly the same way the noparse tags from BBCode were necessary to properly display [b], an escape is necessary to turn off formatting in Markdown. Somehow I suspect that disallowing any formatting whatsoever outside of code tags, or making the formatting language so awkward that nobody would use it accidentally would go over badly compared to requiring some form of escape for our formatting language.

I don't live coding.

It's a fairly simple matter to learn the rules of Markdown, you don't need to be a programmer at all.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Did you not read the link? It's highly situational, so one can't simply say that inline functions are good for constructors/destructors or not in a general sense. I'd personally avoid inlining all but the simplest constructors and destructors, but for the sake of simplicity I've been known to define member functions inside of the class body, which makes them implicitly inline.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Ignoring the questionable use of goto here, you never initialize or update the value of a, yet that value is assigned to j when a number is not prime. That raises red flags. If the purpose of this algorithm is to print the primality of every number from [1,N] then there's really no point in updating j at all.

So let's try this a different way. Use a simple boolean flag, initialized to true, and set it to false if the primality check fails. Then after that check, test the flag and display success if it's still true:

#include <iostream>
using namespace std;

int main ()
{
    int N;
loop:
    cout << "Please enter a positive integer or 0 to quit: ";
    cin >> N;
    if (N==0) return 0;
    else {
        while (N<1)
        {
            cout << "Please enter a positive integer or 0 to quit: ";
            cin >> N;
            if (N==0) return 0;
        }
        for (int j=1;j<=N;j++)
        {
            if (j==1||j==2) {cout << j << " is prime."  << endl; cout << "\n";}
            else
            {
                bool isprime = true;

                for (int i=2;i<j;i++)
                {
                    if (j%i==0)
                    {
                        cout << j << " is not prime.\nIt is divisible by " << i << "." << endl;
                        cout << "\n";
                        isprime = false;
                        break;
                    }
                }

                if (isprime) {cout << j << " is prime." << endl; cout << "\n";}
            }
        }
        goto loop;
    }
    return 0;
}
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

What compiler, OS, and IDE are you using? The simple answer is that you need to add an icon resource to your project and include that resource in the build. But that doesn't answer your question of how to do it.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

cannot convert parameter 1 from 'char *' to 'LPCWSTR'

LPCWSTR is a wide character type. Make sure you don't have Unicode enabled anywhere in your project or code if you don't intend to use the wide character functions.

unsafe operation: no value of type 'bool' promoted to type 'SOCKET'

This one is pretty obvious; you're comparing a boolean to a SOCKET. Could you post the exact line this happens on? It's certainly not line 190.

warning C4996

I'm not the type to recommend disabling warnings, but this one is frivolous and stupid.