deceptikon 1,790 Code Sniper Team Colleague Featured Poster

i have given with only structure and hex data, i ve to printf corresponding values of struct variables in decimal,, i am new to c kindly help.. its my assisgnment...

I basically gave you the solution; all you have to do is mold it to suit your specific requirements. If you're not willing to even attempt doing that then I'm not willing to help you any further.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Anyway, I just spent 10 minutes painting DW a manly Green

Maybe a lil' darker. The contrast isn't quite high enough with the white and gray (especially the gray) to really hit home as a good color scheme.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Purple is for girls.

  1. The owner is a girl.
  2. Men who say "XYZ is for girls" just need more confidence in their masculinity. ;)
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Yeah,but here supplying only integers(not unevaluated arithmetic expressions) will help.

Expecting someone, even yourself, to use a macro correctly is overly optimistic. The macro should be as dummy-proof as possible

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I discarded my crystal ball years ago. Sorry.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Or, just simply call localtime() or gmtime(), which will put it in a structure for you.

But, but that would defeat the purpose of reinventing (poorly) the behavior that someone worked very hard to implement in the standard library! ;p

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

You're probably looking for a variation of atoi(). Read a byte, then multiply a running total by the base and add the byte's digit value. Since your digits all appear to be hexadecimal, the base is 16 and some unchecked code for a 4 byte integer might look like this:

#include "ctype.h"
#include "stdio.h"
#include "string.h"

/*
    @description:
        Converts a character into its corresponding decimal 
        value according to the specified numeric base.
*/
int digitvalue(int c, int base)
{
    char *p = "0123456789abcdefghijklmnopqrstuvwxyz";
    char *q = strchr(p, tolower(c));

    if (!q || (q - p) >= base)
        return -1;

    return (int)(q - p);
}

int main(void)
{
    const char *s = "dfca";
    const char *p = s;
    int x = 0;

    while (*p)
        x = 16 * x + digitvalue(*p++, 16);

    printf("The value of '%s' is %d\n", s, x);

    return 0;
}

Now expand that into something that reads from a stream instead of a string, does it multiple times, and works with structure members instead of a temporary variable.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

But when I enter "45:45" I get 45,4. The last character is missing.

Read all of my reply next time, I predicted this problem and explained and how to fix it. Bump up the size of your array by 1, and account for that new size in your call to getline(). Adding '\0' as the third argument to getline() is silly and pointless because you'll never recieve that character value in any reasonable situation. It also does nothing to deal with the actual problem which is insufficient storage in the string to hold all of the input.

And also, fflush(stdin); cin.get() is not working.

fflush(stdin) isn't guaranteed to work. fflush() is only defined for output streams. But that's not the problem, the problem stems from the fact that you're trying to overfill your input string. getline() is smart enough to recognize that this is bad and puts cin into an error state, which you can see by adding one line before fflush():

cout << boolalpha << cin.fail() << '\n';
fflush(stdin);
cin.get();

You'll notice that it prints "true" because cin is in a failure state. Any requests for input either through fflush() (probably, I'm making an assumption about how your compiler's standard library works) and certainly get() will do nothing. The effect is like get() is implemented as so:

int istream::get()
{
    if (!good())
        return;

    // Do actual work...
}

If you fix the underlying failure, you'll fix the get() too. And if …

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

strtok() is funky in that it works using an internal state through multiple calls. To get the next token you need to call strtok() again with a null first argument (so that it knows to use the previously passed string):

output=strtok(input,":");
while(output != NULL)
{
    cout<<output<<endl;
    output=strtok(NULL,":");
}

Oh, and your input string and getline() call need to account for that pesky little '\0' at the end. Otherwise your last token ("CD") will be trimmed by a character because it's still stuck in the input stream.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Awesome! So i can display any error statement like this??

It depends on what you mean by "this". Note that what I showed you is the internal setup of perror() on my standard library implementation. You can't simply toss my __errno_map into your code and expect perror() to change how it works on the compiler you're using.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

If yes, then will you please tell why is it so ?

The tricks one uses to win a contest are nifty, but would fail any code review for production quality software. It's just like the tricks used in the IOCCC, they're only useful for the contest.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

in other words, You are genius.

Absolutely not. I make up for lack of talent and average intelligence with heaps of effort.

haven't you done codechef, spoj like online judges in your school or university time ?

I'm not a fan of code competitions. Not only are the takeaway skills largely useless as a professional programmer, they highlight my complete lack of ability to devise clever and obtuse solutions that fit within the draconian performance and space requirements. ;)

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

how do you come to know about these types of links which you have given above ?

Programming is my hobby as well as my job, so I do a lot of reading on the subject. It should go without saying that I'll encounter interesting texts and web pages every now and again. ;)

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

why the compiler need to search those header files which are not included in the main.c ?

Because there might be a definition of the variable or function in one of those other files. The headers that are included are already part of the translation unit, so the compiler can see them. Note that a translation unit is basically what the compiler works with after the preprocessor runs.

Can you explain your hard case again ?

I'm guessing you haven't worked with a command line compiler before. ;) Modern IDEs make multi-file projects easier to work with and build, but it can hide the nitty gritty aspects of building such as separate compilation.

C supports separate compilation in that you can compile some source files into an object file. That object file can then be used by the linker at any time without requiring you to recompile the original source files (unless they change).

The compiler and linker often run in the same invokation of a "compiler" program like gcc, or tcc (Turbo C's compiler), or bcc32 (Borland C++'s compiler), or cl (Microsoft's compiler). So you can pass in both source files for compilation and object files for linking all in the same command line (ie. gcc main.c impl.o).

And therein lies the problem, because the compiler can't see what's in those object files even though they might contain exactly the definitions that would suppress an undefined object or function error.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Is this the list you're talking about??

For Microsoft's perror(), yes. Other implementations of the standard library may have different values of errno that are supported and different messages. For example, my implementation presently maps errno values like this:

extern struct _errno_mapping __errno_map[] = {
    {EDOM,     "EDOM",     "Domain error"},
    {EILSEQ,   "EILSEQ",   "Encoding error"},
    {ERANGE,   "ERANGE",   "Range error"},
    {EINVAL,   "EINVAL",   "Invalid argument"},
    {ENOENT,   "ENOENT",   "No such file or directory"},
    {ENOMEM,   "ENOMEM",   "Not enough memory"},
    {EIO,      "EIO",      "I/O error"},
    {ENFILE,   "ENFILE",   "Too many open files"},
    {EACCESS,  "EACCESS",  "Permission denied"},
    {EUNKNOWN, "EUNKNOWN", "Unknown error"},
    {ESETP,    "ESETP",    "Error setting file position"},
    {EGETP,    "EGETP",    "Error getting file position"},
    {ESFMT,    "ESFMT",    "Unexpected string format"},
    {-1,       "",         ""} /* Sentinel record with an invalid errno value */
};

Where the sentinel implicitly gets converted to "Unrecognized error" in strerror()'s implementation:

/*
    @description: 
        Maps the number in errnum to a message string.
*/
char *strerror(int errnum)
{
    size_t i;

    /* Simple case for no error */
    if (errnum == 0)
        return "No error";

    /* Look for a matching errno code */
    for (i = 0; __errno_map[i].code >= 0; ++i) {
        if (__errno_map[i].code == errnum)
            return (char*)__errno_map[i].msg;
    }

    return "Unrecognized error"; /* Give up ;) */
}
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

How about a lazy update of the cells? It really doesn't matter what the contents of a cell are until you access it for the latest value. Unless the value of a cell depends on all previous cells, you can apply the update as needed rather than en masse, and that would save a huge amount of time.

The only caveat would be an extra bit of storage necessary to flag whether the cell has its latest value or needs to be updated.

If the values do cascade somehow, you'd probably still get some performance benefits, though it would be more amortized than absolute. In that case something like r0shi's idea would work, where you apply the change to all cells dependent on the current cell retroactively or recursively depending on how difficult the cascade is to reverse.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Changing c to a double would be my bet!

That's not a bad notion, but the problem isn't in precision of the type, it's in cout's behavior. cout acts much like printf() when printing floating point in that the default precision is 6, and if the fractional part is all zeros it's simply ignored. Since the first nonzero digit is the 7th in the value's precision, it gets truncated along with the zeros whether it's a rounding error or not.

I'm curious what the expected answer is, actually, since I still can't think of a good way to override count's default behavior without changing the value or the call to cout.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

The output shows "No error"

Probably because there's no error. perror() writes the value of errno in human readable form, along with an optional user provided message. It can literally be implemented like this:

/*
    @description:
        Maps the error number in errno to an error message.
*/
void perror(const char *s)
{
    if (s && *s) {
        fputs(s, stdout);
        fputs(": ", stdout);
    }

    puts(strerror(errno));
}

There are only a handful of places in the standard library that are required to set errno to something specific (mostly math functions and multibyte en/decoding); everywhere else is optional and implementation defined. Neither printf() nor scanf() are included in those handful of places.

You're not using perror() incorrectly, as Walt mentioned, but for an application defined error such as exceeding MAX_VAL, you're on your own for setting errno:

if (x > MAX_VAL) {
    errno = ERANGE;
    perror(NULL);
}

So, which function shall i use?? for c++ we can use cin.get(); but what to use for C??

The equivalent of cin.get() in C is getchar().

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

There's another way to set the flags, or you can use another output method, but that's essentially the normal way to override cout's convenience behavior. I can't think of any clever way off the top of my head that isn't too awkward to be reasonable.

Any thoughts?

If it was on a test then you would have learned it in class or through homework. Though I can't help much unless you provide the exact question rather than paraphrasing it.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster
cout << fixed << setprecision(7) << space << endl;

Don't forget to include <iomanip>, and take care with expectations when it comes to floating point arithmetic. The results are often approximations and can suffer from accumulated errors.

stengeljjj commented: This works to fix issue +0
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I'm willing to bet this an encrypted message that students have been given to decrypt as homework.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

If it's an extension of that thread, post in that thread. If it's a new question, ask an actual question.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I think you've been here long enough to realize that posting code without any kind of specific question will just piss people off, or at best get you ignored.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

If at all there is a differnce between their execution times in the range of nanoseconds or picoseconds?

Execution is measured in "waste of programmer time" intervals that increase in direct proportion to the triviality of the statement.

Ancient Dragon commented: LOL +14
iamthwee commented: ha ha ha... ha +14
WaltP commented: Love it!! +14
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

dfca2345f1278aba11110012567ade21

How do you plan to differentiate between the 5 separate values (according to your structure) in that line?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Your code runs fine on my end (with minimal tweaks to make it compile as C90). What compiler are you using, what output are you getting (paste it), and what output do you expect (paste it too)?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

i would like to know is there any advantange or necessity for having loops in a linked lists.

Well, a circular linked list is itself a loop, and they're quite useful. Internal cycles strike me more as a bug or faulty logic (hence the need for detection algorithms), but there may be niche uses that I'm not aware of.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Here's a hint for asking questions: if reproduction of your problem depends on the content of a file, it's a good idea to post a small part of that content along with your code. This way we aren't forced to imagine what your file looks like and devise test cases in the hope that we'll hit your problem. Most helpers will just pass this question by because it implies too much work for what promises to be a very simple issue.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

And when i have declared a variable but didn't define it, then linker will give error, why not compiler ? isn't compiler don't know that this variable is not defined yet ?

As Walt said, the compiler doesn't know. It doesn't know because all it's compiling is a single translation unit, but the project could contain many files and many translation units, all in various stages of building. Here are two examples (of many) that make multi-file introspection a non-trivial and inefficient task:

  1. Simple Case ($ cc main.c impl.c): main.c declares foo, and foo is defined in impl.c. While compiling main.c the compiler would need to search through all other source and header files not included in main.c for the definition of foo before it could accurately produce a diagnostic message.

  2. Hard Case ($ cc main.c impl.o): main.c declares foo, and foo is defined in impl.c. But impl.c has already been compiled into impl.o, and the compiler doesn't have access to impl.c. Now it has to do part of the linker's job and search through impl.o for exported definitions of foo before it can correctly report undefined objects.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

"Best" is subjective and dependent on the needs of the situation, but a concise and reasonable generic solution is this one:

int count_set_bits(int value)
{
    int n = 0;

    while (value) {
        value &= value - 1;
        ++n;
    }

    return n;
}

The benefit of this algorithm over the more naive loop is that this one only iterates as many times as there are set bits. The naive loop would loop as many times as there are bits in the type and include an extra test for a set bit:

int count_set_bits(int value)
{
    int n = 0;
    size_t i;

    for (i = 0; i < sizeof value * CHAR_BIT; ++i) {
        if (value & 1)
            ++n;

        value >>= 1;
    }

    return n;
}

Further options make increasingly more assumptions about the machine, but they can be very quick relative to the loop options because there aren't any conditionals or jumps.

This is a page you might find informative for bit magic.

I_m_rude commented: very nice explained! ;) +2
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

realloc can do that but STL vector does it support ?

std::vector has historically not shrunk memory when the size is reduced to improve the performance of furtuer insertions. The idiom from before C++11 was using the swap() member function:

vector<T>(a).swap(a);

But with the coming of C++11, std::vector now has a (albeit non-binding, so the request may be ignored) shrink_to_fit() member function.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

What version of Chrome? This might be a stupid question, but did you make sure cookies are enabled?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Sorry, missed the first method and zeroed in just on the pragma...

To be fair, pragmas are evil. Sometimes necessary, but always evil. ;)

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

but when i don't declare a variable then compilation error is there.

If you use an undeclared variable, the compiler will complain because it has no idea what you're referring to. If you declare a variable but don't define it, the linker complains because what you're referring to doesn't exist.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Undefined objects or functions will be reported at link time. You might also get a warning during compilation, but link time is the hard stop point.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

james sir, Is there any code snippet that you created on the link which you have given above ?

I've not contributed any snippets yet. But if you're interested in snippets by an individual, you can navigate to their profile page. Underneath the contributions section you'll find a link to all of the member's code snippets (if they've submitted any).

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

And so you trap any reference to a CLSID....

Sadly, yes.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

It's possible to bubble sort a linked list, but it's kind of dumb given the overhead of indexing a linked list on top of the weakness of bubble sort.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I don't use VC at this time but doesn't VC offer a way to disable errors from the project/IDE settings?

In my previous post I described both a project settings method and a direct code method for disabling warnings.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

so linked list always prints number in reverse order???

This list does because new nodes are prepended rather than appended. With some extra work you can walk to the end of the list and append new nodes there, then the list will be in insertion order rather than reverse insertion order.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I'd wager it's a typo for C++.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I was thinking that I'm creating a header..

Well, you are creating a header. It's just that a "header" in C++ doesn't really mean very much. It's just a file with C++ code (usually declarations) that can be easily and conveniently reused with the #include preprocessor directive.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

But that doesn't happen with other header files isn't it??

It has nothing to do with header files, the rules of C++ don't change just because you break your program down into different files. In fact, headers are nothing more than a glorified cut and paste. When you say #include "sum.h", the entire contents of the sum.h header file are textually pasted over that directive. So this:

#include<iostream>
#include "sum.h"
using namespace std;
int main()
{
    sum(5,4);
    displaysum();
    cin.get();
}

Becomes this:

// The entire contents of iostream go here
// The entire contents of sum.h go here
using namespace std;
int main()
{
    sum(5,4);
    displaysum();
    cin.get();
}

We directly use the functions?

You can directly use a member function without qualification inside the class definition because this is implied. So this:

class Foo {
    void Bar() { ... }
    void Baz()
    {
        Bar();
    }
};

Is equivalent to this:

class Foo {
    void Bar() { ... }
    void Baz()
    {
        this->Bar();
    }
};

Outside of a class definition, no such implicit object exists and the compiler will assume that an unqualified function call refers to a non-member function.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

help me .. people out there...

I already gave you a direction for troubleshooting. Were you expecting us to do it for you and then tell you exactly what to fix? Or even fix it for you and give you the working code? :rolleyes:

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

is there any alternative to it?

Well, you could read the message and see that strcpy_s() is provided as a "safe" alternative. strcpy_s() is a part of the C11 standard (it's not standard at all in C++), but C11 was only ratified in January of this year, so there aren't any solid implementations of it yet.

But Microsoft bugged us with that stupid warning long before the bounds checked functions were included in C11, and C11 doesn't deprecate the non-range checked functions, so it doesn't excuse the warning at all. I always disable it from the project settings (Configuration Properties -> C/C++ -> Advanced -> Disable Specific Warnings). You can also disable it directly from code with this pragma:

#pragma warning(disable:4996)

But that hurts your code's portability.

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

And?

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

I'm sensing a confusion with pointers and aliasing. Have you played with Binky yet?