Narue 5,707 Bad Cop Team Colleague

I think this has something to do with the fact that I'm returning a const reference to an object that this function is treating as const.

I think you're smoking crack if you made that connection with the given error. The problem is your syntax in calling size() and at(). Tell me, what is symbol_data.vector ?

Narue 5,707 Bad Cop Team Colleague

>#include <cassert>//what is the use of this???
In that program it's not used. But <cassert> defines the assert macro, which is used for sanity checks:

void foo(int *p)
{
    // Precondition: p is not a null pointer
    assert(p != 0);

    ....
}

Your questions boil down to "How do I convert standard C++ to something that will compile under a pre-standard compiler?". The answer is you don't, it's not worth the effort. Get a newer compiler.

Salem commented: +1 +17
Narue 5,707 Bad Cop Team Colleague

This isn't directly relevant, but have you tried not using Turbo C? It has limitations that will be terribly frustrating on modern hardware.

Salem commented: Well said +17
Narue 5,707 Bad Cop Team Colleague

Yep, I outsmarted myself, and I'll blame Monday. ;) Changed the cast from void* to char*.

Narue 5,707 Bad Cop Team Colleague

Presumably you're using a vector of std::string. The fstream constructors don't have an overload for std::string, so you need to call c_str():

ifstream file(files[0].c_str());
Narue 5,707 Bad Cop Team Colleague

I see you didn't read our rules or the stickies. Specifically, we don't give homework help without some evidence that you've made an honest attempt to do it yourself.

Narue 5,707 Bad Cop Team Colleague

I suspect what you want are property attributes that affect how the property is displayed in a property grid control. If your class is more than just a design-time component, you can also make use of the PropertyGrid control directly in your own applications.

vedro-compota commented: +++ +3
Narue 5,707 Bad Cop Team Colleague

It would be in limits.h if there are any.

Actually, there's a separate float.h for that information because there's more to it than just min/max (such as the radix and epsilon values).

Narue 5,707 Bad Cop Team Colleague

The %d specifier prints a signed int. If you want to print an unsigned value, use %u.

Also, you'd be better off using INT_MIN, INT_MAX, and UINT_MAX from limits.h. That'll protect you from the off-by-one trap that you fell into with 4294967296.

Narue 5,707 Bad Cop Team Colleague

I think the node structure should be like this:

struct node {
    void *data;
    int type;
    struct node *next;
    struct node *prev;

}LINK;

where: type is used for type. For example type=1 indicates int, type=2 indicates float, type=3 indicates userdefine and so on...

and Insert function will look like this:

int inser(LINK **head, void data, int type) {
   if(type==1) { // int
      LINK *newnode = (LINK*)malloc(sizeof(LINK));
      newnode->data = malloc(sizeof(int));

   }
and so on......
}

Um, no. All you're doing is limiting the number of types that the linked list can store. By using pointers to void, you guarantee that it can store any object type past, present, and future. This does place the burden on calling code of making sure that either

  1. All pointed to objects have the same type (a homogeneous list) or
  2. All pointed to objects have some way of exposing their type (a heterogeneous list)

The latter is what your idea does, but in a limited way. The list of supported types should be managed not by the linked list library, but by the application. This ensures greater re-usability of the library. Since the list contains pointers to void, the application can store pointers to an application-specific structure containing supported type information:

struct typed_data {
    enum supported_types type;
    void *value;
};

Your idea precludes that option as the list wouldn't recognize the typed_data structure.

To solve this issue I designed function pointers which provide me with the following:
- Copying data (e.g. when …

Narue 5,707 Bad Cop Team Colleague

Well, there you go. Using feof() as a loop condition will most likely result in processing the last line of the file twice. This is because feof() only returns true after you've tried and failed with an input request like fread. Use the result of fread as your condition instead, and I bet it'll work just fine.

Narue 5,707 Bad Cop Team Colleague

I know C++ well and have written some programs in it but when I look at a program's code,I can't understand it.

There's a difference between knowing C++ and knowing how to program in C++. "Real world" programming involves more than just the language. There needs to be an understanding of the problem domain, the target environment as well as APIs and libraries used.

Real world code works around limitations and often isn't elegant. Real world code (hopefully) strives to be robust, so you'll encounter a great deal more error handling and reporting than in toy exercises.

I wanna know is there a place where I can learn to write real world programs

The only way to learn how to write real world programs is by reading and writing real world programs. Practice makes perfect, and we were all where you are right now. However, you have the enviable position of having so many open source projects (real world programs) to study. Those of us who started programming before that particular boom hit its stride had to figure things out through trial and error.

Celtrix commented: I still learn through trial and error, I find it much more efficient to learn the "Hard Way". Although I sometimes need DW to get me back on my feet. +2
Narue 5,707 Bad Cop Team Colleague

When it comes to being mods, I think we're all losers. But our loss is Daniweb's gain. ;)

Winner gets to delete all spam in the marketing forums for six months.

That's easy, just delete all new posts every time you log in. :D

Narue 5,707 Bad Cop Team Colleague

Both are important. Duh.

jingda commented: Lol +0
Narue 5,707 Bad Cop Team Colleague

the problem is in this part, in case u can't find it in the code above

So are the preceding 1500 lines relevant or not?

Narue 5,707 Bad Cop Team Colleague

Finally with my limitted knowledge of C, does declaring Result as static make sense?

It makes sense, but I'd argue that it's not the best choice. If you look at all of the problematic functions throughout the history of C, the ones that use static local storage are consistently on the list. In my experience, a better solution is to have the caller pass in a buffer of a suitable size:

char *IntToBinary(int in_Integer, char *out_Result, size_t in_MaxLen);

I would think that register hint for t could be used

First I'd challenge you to find a modern compiler that doesn't completely ignore the register hint. ;)

Narue 5,707 Bad Cop Team Colleague

hey look at line so and so ... thats all i needed

No, that's clearly not all you needed, because this wasn't enough:

At a glance, you should terminate str before copying it.

Narue 5,707 Bad Cop Team Colleague
Narue 5,707 Bad Cop Team Colleague

Perhaps it would be a good idea to have "don't subscribe" as the default. IIRC, it defaults to email subscription.

Nick Evan commented: indeed +0
Narue 5,707 Bad Cop Team Colleague

In such a case it's best to post the code and ask how to make it shorter. :icon_rolleyes:

Narue 5,707 Bad Cop Team Colleague

and if i should not even use a goto statement..
what might be the alternative..??

Are you reading impaired? I've listed several alternatives, and they STILL depend on how strict your definition of iteration and recursion are. Stop asking the same damn question when you need to be more specific. Answer the following questions:

  • If I recursively spawn new threads or processes to do the work, does that count as recursion?
  • If I call a library that performs a loop to do the work, does that count as a loop?
  • If I call another pre-existing program that does the work in an unspecified manner, does that still count as a loop or recursion?
Salem commented: Give it up, they ain't worth the effort +17
Narue 5,707 Bad Cop Team Colleague

Not to promote the use of alcohol, but it is in most places legal at least.

Well I turned 21 a few months ago, so I'm wondering what everyone else drinks when relaxing on their off days?

I like:
Miller Lite
Bud Light
Other non-traditional (non-mass-produced) beers like dos equis, killian's irish red
Miller High Life is dirt cheap, so when I'm on a budget it's great.

So, what are you drinking this weekend?

A good single malt scotch whisky, spiced rum, and margaritas (in order of preference) make up my alcohol choices. Occasionally I'll drink wine or beer.

Narue 5,707 Bad Cop Team Colleague

But when I close the file, all the other values become null (0), and only the values I modified stay as they are.

I suspect you're doing more than you say, but each time you open the file strictly for output, it will be truncated to zero length. Try this instead:

fstream saveFile(saveFilePath, ios::in | ios::out | ios::binary);
yoni0505 commented: Helped me to solve a problem! +1
Narue 5,707 Bad Cop Team Colleague

I have used calloc(), instead of combination of malloc and memset as a work around.

calloc is the functional equivalent of malloc + memset. It might be faster due to the potential for standard library optimization over hand rolled code, but probably not enough to make a big difference.

Have you considered

  • Avoiding dynamic allocation entirely?
  • Using malloc, but writing your algorithms such that memset is not necessary?
Salem commented: nice +17
Narue 5,707 Bad Cop Team Colleague

If you want both, you're SOL. Some kind of copying has to be done to end up with the right type.

Narue 5,707 Bad Cop Team Colleague

1) Where did the "5" come from? Why specifically "5"?

You don't need to know or care yet. When you gain proficiency and want to look into how compilers work at a lower level, fine. But for now, studying how undefined behavior works in practice will just distract you from the task of learning proper C. Further, it might encourage you to rely on dangerous behavior.

Had it returned "0" then I would at least understand from where the value returned came from.

Where would the value of 0 come from if your code didn't explicitly return it? I fail to see how 5 is baffling and 0 is not when they're both complete garbage.

Why isn't there a warning or failure? Neither cc nor gcc complains, even with the -pedantic option.

Your warnings aren't turned up high enough, because I get a warning on all of my compilers. Try -Wall and -WExtra.

Actually, I was under the impression that K&R was the standard for many years!

K&R is split into two parts: a tutorial and a reference. The reference served as something of a poor man's standard until ANSI and ISO rolled out a sufficiently large tome. Provided you follow the best practices given in the book, your own code will be pretty good.

Though, I can recompile and run and still get "5" so I actually think that there is something intrinsic about the code that gives "5".

I just ran your code in …

Narue 5,707 Bad Cop Team Colleague

All POST operations are performed at the end of a statement, and all PRE operations are performed at the beginning of a statement. Is that correct?

It's close enough, yes. To be strictly precise, the update can happen any time between the previous and next sequence point as long as the result of the expression is correct. So in your code, it could potentially work like this:

int numItems = 0;
int temp;

temp = numItems;
++numItems;
inventory[temp] = "sword";

temp = numItems;
++numItems;
inventory[temp] = "armor";

temp = numItems;
++numItems;
inventory[temp] = "shield";
Narue 5,707 Bad Cop Team Colleague

i want to write a program

the program should be written in C i need it for "borland c"

FYI, nobody here is going to write this for you. We'll help you write it yourself, but that's it.

Narue 5,707 Bad Cop Team Colleague

but why did it return anything at all?

Because you told it to:

int power(int x, int y);

This says "I will return an integer value". Even if you don't explicitly return something, the compiler will generate code to return a value. The compiler also won't stop calls to this function from using the return value. The assumption is that you, the programmer, didn't lie about returning a value. Since you lied, and didn't return a value, you get what you deserve: undefined behavior.

It would be null, 0, or some other logical value.

Okay, how do you plan to represent some logical value without eliminating one valid value from the type? Say you're returning int, every value in the range of int is perfectly valid. So which of those values shall be made not valid such that it can be used as the logical "null" value in the case where a programmer fails to return something? Further, why should the compiler cater to broken code at the expense of valid code?

I need to learn to read between the lines.

Maybe you're the kind of person who's better off just reading the standard rather than tutorials. At least then you'll have the painfully precise language lawyerese.

dotancohen commented: Thank you for your patience explaining. +2
Narue 5,707 Bad Cop Team Colleague

Note that in this case there is in fact a "useful value" returned, which is 5.

Just how is 5 useful? Sure, it's the correct type, but the value itself is completely unpredictable garbage.

Bug? Error in the text?

Error in reading the text. You apparently read it as "I can define a function that claims to return a value, not return a value, and magic happens". The actual meaning is that functions are not required to return a value. This is as opposed to languages that differentiate between procedures (which don't return a value in all cases) and functions (which do return a value in all cases).

Note that the first chapter is intentionally simplified, so many of the nuances will be left out. In practice, functions with a return type of void cannot return a value (you can use an empty return statement or fall of the end) while functions with any other return type must return a value or risk undefined behavior.

Salem commented: rep++ +17
Narue 5,707 Bad Cop Team Colleague

I just read in K&R that a function, even if not defined as void, need not return a value.

What page? From memory, the only place where K&R makes such a grievous mistake is in the beginning tutorial with the initial definitions of main. However, that's a pedagogical decision that they clearly explain a bit later.

Why is the function returning 5?

The usual explanation is that it's undefined behavior and anything could happen. But to speculate about what's happening in your case, 5 is coming from whatever was in already in the location where return values are stored. As an illustrative example, let's consider a hypothetical compiler that uses a hidden pointer parameter for return values. If you return a value it might look like this after compiler translation:

void power(int *__$retv, int x, int y)
{
    int i, total = 1;

    for (i = 0; i < y; i++)
        total *= x;

    *__$retv = total;
}

void main(int *__$retv)
{
    int __$power$retv;
    int __$printf$retv;

    power(&__$power$retv, 2, 5);
    printf(&__&printf&retv, "%d\n", __$power$retv);

    *__$retv = 0;
}

Without a return statement to translate, the power function would look more like this:

void power(int *__$retv, int x, int y)
{
    int i, total = 1;

    for (i = 0; i < y; i++)
        total *= x;
}

Notice how *__$retv is never set, and the hidden temporary in main is a local variable without any default initialization. This means that if you don't provide a return value in this …

Narue 5,707 Bad Cop Team Colleague

Not all keystrokes on your keyboard are represented by a single key code. Some keys have both an introduction code and a value code.

For example, the F keys (F1, F2, etc...) typically have 0 as the introduction code and then another value designating each key. Thus you would need to call getch twice to get the full code. Another example is the Ins, Home, Page cluster and dedicated arrow keys. Those typically have an introduction code of 224 and then the value code.

Why is this necessary? First because it's somewhat difficult to handle all possible keys on a keyboard with the limited unsigned char type and second because the value codes are reused. Take the Home, Up Arrow, and Page Up keys. First, notice that you have the dedicated keys in their own cluster, and overloaded keys in the number pad cluster. Home, Up Arrow, and Page Up do double duty as 7, 8, and 9 in the number pad cluster, but the value of those keys are no different from the dedicated keys (eg. 71, 72, and 73). So to tell the difference between those, the full key codes are (0:71), (0:72), and (0:73) for the dedicated keys and (224:71), (224:72), and (224:73) for the number pad keys.

The 256 part just normalizes the disparate code combinations in such a way as to avoid mixing the code up with an unrelated key while making sure that you'll get an arrow key regardless of whether …

Narue 5,707 Bad Cop Team Colleague

Is there a difference between programming and scripting?

This question cannot be answered until you clearly define what you mean by "programming" and "scripting".

Narue 5,707 Bad Cop Team Colleague

C++ requires a cast for conversions to and from void*:

SInt8 one_net_memcmp(const void* const vp1 , const void * const vp2, size_t n)
{
    const UInt8 * up1;
    const UInt8 * up2;

    // If invalid, just return zero since there is no error recovery
    if (!vp1 || !vp2) {
        return 0;
    }

    up1 = (const UInt8*)vp1;
    up2 = (const UInt8*)vp2;

    for (; n > 0; ++up1, ++up2, --n) {
        if (*up1 != *up2) {
            return ((*up1 < *up2) ? -1 : 1);
        }
    }
    return 0;
}
Narue 5,707 Bad Cop Team Colleague

what about void main()?

Not portable at best, undefined behavior at worst.

Working fine for me...

You'll find that "it works for me" is not synonymous with "it's correct" in C++. There are a lot of broken things that appear to work when you test, but can and do blow up at the worst possible time.

Narue 5,707 Bad Cop Team Colleague

Thank you very much for your help. Your code reads the error " error: no matching function for call to ‘std::istream_iterator<double, char, std::char_traits<char>, long int>::istream_iterator(std::istringstream)’ "

Serves me right for rushing something out without testing it on another compiler. It should work now.

Narue 5,707 Bad Cop Team Colleague

Since it's a random access iterator, you can take the difference of the two to get the distance between them:

i = myvec.end() - it;

However, this requires a random access iterator. If you decide to change the container to a list, for example, the trick no longer works. The standard library supports a distance function in the <iterator> header that will do the right thing regardless of iterator type:

#include <iterator>

...

i = distance(it, myvec.end());

If the iterator type is random access, the difference trick will be used. Otherwise it falls back onto the less efficient looping methods.

mike_2000_17 commented: Learned something new! cool! +11
Jsplinter commented: Well put, thank you. +2
Narue 5,707 Bad Cop Team Colleague

if you need the program for the university you can call him any time at his mobile phone.

I'm trying to fathom how you thought posting another person's telephone number was a good idea. :icon_rolleyes:

Narue 5,707 Bad Cop Team Colleague

What's the problem and how can I fix it?

const in C doesn't mean the same thing as const in C++. In C, the object is read-only at the source level, but does not constitute a compile-time constant like it does in C++. Your error is coming from this clause of the C standard:

All the expressions in an initializer for an object that has static storage duration shall be constant expressions or string literals.

So with your original code, a direct fix is to use a define rather than a const object for the string value:

#if 1
//! Set Pin string
#define ONCLI_SET_PIN_CMD_STR "set pin"
#endif
Narue 5,707 Bad Cop Team Colleague

And i really don't think that THIS IS THE FRIENDLIEST IT Community.

I'm sorry we didn't spoon feed you immediately. Oh wait, no, I'm not. If you had read our rules, you would know that we require proof of effort before helping with homework. You've given no such proof, so you get no help.

Narue 5,707 Bad Cop Team Colleague

We don't do homework problems for lazy students.

Narue 5,707 Bad Cop Team Colleague

That's an interesting test, but somewhat unrealistic since the words are random. I found myself slowing down significantly (74 WPM) because I trip up when copying poorly constructed grammar.

T.Rex commented: there where a lack of apostrophes... +0
Narue 5,707 Bad Cop Team Colleague

The first is a reference to const int, the object being referenced is const. The second is a const reference to int, the reference itself is const. However, a const reference is nonsensical because references are permanently bound to a single object on definition. To have a const reference implies that you could rebind the reference to another object, which is not allowed.

Narue 5,707 Bad Cop Team Colleague

Pointers to char are assumed to be C-style strings by cout. You can force them to be interpreted as pointers by casting to void*:

cout<< (void*)p <<'\n';
Muhammad Anas commented: Thanks for the help!!!!! +1
Narue 5,707 Bad Cop Team Colleague

You kids learn some crazy things. In the real world, there's only one methodology:

  1. Stare at the monitor with a confused expression
  2. Type feverishly
  3. Stare at the monitor with a distraught expression
  4. Type feverishly
  5. Make an angry face
  6. Pound on the keyboard
  7. Repeat at #1 until you have something reasonably stable that meets requirements
ChrisHunter commented: good comment +2
kvprajapati commented: :) +15
mikulucky commented: Yeah that sounds about right. +1
Narue 5,707 Bad Cop Team Colleague

Daniweb already has an unwieldy number of forums and subforums, so while we're not against adding a new forum, there has to be obvious and significant benefit to justify the addition. In all honesty, I don't see Razor as making the cut at this point.

jonsca commented: Excellent pun +0
Narue 5,707 Bad Cop Team Colleague
Muhammad Anas commented: humm .. it means that I am making progress .. hahaha ... thankyou very much for the feedback!!! +1
Narue 5,707 Bad Cop Team Colleague

I use my iPad to visit Daniweb occasionally, but posting is a bit of a chore.

jingda commented: Agree +0
Narue 5,707 Bad Cop Team Colleague

The underlying problem is failure to include <stdlib.h> and using the abort function without a visible prototype.

vedro-compota commented: ++++++++++ +3
Narue 5,707 Bad Cop Team Colleague

The best cast for quicksort is an already sorted array when the implementation checks for an already sorted subset. In such a case, no partitioning takes place and no recursive calls are made, which results in linear complexity:

function quicksort(a, first, last)
    if is_sorted(a, first, last) then
        return
    end if

    pivot := partition(a, first, last)

    quicksort(a, first, pivot - 1)
    quicksort(a, pivot + 1, last)
end function

Assuming the original quicksort algorithm with no improvements, the best case is an even partitioning in all steps, which is O(N log N).

i m just not able to come up with a large array, that fits this condition perfectly

The dependency is your partitioning scheme, so to create an array that fits the best case, you need both a deterministic partitioning scheme (ie. random pivots will need to be mocked) and an array that gets partitioned perfectly each time. Without knowing your partitioning algorithm, I can't really offer much more advice than that.

jon.kiparsky commented: And that about covers it... +11