program crashes on closure.

ok the HW assignment i was working on i got it back and it crashed on closure. guess why cause i had put the cin.get() at the end and always just exited by clicking x so it just never occured to me it wasnt working. so of course the teacher hit enter and it died trying to close.

so i have no idea why a program would execute just fine and THEN die trying to close. the little bits of commented out code here and there was me trying to fix the problem where it reads the first bit of data in the file then skips over the second one so thats why.

but mainly why does it die closing?

(yes i know this is my whole assignment but eh not sure what part is killing it)

It should be if (elementsize >= arraysize). That is because if the for loop was exited because you read in the maximum number of elements, then elementsize will be 99 (you always stop when the counter is the same as the size of the array because for a 99 element aray only posistions 0-98 are valid). Since the arraysize is also 99, then you know if the input filled the array if the elementsize is equal to the arraysize when you are done.

Don't forget that you cannot read 100 elements into your array since it was declared to hold only 99. Also don't forget that the 99th element will be at position 98, and position 99 is illegal.

ok this is what i did i made a for and just told it read the whole file. then after wards i told it to see if elements is more than arraysize. that works then i told it print the value of count to make sure it passed ok and its a -1 but it crashes on closing.

the rest of the program works fine it tells me if there is a file, if its empty and if there is enough spaces it adds ok. with arraysize being set to 100.

You cannot access an array outside its bounds. If your array is declared to have 99 elements, then you can only access elements 0 through 98. If you try to access the element at position 99, you invoke undefined behavior, meaning anything can happen, including a crash.

Try to understand which indexes of the array are being accessed by your code so you can see why your code is wrong and causes a crash. Look at my suggestion and see where you did the opposite of what I suggested, and try to understand why that didn't work. Also go back and try to understand swoopy's first suggestion and see why implementing it would fix your crash.

The bottom line is you need to pay attention to which indexes you use so that you don't go over the array bounds, otherwise you will get a crash, or worse - a silently incorrect program.

oh ok i got it to work i beleive i got confused cause i kept changing numbers around since at one point or another it seemed to skip the second element when printing it and that was in the middle of me trying to figure out why it was crashing.

although as to whether i understand this completely im going to defently mess with it some more im sure. though it does make sense must make sure elementsize stays lower and never equals arraysize. though i always thought it could equal thats why i was having a hard time. i didnt know you had to set it above intentially cause of that possibly one reason i was way off. which is probably why i removed that equal sign and went in a total opposite direction between that and frustration with it.

also never knew the order of the condition mattered learn something new with that. though makes sense it is an expression and expressions have orders.

and after seeing this i am officially scared if someone ever shows me this using a C++.net array

ok yes i know this thread is old but i think i just got it so it makes sense

i have to test to make sure the elementsize is less than the arraysize followed by making sure i can still read in the file because order matters like any expression and it might read in something outside of its bounds causing said silent error or other undefinedness if it were to read the file first instead of making sure it was in the correct bounds

however you could terminate and there could sitll be more files to be read but elementsize increments past the arraysize and kills the loop so THEN you have to make sure that arraysize and elementsize dont equal.