deceptikon 1,790 Code Sniper Team Colleague Featured Poster

The two primary problems are that head is never updated in main, and your output loop never sets stdPtr to stdPtr->next. Given your description of the problem, as well as syntax errors, I find it difficult to believe that the provided code is the same code that you're executing.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

On Windows it is '\r\n' on Linux it is '\n'.

Which is largely irrelevant because text oriented streams will convert the platform's representation of a newline into '\n'. There are a few edge cases like processing binary streams or parsing files created on a different platform, but for the most part it's a non-issue.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Agreed with Moschops, but this is still a legitimate question.

When does it become necessary to allocate memory for an array?

Typically when the size of the array is unknown until runtime, or the lifetime of an array would be insufficient for your needs (such as returning a locally generated array from a function). Another reason might be that the array is large enough to risk overflowing the stack, but in that case you probably need to consider other data structures.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Your fabulous prize is being listed on the day's leaderboard. ;)

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

English, a bit of Japanese, and fluent profanity.

<M/> commented: I am with you on profanity +0
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

This is your homework, not ours. We'll help if you have a specific question, as long as the specific question does not constitute "do it for me".

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

and there is no relation between bitmap and widget

Well, they're still coupled together in that widget knows about and contains a Bitmap. The problem from before is treating the two classes as entirely compatible, which isn't the case. Here, the usual method would be to provide a constructor, member function, friend function, or operator that handles the initialization/assignment (given that your Bitmap member is private) and simulates that compatibility.

Another problem is how much visibility you're providing from Bitmap. Is the type opaque to widget? Because if it is, you're extremely limited in what you can do with a pointer to Bitmap from within widget.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

They changed the Recycle Bin icon? That's my favorite feature, and the only reason I use Windows! That's it, I'm officially and irreversably moving to Minix. This travesty cannot be be reconciled. Who does Microsoft think they are? Geez!

stultuske commented: ^_^ +0
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

could you let me know why after assignment i am getting error like a* can't be assigned to b* or vice versa

Because they're not compatible types. An a is not a b or convertable to a b, and subsequently a pointer to a is not a pointer to b. C++ is strongly typed, which means without careful scaffolding to support mixing and matching of otherwise unrelated classes, you're going to get errors like this.

So you've spent this thread explaining what you're trying to do, I'd still like to know why you want to do it. What's the problem being solved? There's very likely a better way to complete the higher level task than the way you're going about it.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Write code. Lots and lots of code.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

It is not incorrect way to delete the cobj operator.

In your first snippet (you'll notice, that's what I referred to before), it very much is incorrect because the object passed into the class was not allocated using new.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

The code looks dicey, especially how you incorrectly delete cobj in the first code. What are you really trying to accomplish with this? The class and variable names aren't enlightening as to the underlying design problem you want to solve here.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

so this is not language dependent right?

After a fashion. It's a language-dependent optimization, but it is very common. I would default to assuming that string literals are shared since that's a safer assumption in the general case.

in C also i am getting same results

Yes, C and C++ share roughly the same rules for string literals.

so optimisation will be always done for string literals..

It's not a guaranteed optimization, but I don't know of any modern C or C++ compilers that don't implement it.

they always reside in read only memory..

Yes, this is effectively required by both standards. A compiler can store string literals in read-write memory, but you're not allowed to take advantage of that in portable code.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Good luck and have fun! :)

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Is it possible to obtain 15 reputation points in your first 15 minutes of registering?

Yes. If you catch the eye of one of our heavy hitters in terms of reputation power, you can reach that in one shot (a handful of the top posters have +15 power). Or a series of rep votes from those with a weaker power will do the same thing. However, this depends on other members recognizing a good post and applying rep to it.

Though it might be hard to find a thread, compose a rep-worthy post, and have someone see and vote on it within 15 minutes of registering. So I agree with priteas that it's unlikely.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

How later on reset all elements back to 0.0?

There are a number of ways. One that comes to mind is:

v2d.assign(2, vector<double>(3));

This will completely replace the contents of the vectors with default initialized objects. However, that can be troublesome if you have pointers into the original objects. If that's the case, you're stuck with some variant of a value-replacement loop to retain the same addresses for elements:

for (auto& x : v2d)
{
    for (auto& y : x)
    {
        y = 0;
    }
}

How to clear vec entirely, free memory?

C++11 added a shrink_to_fit member function that is intended to correct the capacity based on the vector's size. However, it's not a hard requirement that memory is released, so we're still in a position where certain tricks are the only guaranteed way to ensure memory is released. For example:

vector<vector<double>>().swap(v2d);

This is a full clear and release, v2d will erase all of its elements. Alternatively, you can just let the object go out of scope and its destructor will release memory for you.

If you want to reset the vector to its original initialized state, that can be done as well given the provided definition of v2d:

vector<vector<double>>(2, vector<double>(3, 1.0)).swap(v2d);

Are there any performance degradation in using vector arrays verus conventionl arrays using new or malloc?

In theory, yes. A carefully written dynamic array can outperform std::vector for specialized code. In the general …

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I thought of searching the web

And what did your web search tell you? Some of your questions are easily answered with a web search, so my answers may be somewhat sarcastic. Where the question is trivially found, I won't even bother.

How to properly override the ostream operator?

ostream& operator<<(ostream& out, const MyClass& obj)
{
    // Class-dependent insertion

    return out;
}

Of course, that implies a dependency on std::ostream, which means you can only call this operator<< with narrow streams. A more extensible overload would look like this:

template<typename CharT, typename Traits>
basic_ostream<CharT, Traits>& operator<<(basic_ostream<CharT, Traits>& out, const MyClass& obj)
{
    // Class-dependent insertion

    return out;
}

This is the non-member design. If the function requires access to private members, you can make it a friend.

What exactly is big O notation?

A way of calculating the growth rate of algorithms. Click Here.

How to count primitive operations?

Please elaborate.

Is there a way to see how much memory my program uses?

Yes, but it's platform-dependent.

Can I use redirected input in eclipse or Xcode?

Yes, you can redirect from the command line or within code. I'd suggest doing it from the command line as you don't need code changes and in-code redirection can be tricky.

What can I truly do with memory management in C++?

Manage memory...

Why isn't everything written in c++?

Why doesn't everyone …

rubberman commented: D, you have a lot more patience than I do! :-) +13
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

It's a compiler optimization. Given that string literals are read-only, there's no reason to create two separate instances of the same string literal in memory.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Functions support parameters, which are designed for this exact purpose:

int add(int a, int b)
{
    return a + b;
}

int main(void)
{
    int a = 5;
    int b = 2;
    int c = add(a, b);

    printf("%d + %d = %d\n", a, b, c);

    return 0;
}

This is basic C that any reference will cover

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

If you show us your code, we can offer advice on improving it. For now, I can only answer your questions.

Does fscanf not work with negatives?

It works fine, provided you use a signed format specifier like "%d".

Do I have to read in the entire file, then parse by fgets or fgetc (or whatever the individual char function is)?

Probably. I say this because each line represents a row of the matrix, and fscanf will not tell you when it reaches a new line. All whitespace is equal where fscanf is concerned.

Why is this such a pain in the donkey? If it were integers it seems like it would be a lot easier.

My guess is it's a pain because you're trying to match a series of lines in the file to rows in an array. I strongly doubt this would be alleviated by storing integers in the file rather than floating-point. It's really a parsing problem, not a data representation problem.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Please read our rules concerning homework. I'll give you time to ask a more specific question and show your work thus far. Otherwise this thread will be closed as a rule violation.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I humbly apologize and throw myself upon the mercy of the court...

I find you guilty and sentence you to ten lashes with a wet noodle. After that, you must endure...THE COMFY CHAIR! Muahahahaha!

ddanbe commented: You're a cruel judge, with no compassion! +15
gusano79 commented: I didn't expect th-- AAH NO, NO, NOT THE EXTREMELY SOFT CUSHIONS...! AAARGHHCHGBKL#BO%@B%$LB!!!#@$ +10
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

However, students may also appreciate knowing that the author of C++, Bjarne Stroustrup himself, recommended cin.sync() to flush the cin stream, as used in the above code.

Yes, I'm aware. However, Bjarne is not infallible, and this is one of those cases where he's wrong. The C++ standard categorically contradicts his recommendation, and when there's a contradiction of advice, the standard rules.

And I have never had a problem on a modern compiler ...

'It works on my compiler' is very rarely a defensible position.

But for quick and easy ...
beginning student-type numeric input type coding problems ...

I agree. That's why I didn't burn you more harshly. ;) There's a certain iterative process in learning, where the strict quality of code improves bit by bit as edge cases like this are learned. So I'm tolerant of poorer examples even though I still prefer to mention that they're poor and point to supplementary resources for improvement if the student is so inclined.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

i need the name of simple search algorithms

And you have a list of most of them. Put in some effort, dude. We're not going to do your homework for you.

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

cin.sync(); // 'flush' cin stream ...

Sadly, it's not that easy. istream::sync is in no way guaranteed to read and discard remaining characters in the stream.

The best approach, in my opinion is either 1) don't depend on the stream being 'clean' in the first place or 2) ensure that it's clean by reading full lines and parsing them in memory where you have more control.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Please help me with reasons or ideas to support the above statement

To qualify Davey's hilarious response, your "question" reeks of homework. As such, we'll require that you put some effort into starting a dialogue by offering the reasons and ideas you already have. Otherwise, we'll be forced to assume you're just trying to leech answers without putting in any effort.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

stdout and stdin are not tied, so the output from printf is only guaranteed to be flushed if you print a newline or call fflush. In this case, you probably don't want to print a newline, so the only solution is fflush:

int main ()
{
    int i;

    printf ("Enter integer: ");
    fflush (stdout);

    if (scanf ("%d", &i) == 1)
    {
        printf("Detected %d\n", i);
    }

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

If there's a write error to the destination stream, you'll get a return value of EOF. Typically this means something along the lines of the target device no longer being available or getting corrupted. However, since puts writes to stdout, this is rather unlikely unless you've redirected stdout to a volatile location.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

That design sounds dicey to me. My preference would be for the main form to manage itself completely, where temporary dialog forms are opened as needed and pass back information objects through properties or events for the main form to own and manage beyond the lifetime of the dialog.

For example:

// Main form
List<InventoryItem> _items;

void addItem_Click(object sender, EventArgs e)
{
    using (var dlg = new AddItemDialog())
    {
        if (dlg.ShowDialog == DialogResult.OK)
        {
            _items.Add(dlg.Item);
            RefreshInventory();
        }
    }
}

// AddItemDialog
public InventoryItem Item { get; private set; }

void buttonOK_Click(object sender, EventArgs e)
{
    Item = new InventoryItem(...);

    // Populate the item with form element data

    DialogResult = DialogResult.OK;
}
darkagn commented: Well explained with good example +11
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

What does allocate look like?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I'd just escape it:

string[] items = str.Split(new char[] { '°', '\''});
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

A blank string is indeed not a valid path. Either you need to account for the possibility of a blank string and act accordingly, or ensure that the blank string doesn't happen.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Databases are pretty fundamental to both parent categories, and while I can guess why Dani put it under web development originally, our current push for tagging rather than categories means that the location becomes less important. Unfortunately, it means that in the interrim there's a bit of confusion.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

can it be omitted in this case?

Yes, a default case can be omitted if you already cover all of the possible cases, or not executing any of the cases wouldn't be damaging.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Experiment with the following, and it's relatively easy to derive a solution by breaking down the number in a loop:

#include <stdio.h>

int main(void)
{
    printf("%d\n", 1234 % 10);
    printf("%d\n", 1234 / 10);
    printf("%d\n", 1234 % 100);
    printf("%d\n", 1234 / 100);
    printf("%d\n", 1234 % 1000);
    printf("%d\n", 1234 / 1000);

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

% is the remainder operator. It performs divison and returns the remainder, so i % 10 only returns 0 when i is divided by 10 evenly.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

You're thinking about it too hard. Just loop from 1 to 100 and add 10 at every multiple of 10 after updating your sum.

for (i = 1; i <= 100; i++)
{
    sum += i;

    if (i % 10 == 0)
    {
        i += 10;
    }
}
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

if the str were to be an array of two dimensions, eg. str[][] format how would it look like?

In a function declaration, only the first dimension size can be omitted. All subsequent dimensions must have a matching size to the array you pass, so it would be something like this:

int mult(int x, int y, int str[][10]);

If you need variable sizes for other dimensions, this is a case where you'll need to simulate an array using pointers completely:

int mult(int x, int y, int **str);

But that changes how you create and use the "array". A proper 2D array won't be compatible with that function signature.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

DATA[0] = new int[100]; //this works

Yup, as it should.

DATA[1] = new int[100][5]; //but this don't

Of course it doesn't, because you're initializing an int* with an incompatible type. int[100][5] is not compatible with int*. You could fake it by saying DATA[1] = new int[100 * 5] and handle the indexing of the two dimensions manually, but in this case I'd favor an array (or vector, ideally) of array class that can handle variant dimensions without affecting your type. A simple example of the concept (pre-C++11):

#include <cstddef>
#include <iostream>
#include <vector>

using namespace std;

namespace JRD
{
    template<typename T>
    class VariantArray
    {
        vector<T> _data;
        vector<size_t> _rank;
    public:
        VariantArray(size_t size): _data(size)
        {
            _rank.push_back(size);
        }

        VariantArray(size_t size1, size_t size2):  _data(size1 * size2)
        {
            _rank.push_back(size1);
            _rank.push_back(size2);
        }

        size_t Ranks() const { return _rank.size(); }
        size_t RankSize(size_t index) const { return _rank.at(index); }

        T& Item(size_t index) { return _data.at(index); }
        T& Item(size_t index1, size_t index2) { return _data.at(index1 * _rank.at(1) + index2); }
    };
}

int main() 
{
    vector<JRD::VariantArray<int>> data;

    data.push_back(JRD::VariantArray<int>(5));
    data.push_back(JRD::VariantArray<int>(2, 5));

    // Initialize the arrays for a clean test
    for (int m = 0; m < data[0].RankSize(0); m++)
    {
        data[0].Item(m) = m;
    }

    for (int m = 0, k = 0; m < data[1].RankSize(0); m++)
    {
        for (int n = 0; n < data[1].RankSize(1); n++)
        {
            data[1].Item(m, n) = k++;
        }
    }

    // Display the contents of the arrays
    for (int i = 0; i < 2; i++)
    {
        cout << "data[" << i << …
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

A language expert Mr. ABC objects the designers of C++ on the bases of following two arguments

I suspect this 'language expert' doesn't actually write code. Orthogonality is an important concept in language design, yet even more important is sacrificing orthogonality for usability. It doesn't matter how beautiful a language is if nobody uses it.

My question is which of these available looping constructs does Mr. ABC prefer as the "only right one", and why?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

It kind of depends on the parts you're finding difficulty with. If you can be more specific, I may be able to offer some advice. Though it's mostly just buckling down and taking more care as you write, which can be beneficial in that it forces you to think more.

In my experience, I've been on the winning side more often than not in terms of coding standardization, but there's definitely a certain measure of compromise. And yes, things that are completely different from my personal style clash for a while. However, there are some aspects of an office coding standard that I've adopted into my personal style.

I have my own standards, and they are that way for a very specific reason, and have fine tuned over the years

That's all well and good. I'm sure we're all the same way. But after many years, I find that my style evolves. What I thought was ideal 10 years or even 5 years ago is woefully suboptimal to me now. The important thing is to be willing to embrace changes, because they might turn out to be your next favorite thing. ;)

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Yes.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

The command looks dicey to me. What is 'test'? If it's a variable, the string is constructed incorrectly:

"DELETE FROM just WHERE folder = '" & test & "';"

If it's a literal value, you need to say as such in the string:

"DELETE FROM just WHERE folder = 'test';"

Either way, I suspect you're checking a string value, in which case single quotes are needed.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Typically modern systems will use a cryptographically secure one-way hash to store credentials. Provided the password is strong, coupled with judicious salting, it can be quite difficult to break given only the hash result. Not impossible, mind you, but much more secure than using straight up encryption.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

saying File not found exception was unhandled

Okay, what's a, and how have you populated/verified the FileName property? Clearly it doesn't refer to a valid file, so you need to figure out why. I wouldn't recommend simply wrapping the code in a try..catch block, because the exception suggests an underlying logic error.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Is this just for practice, or is the disk cleanup utility that comes built into Windows not satisfactory?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

But spamming your blog is a good reason, right? ...Right?

sound of crickets

;p

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

You need a data structure that can hold anonymous (unnamed) objects of the struct. For an unknown number of records, I'd suggest a linked list. Here's something to get you started:

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

struct record
{
    char *name;
};

struct node
{
    struct record data;
    struct node *next;
};

static struct node *list = NULL;

int main(void)
{
    struct node *temp;
    char line[BUFSIZ];

    // Build the list from user input
    while (1)
    {
        fputs("Name: ", stdout);

        if (fgets(line, sizeof line, stdin) != NULL)
        {
            // Clean up the newline character
            line[strcspn(line, "\n")] = '\0';

            if (line[0] == '\0')
            {
                // A blank name stops input
                break;
            }

            temp = malloc(sizeof *temp);

            if (temp != NULL)
            {
                // Populate the record and prepend it to the list
                temp->data.name = _strdup(line);
                temp->next = list;
                list = temp;
            }
            else
            {
                perror("Error creating record");
            }
        }
        else
        {
            // Error or EOF stops input
            break;
        }
    }

    // Display for testing purposes
    for (temp = list; temp != NULL; temp = temp->next)
    {
        printf("%s\n", temp->data.name);
    }

    // Clean up our dynamic memory
    while (list != NULL)
    {
        struct node *next = list->next;

        free(list->data.name);
        free(list);

        list = next;
    }

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

It's generally frowned upon since reverse engineering has historically been used for black hat purposes. Further, due to some sticky legal restrictions, I can easily see it being troublesome to have a course focus on it.