deceptikon 1,790 Code Sniper Team Colleague Featured Poster

For example, if key 0 contains a vector containing a b c d and key 1 contains a vector containing 1 2 3 4, would there be any way to output a 1 b 2 c 3 d 4, instead of a b c d 1 2 3 4?

std::map doesn't readily support that in an elegant manner. Obviously you could repeatedly iterate through all of the keys, maintain an index to the value, and output the element at that index. But this seems like more of a band-aid born from the wrong data structure choice.

So let's take a step back and figure out exactly what the purpose of this map is. Can you clarify?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I want some questions that aren't hopelessly vague. But apparently getting what you want is a rare occurrence.

Can you be more specific?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Rather than bind the combo box values directly to your table, built a list in memory with the contents of the table and any other extra values you want. Then bind to the list.

Alternatively, you can adjust your select statement for ddtable to include selection of the default value from the other table so that it's part of the result and no special logic is needed in your program.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Which "external tool" ?

The one you created. That whole process was to make a new external tool to cmd.exe and link it to the output window.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Yes. Depending on the version of VS, you may have a setup project available either built in or from a plugin. Otherwise, something like InstallShield LE, InnoSetup, or WiX can offer freely available installers to package your solution. I'm not a huge fan of InstallShield LE, but for simple installers it's fine and easy to use. For complex installers I prefer WiX, though the learning curve is astronomical.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Please post the whole of your relevant code. My guess would be the counter is being incremented incorrectly, or the condition of the loop doesn't check the counter the way it should.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Trick didn't work, it opens in cmd.exe, not in output window :|

It works for me. The "trick" might be version dependent. I tested it on Visual Studio CE 2013. You might also not have done something needed, like the output window check box in your external tool. I'm reasonably sure I listed all of the steps I followed.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

The auto keyword is compiler magic from C++11 that interprets the type of a variable from its initializer. It's not a dynamic type or anything, so as an example if you're using v.begin() as the initializer, auto will choose the type of the collection's non-const iterator (in this case vector<vector<double> >::iterator). Really the only difference is you don't have to manually use the type name or worry about changing any use of it if the if the type changes.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Wow. Just...wow.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

vector<vector<double> >::iterator and vector<double>::iterator, respectively for the nested loops. Just like you're doing with miter, which is why I didn't mention it...

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

is there no such thing for Windows?

It depends on the IDE being used and how it handles program output. I'd be surprised if there weren't an IDE that allows you to display output in a tool window like XCode. Typically you'd have your program open in a new console window for testing, is that not sufficient for your needs?

That said, you can do this with Visual Studio after a fashion.

  1. In VS, navigate to Tools | External Tools.
  2. Click the Add button.
  3. Under the Title field, type something like Console.
  4. Under the Command field, type cmd.exe.
  5. Under the Arguments field, type /c $(TargetPath)
  6. Check the Use Output window option.
  7. Hit OK to save the new tool.

Now Console is available from the Tools menu, and you can add a button to your toolbar that activates it easily to execute your latest build and redirect stdout to the Output window.

There may be an easier way to do this, but I don't know it.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Forget that you're dealing with miter->second for a moment. How would you display the contents of a vector? We know cout doesn't have an overloaded << operator for collections, so you're stuck with some variant of a loop. This comes immediately to mind:

for (auto it = v.begin(); it != v.end(); ++it)
{
    cout << *it << ' ';
}

Since this is a vector of vectors, you have a nested loop:

for (auto row = v.begin(); row != v.end(); ++row)
{
    for (auto col = row->begin(); col != row->end(); ++col)
    {
        cout << *col << ' ';
    }

    cout << '\n';
}

Now replace v with miter->second, as that's your reference to the mapped vector object.

The idea here is to start with something you understand, then add complexity until you have the final solution that you're looking for.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I've never used C++/CLI, but it looks like std::string is a distinct type from System::String and presumably Console::WriteLine only works with the latter as it is a .net method and std::string is a C++ class.

Correct. System::String^ is a managed reference type which is not directly compatible with std::string.

Presumably C++/CLI offers some way to convert between the two string types, but I imagine in this case it'd be easiest to just either only use C++ strings and C++ IO functions or .net strings and .net IO functions.

Yes, and agreed. There's a method to convert between the two, but it's awkward enough that it should be avoided where possible.

Results in: Error 1 error C1083: Cannot open include file: 'stdafx.h': No such file or directory. The funny answer I got is "just delete it"... but it's not even there and it isn't even included.

Visual Studio is stupid like that. If you generate a console project, stdafx.h will be generated as well, along with a switch in the project settings saying to expect it. For this reason I strongly recommend starting with an empty project instead, because it saves you from having to dig into project settings to fix things like this as well as delete any autogenerated kruft.

Is there no way to program standard C++ CLI applications without having this ".NET CLR"?

No.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

In socratic fashion, I'd ask where does x point at the start of the second loop?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

That is a fantastic solution

Eh, I'm not a fan of it. It's tied heavily to fgets, requires a special buffer that can easily cause name conflicts, lacks flexibility in where it can be called, and ignores error handling. It's inefficient in general, and most importantly, neglects that the newline handling of fgets is there for a reason. Instead of calling characters beyond the length of your buffer "junk" and throwing them away, robust code will at best allow them somehow and at worst gracefully refrain from actively deleting data.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Please define your terms. I've seen a number of different, conflicting, and overlapping definitions of "programmer" and "software engineer". It's difficult to answer your question without first understanding what those terms mean to you.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

hes doing codefirst so the code will generate the database

That's an unwarranted assumption. Code first doesn't unconditionally generate a database; how you've defined your context specifies what EF does with the model to maintain database integrity.

where it does that depends on the connection string

Indeed, it does. But would you rather direct an apparently new programmer to the location of the connection string in a project that you haven't seen, or simply get a URL to the tutorial being followed so you have a reasonable expectation of what the project looks like? ;)

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Depends on what tutorial you've been following. You can code against an existing database (in which it's your job to create it) or you can code against a new database where your context will attempt to create the database when needed. In both cases, of course, you need something to host the database like SQL Server.

So...what tutorial are you following so we have a better idea of what your project looks like?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

m==textBox1->Text;

Two things:

  1. m is declared as std::string, while the Text property is declared as System::String^. The two types are not directly interchangeable.

  2. == does a comparison, not an assignment.

Also note that m only exists inside the event handler, so if you need it beyond that, it should probably be a private member or something.

ddanbe commented: Your indeed worth, to be called a "Code Sniper" +15
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

arrayofstr = malloc(9 * sizeof (char*)) is misplaced. Move it outside of your first loop.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Yes, but it is imperative that the method accepts a path as a parameter.. I cannot change it.

  1. Get the stream.
  2. Create a file from that stream.
  3. You now have a path.
  4. Profit.
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

The file is embedded in your assembly, it has no path. If you want a file you can work with outside of the resource interface, then consider GetManifestResourceStream from the assembly object for a little more control.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Clickie to learn about the difference between a pointer to a function and a pointer to a member function. They're not even close to the same thing.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Do you enjoy programming? If yes, why?

I like solving problems, creating something useful out of nothing but an idea, and I seem to be good at it.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Most image processing libraries let you do the work using memory streams. I'd start by looking through NuGet for popular libraries to help.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

what about Static functions?

What about them? The only similarity is the static keyword.

How will you explain that thing?

An arbitrary decision by the language designers. Sometimes the answer to "why" is there's no good reason, that's just how things evolved.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

But I am declaring it in the class right?

Yes, but a declaration is not necessarily a definition. In the case of non-const static data members, the declaration is unconditionally not a definition. Hence why you need to provide a definition outside of the class.

Here, they are saying that by dafault, it will be zero if not other value in present.

True, but the object itself must exist. These are two different things.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

It's not really that advanced, but conceptually, structures are stored and accessed as a punned array with padding between the "objects" to facilitate faster individual access by the CPU (which some compilers can optionally change or remove to alter or make consistent the storage size).

The template, which I interpret to mean the order and type of members, is handled by the compiler's symbol table. It's unlikely to be retained in compiled object code.

Structure accesses, such as mystruct.member is your compiler magic and under the hood it's turned into something like *(type*)((char*)mystruct + offset) where offset is the byte index of the member in the structure memory block from the symbol table.

As far as the CPU is concerned, structures aren't special at all. The only real difference from a scalar variable and a structure member is the initial offset calculation to find the member in a heterogeneous "array".

Once again, all of this is conceptual in nature. Compilers are free to implement structures however they want, provided all of the rules are met and public interfaces exhibit correct behavior. But what I described above is the most immediately obvious and straightforward potential implementation.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Yep, that looks like a bug in the Markdown processing to me. Markdown can be a little tricky when it comes to lists and code blocks.

In this case, you can double tab the code to nest it underneath the third list item and the block is formatted correctly, or use a dummy line after the list to fully separate it from the code block and continue to use the regular single tab.

Example 1:

  1. One
  2. Two.
  3. Three.

    <?php
    
        Dooby dooby doo
    
    ?>
    

Example 2:

  1. One
  2. Two.
  3. Three.

Blah blah blah.

<?php

    Dooby dooby doo

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

...so you said the same thing with more words, and it's still not enough information. Please post an example of the expected output of this program.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Quicksort is implemented in C with the qsort() function.

The interesting thing is the C standard doesn't in any way require that qsort be implemented as quicksort, nor does it impose any performance requirements in the same way that C++'s sort function does. The C++ standard retains this lack of guarantees for the C library functions.

You can expect it to be efficient, of course, given that implementors are not stupid. As an example, I chose heapsort as the qsort implementation for simplicity of implementation and guaranteed time complexity across the board. From stdlib.c:

/*
    @description:
        Sorts an array of n objects, the initial element of which is
        pointed to by base. The size of each element is specified by size.
*/
void qsort(void *base, size_t n, size_t size, _cmp_func_t cmp)
{
    _sort(base, n, size, cmp);
}

And from _sort.c:

#include "_sort.h"
#include "_system.h"
#include "stdint.h"
#include "stdlib.h"
#include "string.h"

static void do_heap(void *temp, uint8_t *base, size_t i, size_t n, size_t size, _cmp_func_t cmp);

/*
    @description:
        qsort-friendly implementation of heapsort for guaranteed time complexity.
*/
void _sort(void *base, size_t n, size_t size, _cmp_func_t cmp)
{
    void *temp = _sys_alloc(size); // The size of items is unknown, so we must allocate
    uint8_t *p = (uint8_t*)base;   // Pun the base into a byte array
    size_t i = n / 2;

    // Heapify the array
    while (i-- > 0)
    {
        do_heap(temp, p, i, n, size, cmp);
    }

    // Extract the heap max and place it in sorted position …
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I'm unable to reproduce that output with the posted code.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Not enough information, can you be more specific?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

k = n & andmask;

The bitwise AND will set each corresponding bit to 1 if the same bit in both arguments is 1. Otherwise the bit will be set to 0. Since andmask only has at most one bit set to 1, the purpose of this statement is to check the bit at position j to see if it's set.

k will be zero if both bits are not set and nonzero if both bits are set.

k == 0 ? printf("0") : printf("!");

And this statement prints the result of whether the bit is set or not. It's equivalent to the following:

if (k == 0)
{
    printf("0");
}
else
{
    printf("!");
}
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

void print_all(int [][][],int,int,int);

All but the first dimension requires a size.

print_all(arr[][2][3],degree,row,col);

No indexing is required here, just use arr as the argument.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

char names[100]; - its just a character array which can hold 99 characters and the last one will be '/0'

Yup.

char *names[100]; - All the 100 array elements are character pointers and are pointing to the starting element of 100 names somewhere in the memory location.

Close. Depending on where this array is defined, the pointers could point to nowhere (ie. null) or some unpredictable addresses by default. It's not quite correct to say that they point to names unless you've already allocated memory to the pointers and populated that memory with strings representing names.

if i want to print all those names cout<<*names[i] where i iterate from 0 to 99 will work?

Close. Dereferencing the pointer will give you the first character that it points to, so an iteration loop (assuming the pointers actually point somewhere you own) would be:

for (int i = 0; i < 100; i++)
{
    cout << names[i] << '\n';
}

The reason this works is the << operator knows how to interpret a pointer to char as a string and will do the "right" thing.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

First and foremost, the argv array always ends with a null pointer, so there's a possibility that either argv[1] won't exist, or it's NULL. You really need to check argc and act accordingly:

if (argc > 1)
{
    // argv[0] and argv[1] are safe to reference
}

Second, malloc(strlen(argv[1])) has an off by one error. strlen will return the number of characters up to but not including the null termination character, so you're not allocating enough memory to hold the complete contents of the string and its null character at the end. Add 1 to the result of strlen:

char* name = malloc(strlen(argv[1]) + 1);

Finally, malloc can fail and return a null pointer. You need to check this as well and act accordingly. General best practice is to free any memory you allocate as well. The full corrected code would be:

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

int main(int argc, char* argv[])
{
  if (argc > 1)
  {
    char* name = malloc(strlen(argv[1]) + 1);

    if (name != NULL)
    {
      name = strcpy(name, argv[1]);
      printf("%s\n", name);
      free(name);

      return EXIT_SUCCESS;
    }
    else
    {
      perror("Memory allocation failure");
      return EXIT_FAILURE;
    }
  }
  else
  {
    fputs("Insufficient arguments provided", stderr);
    return EXIT_FAILURE;
  }
}
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Wheres as,

IN C ...

one ALWAYS needs to return an int value in main, and to code that in explicitly, if the compiler conforms to any standard from C89/C90 forward ...

The clause for omitting a return from main was added in C99 and retained in C11.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Not all compilers are so compliant...

That sounds reasonable on the surface, but has an undertone of irrationality if you think about it. Where do you draw the line for features that are hard requirements in a conforming compiler? If the bar is as low as this, it can be argued that it's impossible to write a valid C++ program because some compiler somewhere will fail to be compliant enough in all of the right arbitrary places.

I'll also ask if you can name one C++ compiler that claims to support any ISO standard, yet doesn't support this particular "feature".

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

And i don“t know why the program returns can't open file.

fopen sets errno on failure, which you can query to find out why. The easiest way here would be to interpret the error code using strerror:

if(!(fd = fopen(buf, "r")))
{
    printf("can't open file `%s`: %s\n", buf, strerror(errno));
    exit(0);
}

Make sure to include <cerrno> or <errno.h> and <cstring> or <string.h>.

It looks to me that fopen doesn't return true or false, it returns a pointer, which is null on fail.

True, but when taken in boolean context, the pointer will be true if not null and false if null, so the code as originally given is fine. From a stylistic standpoint, I prefer your version though. ;)

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

im still working on it i was going to post what i have been working on

Cool. We'll wait. :)

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

We're clearly the devil's spawn, being all objective and fair. ;)

I'd wager this complaint has something to do with not being able to differentiate between enforcing the rules as a moderator and participating in discussions as a member.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Why are you confused? What have you done? What have you considered? What are you having problems with? Did you try anything before running for help?

Simply posting your homework problem with no other substance is not only frowned upon here, it's actually against Daniweb rules.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Is this issued happening because I have "fileName" still open as "objReader"?

Yes.

If this is the case, is there a way to close it while still in my loop?

Well, you could dispose of the reader and break out of the loop, but that's very ugly and prone to error. And I suspect the reason you're asking is you want to continue reading from the reader after moving the file. I'd be more inclined to tweak the logic so that this exclusive access problem simply doesn't exist.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

word= (char )malloc(sizeof(char)100);

word is a completely separate and independent entity from words[i]. Essentially what you're doing here is creating a new pointer, allocating memory to it, then throwing it away (along with losing your only reference to the allocated memory).

words[i] = (char )malloc(sizeof(char) 100);

Here you're still creating a new pointer, but not trying to overwrite it. Instead, you're dereferencing it to get to the original location and overwriting that. This works because the temporary pointer isn't your only reference to the allocated memory.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Yay, my crystal ball still works. :)

By the way, in the future, I am to use a namespace instead, how do I do it?

It's super easy. You create a namespace like so:

namespace MyNamespace
{
    class clock
    {
        ...
    };
}

And use it like so:

MyNamespace::clock myclock;

There are a few extras like being able to nest namespaces and multiple namespaces with the same name get merged as if they were all one, but it's mostly intuitive how things work.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

True. The loop would be cleaner and more intuitive, off the top of my head.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

A loop is simple enough, but you can LINQize it fairly easily:

var everyNth = src.Where((x, i) => i % n == 0);

That gives you the elements, and copying them to another array is trivial.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Coolio. Actually, clock is a horrid name because it's also the name of a function in the C library. Your implementation could very easily expose that even if you don't include ctime, so I'd strongly recommend using either a different name or dropping the class in a namespace.

That's most likely your issue. The compiler sees clock as a function rather than a type, so the declaration is malformed.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

If that helps.

Not really. What would help is paring down your code to the absolute minimum without eliminating the error and posting it here so we can see what's going down. Typically that error occurs when you have a syntax error somewhere above the line that's reporting it.