#(flimsynimsy) i need some simple c++ help in vc PRETTY PLEASE

170 messages · Page 1 of 1 (latest)

fallow totem
#

yo, im taking a data structures class and my first assignment has to do with templating and some simple functions to make, but im sorta stuck on how to do some things. I'll be in vc!

maiden raftBOT
#

(flimsynimsy) i need some simple c++ help in vc PRETTY PLEASE

maiden raftBOT
#

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.

civic urchin
#

i can probably help a bit but not available to vc if you want to just ask whatever questions you have

fallow totem
#

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

civic urchin
#

i assume Container is like an array based list or something?

fallow totem
#
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

civic urchin
#

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?

fallow totem
#

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

civic urchin
#

you don't need to empty out the container[i] because you can just overwrite it with container[i+1]

fallow totem
#

true

#

i dont understand how everything shifts to the left though since im adding 1

civic urchin
#

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

fallow totem
#

ohhhh right

fallow totem
civic urchin
#

i would wager if you print out container[_size] it would still return the value that used to exist instead of being null

fallow totem
#

ill try it out

civic urchin
#

the last element should be at container[_size-1] because 0 index

fallow totem
#

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?

fallow totem
# civic urchin and then you empty out the very last element that exists

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;
        }
    }

}
civic urchin
#

oh wait no that should be fine how you have since the last element would be copying the termination back one too

fallow totem
#

so there's no need to clear out the last element?

civic urchin
#

hold on give me a second to get back into c++ mindset lol

fallow totem
#

lol

#

i think it's fine

#

because it's just being overrided

#

so there's no need to clear anything

civic urchin
#

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)

fallow totem
#

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;
}
civic urchin
#

should be able to just return _size == 0

fallow totem
#

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

civic urchin
#

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

fallow totem
#

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

civic urchin
#

remember arrays are 0 indexed

fallow totem
#

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";
civic urchin
#

oh its because of the empty return

fallow totem
#

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

civic urchin
#

you should throw new std::string

#

because that's what the comment says

fallow totem
#

wdym where

civic urchin
#

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

fallow totem
#

i dont get what it means by "throw the string"

civic urchin
#

literally instead of return use throw

fallow totem
#

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?

civic urchin
#

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");

fallow totem
#

yea im using namespace

#

what's the difference between throw "oob"; and throw new string("oob");?

#

is it just better practice?

civic urchin
#

without the string i believe it will create a bare char[] instead

#

while a string is a class

fallow totem
#

oh ic

#

also, dont i need to add a return; after the throw?

civic urchin
#

no, the throw ends the execution

fallow totem
#

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

civic urchin
#

that is visual studio, not vscode

fallow totem
#

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

fallow totem
#

ohhhhh wait

#

i think it's because of the throw command that the error is happening

civic urchin
#

probably, it should be in a try catch

lavish baneBOT
fallow totem
#

i dont think that's what he's asking for though

civic urchin
#

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

fallow totem
#

wait maybe my professor wants the oob thing to throw an exception like that?

civic urchin
#

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

fallow totem
#

the thing is

#

the one thing he doesn't have an example for is this 😂

civic urchin
#

email him and ask! or a TA

fallow totem
#

uhh

#

i sorta gotta submit this in 10 minutes lol

civic urchin
#

oh

#

well then you should not have procrastinated

fallow totem
#

true

civic urchin
#

maybe try putting the return into an else and see if that satisfies the ide

fallow totem
#

i tried to

civic urchin
#

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

fallow totem
#

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?

civic urchin
#

its because T container[N]; isn't initialized with a value, but that shouldn't stop you from compiling

fallow totem
#

yea idk what i clicked

#

maybe it's related to this

civic urchin
#

at the top click the green play button

#

there's one that's attached to the debugger and one that launches without the debugger

fallow totem
#

i do

#

this is when i click the hollow green play button

civic urchin
#

that likely means something with the amount of memory allocated to the container array was not enough

fallow totem
#

but like- why now all of a sudden

civic urchin
#

when you add_item is it trying to add another int to an already full container?

fallow totem
#

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

civic urchin
#

oh yeah it should be _size-1

fallow totem
#

alright well

#

tysm for the help boothin!