Search:

Type: Posts; User: laserlight

Page 1 of 20 1 2 3 4

Search: Search took 0.17 seconds.

  1. For that, the typical approach is to #include...

    For that, the typical approach is to #include <algorithm> and write:

    mInventory.erase(
    std::remove_if(
    mInventory.begin(),
    mInventory.end(),
    [](const auto& item) {...
  2. I think you need to realise that range-based for...

    I think you need to realise that range-based for loops are meant for those times when you want to iterate over the entire container (or range, etc). You seem to be in the habit of reaching for them...
  3. It's mInventory[choice - 1] because you numbered...

    It's mInventory[choice - 1] because you numbered the choices starting from 1. So you would write get<0>(mInventory[choice - 1]) to get the Item itself.
  4. It sounds like the problem is that you're not...

    It sounds like the problem is that you're not actually using the item choice, i.e., you're just looping through the whole inventory after the choice has been made. What I might do is this:

    1....
  5. It is not wise to have the destination array...

    It is not wise to have the destination array smaller than the source array.

    But you may not need an auxiliary array, e.g., I think you might want to do something like this instead:

    void...
  6. To understand the problem, let's go back to a...

    To understand the problem, let's go back to a simple example:

    #include <stdio.h>

    void foo(int x)
    {
    x = 123;
    }

    int main(void)
  7. Replies
    16
    Views
    382

    Oh, but now that's something else. It is not...

    Oh, but now that's something else. It is not fgets itself that returns -1: it is the code in the body of the read_coords (or read_file) function that returns -1, if fgets returns a null pointer.
    ...
  8. Replies
    16
    Views
    382

    Yes. Do you understand why I put it there, and...

    Yes. Do you understand why I put it there, and what the significance of the strings are?

    The easiest solution to your problem right now is to delete that code, but it is more important to...
  9. Replies
    16
    Views
    382

    What does this do? if (strcmp(header, "x y\n")...

    What does this do?

    if (strcmp(header, "x y\n") != 0)
    {
    return -1;
    }
  10. Replies
    16
    Views
    382

    Great. So now you need to check the file content:...

    Great. So now you need to check the file content: is it really what you expect it to be? For example, could it be tab separated instead of space separated?
  11. Replies
    16
    Views
    382

    Well, I wrote the code in post #2 as an example...

    Well, I wrote the code in post #2 as an example intended to be adapted, so I did not compile and test it, hence it is possible that it might contain a bug or two. I decided to write a program to test...
  12. Replies
    16
    Views
    382

    No, it represents the maximum number of...

    No, it represents the maximum number of coordinates that will be read from the file. Ideally this will be equal to the number of coordinates in the file.
  13. They are different pointers. numPtr1 points to...

    They are different pointers. numPtr1 points to num and numPtr2 points to numPtr1, i.e., the value of numPtr1 is the address of num and the value of numPtr2 is the address of numPtr1. So, what you...
  14. Replies
    16
    Views
    382

    That's a problem: your file format contains a...

    That's a problem: your file format contains a header, but you're reading it as if there was no header.

    I might suggest parsing it like this:

    int read_coords(FILE *fptr, struct Coord coords[],...
  15. Perhaps you meant "theoretically" rather than...

    Perhaps you meant "theoretically" rather than "practically". In practice, you of course do not have unlimited space since it is limited both by hardware and the fact that the space allocated needs to...
  16. Replies
    3
    Views
    190

    You probably want to declare the array pointer...

    You probably want to declare the array pointer earlier: makes no sense to declare it in that inner loop in the middle of parsing since you want to parse the current item and assign it to the relevant...
  17. Replies
    3
    Views
    190

    malloc(sizeof(char)) allocates space for exactly...

    malloc(sizeof(char)) allocates space for exactly one char, so if you're trying to use it as a string, it can only store an empty string. But then you overwrite it with strdup, so you end up with a...
  18. Replies
    16
    Views
    420

    You are mistaken. result is an array of 200 char,...

    You are mistaken. result is an array of 200 char, not a pointer to char. It is true that in many cases it is implicitly converted to a pointer to its first element, but in this case that does not...
  19. Replies
    16
    Views
    420

    Ugh, hope you get that figured out. Between my...

    Ugh, hope you get that figured out. Between my personal laptop and my work laptop I'm effectively triple booting Mac, Linux, and Windows hahaha


    That's a good point, but it does involve a...
  20. Replies
    16
    Views
    420

    Since deathmetal appears to want to loop and...

    Since deathmetal appears to want to loop and seems to have a maximum length in mind, wouldn't it make more sense to use snprintf if you're taking that approach rather than just concatenating?
  21. Replies
    16
    Views
    420

    You must have missed the part where I explained...

    You must have missed the part where I explained that it is less efficient than deathmetal's original code, which itself is not too bad where efficiency is concerned. If the aim is to be efficient...
  22. The problem is that fwrite writes the struct...

    The problem is that fwrite writes the struct object member-wise, so if it has a pointer member, it writes the value of the pointer, i.e., an address, not what the pointer points to. You could change...
  23. Replies
    16
    Views
    420

    I might suggest something like this: #include...

    I might suggest something like this:

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

    char *join_by_char(char *dest, size_t dest_maxlen, const char *left, char glue, const char *right)
    {
    size_t...
  24. Replies
    7
    Views
    327

    You forgot the part where you tell readers what...

    You forgot the part where you tell readers what you are trying to do and how does it not work.
  25. You start off with n elements as the size of the...

    You start off with n elements as the size of the array. If there are any duplicates, after removing them, the size of the array would surely be less than n, but you did not update the value of n,...
Results 1 to 25 of 500
Page 1 of 20 1 2 3 4