Iam having a bit of an issue with a function and I was wondering if anyone would be able to help me. The function is meant to read in an entire massive file to a 1d character array, then convert it into a 2d array by the space character.
cout << "Importing dictionary from file...";
char dictstream ;
longint i =0; // index of dictstream
longint w = 0; // word count
int l = 0; // letter count
while (dictstream[i] != 0)
if (dictstream[i] != ' ')
word[l][w] = dictstream [i];
l = 0;
cout << "Complete!";
As soon as the function is called, the program crashes, without even getting to the cout statement at the top of the function. I don't know why this happens as the line before it executes properly. I have tried declaring the function with or without 'void' in the brackets.
Here is where it is called:
cout << "Program will guess the encryption key and decrypt." // this line executes << endl;
dictionaryload(); // seems to crash either here or at the start of the function
char dictstream ;
Chances are, this line is happening before the cout statement even though it is written after. That's a massive amount of memory on top of what you already have, I'd assume it's a stack overflow you're getting.
To fix it, you'll need to use the heap (new and delete) - the stack just isn't cut out for piling up that high in the air without overflowing.
Since you are using C++ I recommend using a std::vector or one of the other standard containers instead of the arrays, for both dictstream and word, and since your sizes are so large I recommend that you use size_t for the index and count variables.
I wrote it the first time using string class, but the program involves a lot of typecasing from a letter in the string, to an integer, and back to the string (thats how the encryption works, it modifies the ascii code based on what type of encryption and the key). I was having lots of unexplained errors with string type, such as values what shouldn't be there and trouble converting the numbers back into string class. I dont know why it was doing this, but a friend suggested I just use character arrays. So I re wrote the entire thing using character arrays (not char*) and it worked properly
What would the 2 values be? would it be like a 1d array of string in that you have an array of words and an array of characters inside each word?
When I removed one of the digits in dictstream (making the array 10x smaller) it did get to the first cout statement, so I would assume that means the issue is the stack overflow, as everyone said before.
Why is it that the variable gets declared before the cout statement, even though it is after it in the code? I was under the impression programs always run sequentially.