#(flimsynimsy) i need some simple c++ help in vc PRETTY PLEASE
170 messages · Page 1 of 1 (latest)
(flimsynimsy) i need some simple c++ help in vc PRETTY PLEASE
Hi I'm AutoThreadBot! Don't mind me, I'll just be adding the helper team to this thread so they can see it. A human will get to you soon.
i can probably help a bit but not available to vc if you want to just ask whatever questions you have
sweet
so i have this rn
im not sure how to pass the datatype in the function
actually nvm, i dont think that's my probelm
im just trying to remove the item from the list, and then shift everything back one
i had an idea to just move the "empty" space in the array onto the end of the list, but i thought maybe i could just recreate the container without the "empty" item in the first place?
made a small mistake with i and j in the second for loop btw
eh, whatever, i think ik a better way
remove first occurrence of item, shift items to fill empty position
this is what remove_item is supposed to do btw
i assume Container is like an array based list or something?
template <class T, int N>
class Container
{
public:
using value_type = T;
void add_item(T item); // output "container full", if add_item cannot add
void remove_item(T item); // remove first occurrence of item, shift items to fill empty position
T get_item(int index); // throw an std::string if index out of bounds
bool empty(); // check if Container is empty
void clear(); // clear all contents , assign value_type
constexpr int size(); // return current number of elements in container
T* begin();
T* end();
private:
int _size = 0; // number of elements in the Container
T container[N]; // a Container of length/size N
}
this is container
so think about it like this, say you find the item to remove, and its at index k
how do you move everything 1 to the left?
well
this is what im doing rn
(or trying to do)
template <class T, int N>
void Container <T, N >::remove_item(T item) {
for (int i = 0; i < _size; i++) {
if (container[i] == item) {
container[i] = T();
//remake the list without the empty gap
for (int j = i; j < _size; j++) {
container[j] = container[j + 1];
}
_size--;
break;
}
}
}
i think this works
oh wait
there we go
you don't need to empty out the container[i] because you can just overwrite it with container[i+1]
and then you empty out the very last element that exists
because you're setting the one before to equal the one to the right of it
ohhhh right
also, is there really a need for this? it outputs correctly rn
i would wager if you print out container[_size] it would still return the value that used to exist instead of being null
ill try it out
the last element should be at container[_size-1] because 0 index
well also, i do subtract the size value as well
so you'll only be able to check by unconventional means
cout << container[_size];
if i do this, it just returns empty
but maybe that's because im removing the value Red , which is in between the two values?
so you're thinking something like this?
template <class T, int N>
void Container <T, N >::remove_item(T item) {
for (int i = 0; i < _size; i++) {
if (container[i] == item) {
//remake the list without the empty gap
for (int j = i; j < _size; j++) {
container[j] = container[j + 1];
}
container[_size] = T();
_size--;
break;
}
}
}
oh wait no that should be fine how you have since the last element would be copying the termination back one too
so there's no need to clear out the last element?
hold on give me a second to get back into c++ mindset lol
lol
i think it's fine
because it's just being overrided
so there's no need to clear anything
yeah i was thinking about char string arrays where they are terminated with a special character, i think technically for safety you'd want to remove the last value, but if the other operations are bounded by _size then it doesn't really matter
(in this case)
ig ill do it for safety
so there's another function they want me to implement too
bool empty(); // check if Container is empty
couldn't i literally just do
template <class T, int N>
bool Container <T, N >::empty() {
if (_size == 0) {
return true;
}
return false;
}
should be able to just return _size == 0
oh ok cool lol
final implementation
T get_item(int index); // throw an std::string if index out of bounds
ill try to do it myself rq
hmm
for the empty() it's like denizen's common mistakes,guide, it parses down to if true return true so just return the comparison directly
true true
this is what im doing for the get_item function btw
template <class T, int N>
T Container <T,N>::get_item(int index) {
if (index > _size){
cout << "Index out of bounds!";
return;
}
return container[index];
}
but i dont think it's working
remember arrays are 0 indexed
oh right
so i gotta check for _size-1
still has problems though
this is what im doing to check it in main
cout << "container_of_strings.get_item()=" << container_of_strings.get_item(3) << "\n";
oh its because of the empty return
yea i fixed it
so im just doing this now return "Index out of bounds!";
but now the if check is broken, so imma print out the index and _size value
wdym where
instead of returning in the if checking for index oob
throw the string instead of returning the string
because that's what it asks you to do lol
i dont get what it means by "throw the string"
literally instead of return use throw
oh lol
template <class T, int N>
T Container <T,N>::get_item(int index) {
if (index > _size-1){
throw "Index out of bounds!";
}
return container[index];
}
so just this?
throw implies an error, and normally you wouldnt throw a string but instead throw an exception
but always stick to what the assignment asks lol
you'd need to throw new std::string("index oob"); or if you're using std namespace then just throw new string("index oob");
yea im using namespace
what's the difference between throw "oob"; and throw new string("oob");?
is it just better practice?
without the string i believe it will create a bare char[] instead
while a string is a class
no, the throw ends the execution
ah
ugh i accidentally enabled this annoying debug thing in vscode now it doesn't even debug properly
idk how to fix this shit
like- it does this
whereas i just dont want it to debug and i want it to tell me the errors
that is visual studio, not vscode
oh yea my fault
vscode
how do i fix it
im getting so annoyed 😭
template <class T, int N>
T Container <T,N>::get_item(int index) {
if (index > _size){
throw new string("Index out of bounds!");
}
return container[index];
}
i dont think this is working
yo @civic urchin im tryna understand how to fix this, any ideas?
ohhhhh wait
i think it's because of the throw command that the error is happening
probably, it should be in a try catch
i dont think that's what he's asking for though
but i dont think it actually HAS to be because c++ lets you do wild shit sometimes and its just your ide getting mad that you're doing bad programming
wait maybe my professor wants the oob thing to throw an exception like that?
this is where it gets kind of hard because i have no idea what youve been taught
i would probably look through his examples and find what he is expecting you to do
email him and ask! or a TA
true
maybe try putting the return into an else and see if that satisfies the ide
i tried to
yeah i got nothing for that, i just tried it and it let me do it here
wait it should just work like that yeah, did you actually try just compiling the program isntead of debugging it
the debugger will catch it because its literally throwing an error, but it will still compile and work as intended if you aren't giving it a value that errors
so how do i fix?
this is what im getting rn
i dont understand this bro
i think im going crazy
how do i compile it?
its because T container[N]; isn't initialized with a value, but that shouldn't stop you from compiling
at the top click the green play button
there's one that's attached to the debugger and one that launches without the debugger
that likely means something with the amount of memory allocated to the container array was not enough
but like- why now all of a sudden
when you add_item is it trying to add another int to an already full container?
well that's why we have a check for it
template <class T, int N>
void Container <T, N >::add_item(T item) {
if (_size >= N) {
cout << "\nContainer full!";
return;
}
container[_size] = item;
_size++;
}
and for some reason it doesn't exit when i press the "enter any key to exit"
ok so the error resides somewhere in here
found it
it's in remove_item
bruh i found the issue
shouldn't have done this
oh yeah it should be _size-1