I am not a programmer, but I've been given a programmer's task, so forgive any crudeness in my code. I need to take a text file and read it to an array. The text file looks like this:
btw, why are you solving programing tasks if you're not an programer?
I do wonder the answer to that question myself sometimes. lol
Why would it start printing out everything up until [6] if the array was not initialized properly?
My file currently reads:
1 2 3 4 5 6 7
J,Jackson County
R,Riverside
S,San Bernardino
W,Wolf River
S,Sean
B,Ben
S,Sam
What the code spits out is:
1 2 3 4 5 6
[J] Jackson County
[R] Riverside
[S] San Bernardino
[W] Wolf River
[S] Sean
[B] Ben
Followed by a crash. If I remove "S,Sam" the code continues on as normal.
Also, isn't initializing an array of Array[0] the same as an array with one spot? I would figure that since I can call on Array[0] to spit out something, that initializing Array[0] is OK.
Changing CountySize to 1 crashes the program outright.
"isn't initializing an array of Array[0] the same as an array with one spot?"
No.
On line 12 & 13, you're attempting to access the second std::string of the first row (which doesn't exist). Since there's no second object, you're entering unknown memory; possibly memory that your program doesn't own.
But would that actually cause the array to crash after a few inputs? Wouldn't it crash it outright?
I changed string CountyArray[CountySize][1] to string CountyArray[200][1] and it works. I think I will just go with it for now, but is it odd that using a dynamically changing int as the size of your array would crash it, or is that expected?
"But would that actually cause the array to crash after a few inputs? Wouldn't it crash it outright? "
No. Sometimes, the operating system doesn't instantly detect segmentation violations. By the time the OS detects something, you could've written into 2-4 locations.