deceptikon 1,790 Code Sniper Team Colleague Featured Poster

The problem here is the function ExecuteAddtion is not passing the result to the main class variable sum.

No, the problem is that you're expecting the two completely separate objects to magically communicate just because their types are related. It's no different from creating two integers, incrementing one, and expecting the other to be incremented too even though you did nothing with it.

WaltP commented: Quantum programming? It's real, isn't it? +14
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

The addition I beleive is executed but the sum result was not pass in Addition class sum variable.

Yup.

Even if Addition class is inherited as public?

I think inheritance might be confusing things, but you can see what's going on by removing that variable entirely. What happens if you use two Addition objects instead?

Addition Addme;
Addition Addme2;
int main()
{
    cout<<"Enter first number: ";
    cin>>Addme.num1;
    cout<<"Enter second number: ";
    cin>>Addme.num2;
    Addme2.ExecuteAddtion();
    cout<<"The sum of "<<Addme.num1<<" and "<<Addme.num2<<" is "<<Addme.sum<<endl;
    system("pause");
    return 0;
}
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Why do you think modifying SubtractMe's copy of sum should affect AddMe's copy of sum?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

but why we don't use printf()?

Allow me to make an enlightening comparison from my own stdio library. The back end to putchar() is fputc(), which totals in at 22 lines of code. fputs() is built on top of fputc() and is all of 6 lines.

The guts to printf() are pushing 1,000 lines of code.

print() is a very heavy function that does a lot of work. If all you need to do is print a string or character directly, there are simpler functions that do this without any extra rigmarole.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

what is size_t in this??

size_t is a typedef representing the type returned by the sizeof operator. It's very often used as the type for array indices because the maximum value of size_t will never be less than the potential number of indices in any array.

can't we take it as int because we are just passing the size which is an integer??

Yes, you can use int instead.

and could you please explain this part too??

When you type a backspace character, both the last actual character stored in the string and the last displayed asterisk need to be removed. fputs("\b \b", stdout) will handle the latter by first printing a backspace (which isn't destructive, it only moves the cursor), then one space character to overwrite the character being removed, and finally another backspace to place the cursor in the correct position.

Removing the last character from the string is even easier because all you need to do is decrement the index and the character will be overwritten on the next iteration. But there's one edge case where the index is already 0, and you don't want it to go negative or bad things happen.

These two tasks work together to keep both the password string and the display in sync when you type a backspace.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Since you've sidestepped the command shell's conveniencies like being able to edit your input before sending it to the program, that functionality needs to be duplicated within your program. In this case that means recognizing and handling the any special characters that the shell normally handles for you:

#include <stdio.h>
#include <conio.h>

char *get_password(char *buf, size_t n)
{
    int done = 0;
    size_t i = 0;

    while (!done && i < n - 1) {
        int ch = getch();

        switch (ch) {
        case '\r':
            /* Convert CRLF to a newline */
            putchar('\n');
            buf[i++] = '\n';
            done = 1;
            break;
        case '\b':
            /* Roll back a previous character */
            fputs("\b \b", stdout);
            fflush(stdout);

            /* Don't underrun the buffer */
            if (i > 0)
                --i;

            break;
        default:
            putchar('*');
            buf[i++] = (char)ch;
            break;
        }
    }

    buf[i] = '\0';

    return buf;
}

int main(void)
{
    char buf[BUFSIZ] = {0};

    fputs(get_password(buf, sizeof buf), stdout);

    return 0;
}
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

time_t is the number of seconds since 1 Jan 1970 to the specified date.

While it is indeed the canonical epoch, there's no guarantee that time_t represent the number of seconds since 1/1/1970. For example, I very seriously considered using 1/1/1900 as the epoch for my time.h library, to the point where it was functional, before changing my mind and conforming to convention.

Anyway, a good technique for setting up the tm structure when you only care about the time portion is to initialize it to the current day:

time_t t = time(0);       // Get the current datetime as time_t
tm *info = localtime(&t); // Break it down into a local calendar object

// Update desired fields
info->tm_hour = new_hour;
info->tm_min = new_min;

// mktime normalizes the calendar object
if (mktime(info) != (time_t)-1) {
    // info is now safe to use
}
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

30 minutes, IIRC.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Take a look at the ctime header. It includes types and functions for manipulating dates and times. With minimal effort you can parse your time string into a tm structure, convert those into time_t objects with mktime(), and finally get the difference with difftime().

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

My understanding of the C language is that the same identifier cannot be reused.

That's correct, but only within the same name space (not the same as a C++ namespace). There are five listed name spaces in C:

  1. Label names (such as cases and goto labels).
  2. Structure, union, or enum tags.
  3. Structure or union members.
  4. Typedef names;
  5. Everything else.

With those name spaces in mind, it's easy to understand why the following is legal:

typedef struct foo { int foo; } foo;

In your code, the typedef error and the macro error are in different name spaces (the typedef falls under #4 and the macro under #5), so there's no direct conflict.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

zeroliken you are wrong
Its not the right answer

Then what do you think is the right answer? The sequence you showed is clearly 5^n at every step. If the 5th step isn't 3125 (5^5) then either you didn't provide sufficient information about the sequence to reach the next number, or you don't realize that the answer is 3125. ;)

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

strrev() isn't a standard function, so compilers aren't obligated to support it. What compiler are you using?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

i am submitting there. the site is giving me time limit exceeded.

Okay, that's completely different. Obviously if you're writing to cout over 10,000 times then it's going to take a while. That's the problem, and the root cause is that your code is too slow due to the overhead of output.

my code complexity is O(1).

No, your code complexity is O(t).

so i tried it on ideone which gives me run time error.

It's not unreasonable to assume that Ideone has stringent limits on things like this, so I'm not surprised at all that you got a SIGXFSZ error. Use a real compiler on your local machine and that error will vanish.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Are you redirecting output to a file? SIGXFSZ is the signal raised when a file exceeds its size limitation.

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

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

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'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

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

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

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

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

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

if(root==NULL)

has no meaning at all...

It has meaning, it's just a pointless meaning. ;)

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

i use Dev- C++ and was thinking to use CreateDirectory() and CopyFile() but failed!!

I'm not surprised. It's not just a matter of copying the executable and such, you also need all of the dependent libraries that may not be on the target system. That's why I recommend that you use Windows Installer, because that's the correct way to create an installation package on Windows.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I imagine it's not printing because root is not NULL. Change your if statement's test to

if (root != NULL) cout<<root->info;
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Windows has its own installer program that consumes MSI/MSP files, so you need to create one of those files for your program. I think the easiest way to go about that would be a tool like Inno Setup, but you can also use a setup project in, for example, Visual Studio (not the Express versions though).

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Is it possible to add a new category? Yes or No?

If YES we dicussed about it or NO I will click Solved and more on.

Yes, it's possible. Either Dani or I would have to do it on the code/database side, but it's certainly possible. Whether adding categories when Daniweb is already fragmented (some would say overly fragmented) is another matter.

In the past we've gone with the petition route. If you can find enough people to petition for the addition of a new forum or category, we can consider it. The problem is that we don't want a lot of forums with low activity, so there needs to be sufficient demand before the addition can be justified.

Another thing to consider is that the tagging system is the intended future. With the design of the new system we were writing it with an eye toward making tags more of a key player (somewhat similar to Stack Overflow) than the current forum hierarchy.

LastMitch commented: Thanks for answering my question! +0
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Please don't put words in my mouth meaning qoute what I post not make up something!

To be fair, your posts suggest a non-native English speaker and I've found them slightly difficult to decipher. It's not unreasonable to see misunderstanding your posts when not everyone speaks English at the same level or with the same background, so a little tolerance couldn't hurt.

In other words, please don't be so quick to take offense.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

If none of the sub forums are quite right for your question, you can post to the category (ie. Databases) and tag the thread accordingly.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Yes, there's certainly a way. What OS are you targeting?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I want to get output which same as the input.

Then use strings. Floating-point is inherently imprecise due to the limitations of the machine and gains or losses from rounding. It's unreasonable to expect the internal representation of a value to be exactly what the user typed for input, especially given that printf() does its own rounding when converting that value back to a string. In this specific case you can normalize the input and output by forcing a precision to the input:

printf("%.2f\n", n);

But that doesn't help if the input changes to use 3 characters past the radix, you'd see the same problem in reverse.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Dude...start back at the hello world program, because you very clearly aren't getting it. You need to take a few steps back and start learning the basics again.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Semicolon. And you've been asked before to state what the error is. If you continue to post vague requests for help, I'll stop trying to help you.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

If you can't think of a program with all of those constructs, maybe you shouldn't be using them all at once yet? ;)

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Perhaps someone can describe this better!

C++ performs name mangling to enable the overloading feature. If you declared the entities with C linkage (by compiling them as C) and tried to link with the same entities that have C++ linkage (by compiling them as C++), there won't be a match due to mangling. So while the names would be declared, and appear to be defined in the source code, they wouldn't actually be defined.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Because I couldn't install netbeans at home so I couldn't try.

So NetBeans is the only C++ implementation? I see you tried sooo hard. :P

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

but when i asked you the question abonu changing the constant through the pointer, then you said that is undefined.

Undefined means the C standard places no restrictions on behavior. This is obviously quite different from disallowed, where the standard says clearly that you aren't allowed to do something. The former means anything could happen and you need not be warned in any way, while the latter will result in a diagnostic message.

So this means i can't do that thing as it is undefined totally. So it's not correct to do that.

It's not correct, but you can still do it.

int a=10;

is it compile time constant ? how ?

10 is a compile time constant, a is not.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Undefined reference means you've declared a name but not provided a definition anywhere. This usually happens when using a library's header without linking to the library.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Constant means it cannot be changed, but you've already learned how to change a const qualified value through a pointer, right?

As pertains to this issue, compile time constant basically means that the constant can be used as the size of an array or as a case label in a switch statement. If you can't do either of those two things, it's not a compile time constant.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

As far as goes, it's unnecessary to micromanage like that. However, I think that given the awkwardness of using iterators here, subscripting is cleaner anyway. Just make sure to check boundaries of both vectors.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

in this, do u mean b is a compile time constant and a is not ?

It is not a compile time constant.

but sir, what const has created the difference ?

The const keyword gives the compiler the chance to warn you if you accidentally try to modify the value of a variable that shouldn't be modified. It's a convenience to you, the programmer, in writing robust code.

I see that you're confused, so for now just remember that const isn't really constant, it's just read-only.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Does it still show as 1 when you empty both your outbox and inbox? That should reset the count as well as remove any orphaned messages.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

what exaclty u meant by compile time constant ?

A compile time constant in C is essentially a literal value. 123, 5.6, and "test" are all compile time constants when placed in code, but any variable (even a const qualified variable) is not.

secondly, again i want to ask will you explain further when memory is allocated for variables ?

The times differ depending on where the variable is declared and how memory is being allocated. You can typically divide things into three different times:

  1. Static data resides in the executable and is allocated when the process is loaded into memory.

  2. Local variables are stored on the stack. The stack itself is allocated in toto when the process is loaded, but individual variables are doled out from the stack as they're reached during execution. There are details where some compilers differ, as Ancient Dragon mentioned, but you really don't need to concern yourself with that just yet.

  3. Dynamic data, such as that returned by malloc(), will be allocated on request.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

can anyone tell me whether we can use const variables for case ...

No, case labels must be a compile time constant. In C the const keyword doesn't make the variable truly constant, it just makes it read-only under normal circumstances. Turbo C might offer a extention to support non-constant labels, but you can't rely on that outside of Turbo C.

likewise if we use this const variable as array index during initialisation...would it work...

No. This works in C++ because C++ changed the semantics of const, but not C.

can anyone explain ...at what time memory is allocated for variables...

It depends on where you declare the variable, but for the most part variables are stored on the stack at runtime.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

also did the program compiled without including the ctype and string libraries necessary for using tolower and strlen functions

It's not good practice, but that's allowed except when the function being called uses variable parameters (like printf() or scanf()). If you're calling a variable parameter function then a declaration must be in scope or you invoke undefined behavior.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

error is at line 23 can i use ascii value of vowels????

Two things:

  1. POST THE ERROR! You should make it as easy as possible to help you, and forcing us to either be a human compiler or copypasta your code into a compiler to see what the hell you're talking about is encouraging people to not help.

  2. Read your own errors. If you had taken the time to read the error and study your own code, you wouldn't have needed to ask for help. This is a very obvious error and a very common bug among beginners and experienced C programmers alike. You're using = instead of == for comparison.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

the compiler say the error is at here at 18th line if(ch== '')

There's no such thing as an empty character literal, the compiler is telling you that. I can only speculate about what you were trying to do, which is check for the end of a string? In that case you test for '\0'.