For that, the typical approach is to #include <algorithm> and write:
mInventory.erase(
std::remove_if(
mInventory.begin(),
mInventory.end(),
[](const auto& item) {...
Type: Posts; User: laserlight
For that, the typical approach is to #include <algorithm> and write:
mInventory.erase(
std::remove_if(
mInventory.begin(),
mInventory.end(),
[](const auto& item) {...
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...
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.
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....
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...
To understand the problem, let's go back to a simple example:
#include <stdio.h>
void foo(int x)
{
x = 123;
}
int main(void)
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.
...
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...
What does this do?
if (strcmp(header, "x y\n") != 0)
{
return -1;
}
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?
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...
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.
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...
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[],...
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...
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...
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...
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...
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...
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?
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...
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...
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...
You forgot the part where you tell readers what you are trying to do and how does it not work.
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,...