deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Dude, I'm not against helping you, but you need to read our rules:

Keep It Organized
  • Do provide evidence of having done some work yourself if posting questions from school or work assignments

If you continue to post nothing but homework questions without any evidence of thought or effort on your part, I'm going to start deleting.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

i don't understand the question its bonus question

What part don't you understand? You're supposed to write a program that accomplishes the stated goal, and the question even tells you how to implement it. Since this is a homework/classwork question, I'm not going to do it for you, but I'll be happy to help you understand any specific problems with the question itself, or any specific problems you have with your code.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Please provide evidence of having attempted to solve this homework question on your own.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

data.read(reinterpret_cast<char*>(corpData), sizeof(corpData));

This is an exceptionally bad idea because the Division type (of which corpData is an array) contains non-POD object types, std::string to be precise. You can't safely treat non-POD types as sequences of bytes for two very importan reasons:

  1. The structure of the object is dependent on many things, and internal "dead space", or padding, can create trap representations that will likely crash your program.

  2. Classes that manage dynamic memory internally won't be able to reproduce their structure or data simply by xcopying bytes from a file into an object that's not specifically prepared for the data. This is the most likely cause of your current crash.

I strongly recommend that you take a step back and do I/O in a simpler manner. Instead of trying to pun your data into a sequence of bytes, write and read each element of the array in a precise manner, field by field. It might be more tedious because you can't use a one-liner like read() or write(), but it's much safer.

An alternative is to support a serialize() and deserialize() process in your Division objects. This would produce a sequence of characters that can be safely read and written using the binary one-liners of read() and write().

So to summarize, these two lines are broken and need to be replaced with a different solution entirely. There's no way to make them safe as-is:

data.write(reinterpret_cast<char>(corp), sizeof(corp));
data.read(reinterpret_cast<char
>(corpData), sizeof(corpData));

Sendy Hipo commented: thx :D +0
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

No offense, but while I could do your debugging for you and tell you exactly what the problem is and how to fix it, you wouldn't learn jack from that. Given that this is the first of many, many, many access violations you're sure to encounter, you'd benefit greatly from learning how to troubleshoot the problem on your own in a very simple program.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Aha! so is it completely fine to drop "iterator" and template <typename Iterator> ?

Yes, but only if you replace any use of Iterator with the appropriate corresponding type. In your example, int isn't the correct type; it would be int* because that's the correct "iterator" type for an array of int. It's important to recognize that the template parameter is just a fancy cut and paste. It figures out what type you're actually using and replaces the template with a concrete definition using that type. Note that we're working with addresses here, not data values, so the correct conversion (and what the template system would do automagically) results in this:

#include <iostream>
#include <algorithm>
void heap_sort(int* begin, int* end)
{
    std::make_heap(begin, end);
    std::sort_heap(begin, end);
}
int main ()
{
    int a[ ] = {1, 2, 3, 4, 5, 6, 7};
    const int n = 7;
    heap_sort( &a[0], &a[n]);
    return 0;
}

You must have a valid iterator, even if the concrete form of the iterator is a pointer, because that's just how make_heap() and sort_heap() are written. If you're writing your own heap sort algorithm you can do what you want and make it work by passing two indices, but when using a library you have no choice but to conform to its design.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

The error you're getting typically means there's an access violation. Access violations are typically caused by invalid pointers, so your first line of attack should be to step through your code in a debugger and keep a close watch on your pointers to make sure that they're valid at any given time.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

That does strike me as something that would best be implemented using a graph, but my experience in 3D motion capture is pretty much nil. So I'll defer to someone else to offer up a viable solution. ;)

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

The thing is, I don't know what's the "Iterator" doing exactly and what's begin/end, how can I use arrays with the former piece of code?

"Iterator" in this case is a generic term for the location of the first item in the collection and one past the last item in the collection. You can translate it directly to arrays like so:

int a[] = {1, 2, 3, 4, 5, 6, 7};
const int n = 7;

heap_sort(&a[0], &a[n]);

&a[0] is the address of the first item, and because arrays are indexed 0 to n-1, &a[n] is the address of one past the last valid item. Note that this is perfectly valid. The C++ standard explicitly allows you to take the address of one past the last valid index, and as long as you don't try to read the valid there, it's completely safe.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Your description is somewhat vague, but it sounds like you're looking for a graph. What exactly are you trying to accomplish with this data structure and algorithm? Explaining the ultimate goal of the feature as opposed to speculating about how it might be implemented will help third parties (like us) fully understand what you want to do.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

This page has a good explanation with several different code variations. Read through that and if you have any specific questions, I'll be happy to answer them for you.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

That's all fine and dandy but it does NOT explain the fact that my strings change their values for no apparent reason.

Yes, it does. Let's do a little visualization:

 a   b
---------
| | | | |
---------

Copy "12\0" to a (note how a isn't big enough, so it overflows into b):

 a   b
---------
|1|2|0| |
---------

Copy "x\0" to b (note how the \0 from a is overwritten):

 a   b
---------
|1|2|x|0|
---------

Print a, and the output is everything up to \0: "12x"

You're a victim of buffer overflow.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

It is very convenient to have an implicit conversion to bool, as in while( cin >> value ) and things like that.

With C++11 that particular conversion was made explicit bool:

explicit operator bool() const;

But before that, due to unintended consequences, the effect was achieved through an implicit conversion to void*:

operator void*() const;

At the very least there's a precedent for avoiding it. IIRC, one of the reasons was conversion to other integral types in typos and nonsense, like if (cout < 123). Granted, those are obscure, but std::ios_base is a very heavily used class, so it may just have been covering for unknowns.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

The compilation success is contigent on the fact that the compiler allows 0 to be implicitly converted to the pointer type DOMNode*. I'm not sure what is the standard behavior required here.

The integer 0 is always recognized as a null pointer when in pointer context. I'd be very very surprised if any modern compiler didn't allow that conversion. However, the OP could be using something ancient. The code looks fine even for older compilers, but I don't have any of them handy to test with, so I can't say for certain.

In general, if you replace 0 with NULL, it should work, because NULL must be convertible to any pointer type, and for compilers that don't automatically convert 0 to pointer types, they define NULL as ((void*)0) which solves that problem.

C++ doesn't include a cast for NULL because that would produce annoying warnings about conversion from a pointer to void. For example:

int *p = ((void*)0); // Bzzt! Cannot convert void* to int* without a cast

To the best of my knowledge, NULL has been defined as the following since the early days of the ISO standard:

#define NULL 0

So in general, NULL and 0 are synonymous, where NULL would be used to make it clear you intended a pointer context. Ideally, with the advent of C++11, nullptr would be used instead, and the whole issue becomes moot. :D

A common trick to solve this problem is to …

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Read Let us c by yashavant p. kanetkar

And be aware that it's not a very good book, despite being highly recommended by a large portion of equally not very good C programmers. ;) But you can still learn things from a bad book, so I encourage reading as much as possible.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Assuming you meet the requirements of LGPL, it seems that way. This is cool, I wasn't aware Qt had moved to an open source library model. That's what had kept me from using it. :)

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I don't get that error in Visual Studio, GCC, or Comeau. What compiler are you using?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Although the pros of this he gave were strong (so that it was adopted), it still doesn't sit right with me.

Coding styles are built on good arguments. If you couldn't come up with any cons for his method then there's no reason not to use it. Case in point: I didn't care for Dani's preferred coding style on Daniweb, but it was a reasonable style so I couldn't make any arguments against it that weren't purely personal preference. So we used that style. ;)

Another disagreement was with braces - where to put them?

This is usually the biggest disagreement. The usual advice is to put them anywhere as long as it's consistent, but if you find someone's preferred bracing style so completely difficult to read then it might be a good idea to ask them to change so that the project can get rolling more quickly. For example, the K&R and Allman variants are both widely used and admittedly easy to read:

K&R:

#include <iostream>

using namespace std;

int main() {
    int a[] = {1, 2, 3, 4, 5};

    cout << "Array contents:\n";

    for (int i = 0; i < sizeof a / sizeof *a; ++i) {
        cout << "a[" << i << "] = " << a[i] << '\n';
    }

    cout << "Done.\n";
}

Allman:

#include <iostream>

using namespace std;

int main()
{
    int a[] = {1, 2, 3, 4, 5};

    cout << "Array contents:\n";

    for (int i = 0; …
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Have your boss put a constrain on you that you should only use standard library. However you could do it.

Not with standard C++ since there's no concept of a directory, but using the system's standard API or a wrapper on top of it you certainly could. For a portable library I'll suggest Boost::FileSystem.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

When you call a function, you do not but a space between the function name and the open bracket.

You've pointed out a case of total irrelevance. Whether the space is there or not changes absolutely nothing, though I agree that it's recommended to be consistent with whatever style you choose.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

it's not homework

It's close enough.

i have a final exam and this question was in one of the final exam sample
i just want to know how to solve it.

I have an idea, how about you try to solve it and we'll help out when you hit any stumbling blocks. Just having someone else solve the problem for you doesn't teach you jack about how to solve problems.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

As I have tested in VC++, if I use queue from <queue> and call any of these functions for an empty queue, program crashes.

If the queue is empty, what were you expecting a reference to by calling front() or back()? This is a case of undefined behavior, which is largely why the empty() member function exists.

Could this problem be solved by simply adding a throw, in case when queue is actually empty, so front and back would throw an exception which'd be caught by try block?

Assuming we're moving away from std::queue and into your own implementation of a queue, that would be an acceptable solution.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

What have you done so far to complete the task aside from starting this thread in the hopes that someone will give you the answer?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Have you searched previous questions of this nature? Writing a compiler isn't a trivial feat, and C++ is an especially difficult language to process. Your best bet is to start by learning how to write a very simple compiler and then slowly expanding on what you learn.

Don't expect to write a C++ compiler as your first attempt.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

real_T add(real_T x, real_T z);

And where is add() defined? The error is saying that you have a declaration for the function, but no definition exists and therefore it cannot be called.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Please post in English. On top of not conforming to our rules, you're limiting the number of people who can (or will choose to) help you by asking questions in French.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

i'm so sorry for this answer but if i don't need ur help i wouldn't sign up here to contact with u!!!!!! i'm a student and i'm not good in this course this data structure i repeat it many times ,, i need to graduate only for my parents ,, i'll not going to work in this domain because i don't like it but i need to graduated ,,, i wrote something in this program, but this is my first time to put my question here because of this i didn't write anything more ,, but if u can't help me u can say sorry we can't help u and don't write some silly words because when i wrote to u i wrote in a respect way ,,,,

Please read our rules: "Do provide evidence of having done some work yourself if posting questions from school or work assignments". If you aren't willing to do anything, we'll show exactly the same attitude toward helping you. I'm happy to teach what I know about ternary trees, but not to a time vampire who has no interest in putting forth even enough effort to pass his class.

So I'll kindly suggest once more that you show some proof of effort, or I'll close this thread as it's in violation of our homework rule.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

toupper() both accepts and returns an int to account for EOF. You need to assign that result to a char or cast it before printing:

writefile << (char)toupper(ch);
Sendy Hipo commented: nice! +0
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Have you read the Starting C sticky?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Do you want to say that, if i define pointer to a char,it doesn't reserve the memory for entering a string??

Exactly.

However, my problem being, the below code does take the string,prints it in the 6th line, but doesn't print the character at the adress.

Your problem is that invoking undefined behavior (eg. writing to memory you don't own) can do anything, including work as expected. That's why the whole "it works for me" argument is invalid when it comes to undefined behavior. Unfortunately, working as expected gives people a false sense of security in broken code, which is why you have to know what causes undefined behavior and avoid it.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Can you post a complete program that exhibits the problem? cout should be smart enough to treat chars and strings differently from integer types, so I suspect the snippet given isn't entirely accurate compared to what you're actually testing.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Yes, there are many things the editor converts that the Markdown parser doesn't. Unfortunately, we use an editor with an especially obtuse and finicky parsing algorithm, so I've had trouble customizing it without making Dani nervous. ;)

The posted result is what should happen, sadly. So keep an eye on the preview for now.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

The error suggests that you have two main() functions, which isn't allowed in C++.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

The problem is that the line will contain more things than just the number.

Then you need to parse the line to extract the number. This is why I asked what the format of the file was in my initial reply. Unfortunately, I notice that you left out the last line, which I assume is the remaining credit while the file starts with a beginning credit. So I can't say how to parse the last line...

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

how to write it :)

Is this your question? If so, you'll need to put forth a little bit of effort. I doubt anyone is willing to write a ternary tree for you because you're too lazy to make an attempt.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Well, presumably the number is on the last line of the file by itself, so a general solution would be to extract the last line:

FILE* fp = fopen("file", "r");

if (fp)
{
    char line[BUFSIZ] = "";

    while (fgets(line, sizeof line, fp))
    {
        /* Nothing to do here */
    }

    if (line[0] == '\0')
    {
        fputs("Remaining credit not found\n", stderr);
    }
    else
    {
        /* Using atoi() and no error checking for brevity only, not recommended */
        printf("Remaining credit: %d\n", atoi(line));
    }

    fclose(fp);
}
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

What part of "small, complete program that exhibits the problem" was unclear? I want to copy the code as-is, paste it into my IDE, and compile it without changing anything.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

What's the format of the file? Is the number being read as a string or an int? Is the file being opened as binary or text? There are a number of things that could throw a wrench into any solution, so you need to provide more information.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

One thing to note is that difftime() returns a double. If you then cast the result to int, any precision is lost. Another common issue when difftime() appears to return 0 and the dates are clearly different is using the wrong specifier with printf().

Can you post a small, complete program that exhibits the problem. That would be easier than requiring people to extract the relevant parts of your snippet and write a framework around it. Unfortunately, both of those tasks can hide the error on our end, so something that can be cut, paste, and run without changes would be ideal for troubleshooting.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I wouldn't say it's a favorite, but I'm cheap, and the best free IDE I've used so far has been NetBeans.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

time_t isn't a compatible type with unsigned int, which srand() expects as an argument, so the implicit conversion fails. You can take the easy way out and cast the result of time() to unsigned int:

srand((unsigned)time(NULL));

However, be warned that this conversion technically isn't portable. But in practice, the chances of it failing are vanishingly small.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I suspect that's the wrong language! The usual Java version is System.out.println(...

Whoops. Can you tell that I'm a C# guy? ;p

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

If all you're doing is excluding the 'b's then it's as simple as this:

for (i = 0; i < length; i++)
{
    if (writtenStuff.charAt(i) != 'b')
    {
        Console.out.print(writtenStuff.charAt(i));
    }
}
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

This is not my homework. I am 15 years old. I am learning java just because i like computer.

"Homework" in this case is meant in the more general sense of something that you'd benefit from putting effort into rather than simply being handed explanations and solutions. Experts don't become experts by having everything explained to them, they become experts by not depending on others to do their thinking.

This isn't to say that we won't help you along, but especially if you're doing this as a hobby because you enjoy it, we're less tolerant of apparent laziness in the learning process.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster
  1. If the search string is "abcdefg" and the search key is "xyz", there's no point searching beyond 'e' because a shorter search string than the key is guaranteed to not match.

  2. test is an abritrary label for the break statement. If it doesn't exist, the continue and break statements won't know where to jump to. Read up on the goto statement, it's a better way to get introduced to unconditional jumps.

  3. Step through the code in a debugger and you'll find it easier to wrap your head around exactly what's going on. I'm not sure anyone will be willing to hold your hand through the entire algorithm, simple as it is.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

You can use mktime() after populating a tm object with the suitable date. Then the comparison can be done with difftime():

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

time_t get_today(struct tm** date, int is_gmt)
{
    time_t now = time(NULL);

    if (date)
    {
        *date = is_gmt ? gmtime(&now) : localtime(&now);
    }

    return now;
}

time_t get_date(struct tm** date, int is_gmt, int year, int month, int day)
{
    struct tm* working_date;
    time_t ret = get_today(&working_date, is_gmt);

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

    if (date)
    {
        *date = working_date;
    }

    return ret = mktime(working_date);
}

int main(void)
{
    int year = 2012, month = 6, day = 20;
    time_t today_time = get_today(NULL, 1);
    time_t target_time = get_date(NULL, 1, year, month, day);

    if (target_time == -1)
    {
        fprintf(stderr, "Invalid date: '%d-%d-%d'\n", year, month, day);
        return EXIT_FAILURE;
    }

    if (difftime(target_time, today_time) < 0)
    {
        puts("Your trial period has expired");
    }
    else
    {
        puts("You are using a trial version of this software");
    }

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

And what's the problem?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

and the .h is depreciated in the C++ language nowdays.

You mean deprecated, and that's still not correct. Deprecated is a term used for standard C++ features that have been superseded by another feature and are no longer recommended for use because they may be removed from the standard in a future revision. <iostream.h> was never a standard header, it was morphed into
<iostream> during development of the first release of ISO C++, so deprecation status is impossible.

<iostream.h> is simply non-standard.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

you have to create a parse tree of the float chart, then you can do a depth first search to get the contents of the parse tree and create your pseudo code. if you are not familiar with the depth first algorithm, this is how it works, you have to get the left most node then go back to its parent, in a flowchart only the condition will have left and right node. this has been done by my classmate during college and help them formulate an algorithm that will translate a flowchart to c or c++.

Dafuq did I just read? Incognitus, I suspect the OP is asking how to write pseudocode corresponding to a flow chart manually, not how to devise an algorithm that does it programmatically.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

How many supported phrases do you ultimately want? If it's only a handful like in the example program, even chains of if statements will be more than fast enough. Walt's suggestion of binary search is the next step, followed by perhaps a binary search tree.

If you want a lot of phrases then something better suited to textual search would be a good idea, in which case I'd recommend looking into a trie. The reason I suggest this only for a large number of phrases is because the setup and constant overhead for a trie can be overwhelming on smaller collections even though the search complexity is excellent.