0

I have a program that uses getline to populate an array of strings. When I run this program, it outputs the correct values from the getline(cin) and then the program crashes, issuing the error messege "Process returned -1073741819 (0xC0000005)" I understand that this code has to deal with accessing memory locations. I have tried using pointers, but everything that I have tried has caused the program to crash in the same manner. Can you please provide advice on what I can do to fix this issue?

I am using Code::Blocks for this program on windows 10

edit: I am putting the strings in an array to be tested for the number of similar words

    int wordCount = 0;
    cout << "Enter the number of words in your email: ";
    cin >> wordCount; //size of array determined by wordCount, which is retrieved from the user
    string testEmail[wordCount] = { }; // declaring and initializing the array
    cout << "Enter the email you wish to test: ";
    getline(cin >> std::ws, testEmail[wordCount]); // this will populate the array from user input
    cout << "The email that will be tested is: ";
    for(int i=0; i<wordCount; i++) // for loop used for testing
        cout << testEmail[i] << " ";
cmbtmstr
  • 25
  • 6
  • Please provide a [mcve]. – Paul R Apr 26 '20 at 19:27
  • 1
    What do you expect `getline(cin >> std::ws, testEmail[wordCount]);` to do? Discuss this one with [your Rubber Duck](https://en.wikipedia.org/wiki/Rubber_duck_debugging). – user4581301 Apr 26 '20 at 19:29
  • Usage of codeblocks (or not) has nothing to do with the problem. – Jesper Juhl Apr 26 '20 at 19:37
  • Getline would get the input from the user. When it detects a white space, it will store the word as a string in the array. It would do that for the rest of the input. I have never used getline to populate an array and I'm not sure about the properties of doing that with strings and creating new entries at the white spaces. – cmbtmstr Apr 26 '20 at 19:45
  • 3
    That's just not how `getline` works. That line discards leading whitespace, and then reads a single line into `testEmail[wordCount]` which is out-of-bounds. You can't program by throwing stuff together without knowing what you're doing and hoping that it works. Learn the language thoroughly with something like a [good book](https://stackoverflow.com/q/388242/9254539). – eesiraed Apr 26 '20 at 19:51
  • I think the dimension for the array wordCount should be a constant expression too? – octopus Apr 26 '20 at 20:06

1 Answers1

0

0xC0000005 is the error code for an assert. Given your code the most likely assert to fire is validation that your array indexing is valid. As already pointed out in the comments testEmail[wordCount] is out of bounds as C++ arrays are index 0 based the "count" is one past the last valid index. The application is likely asserting this is invalid at runtime.

DClyde
  • 96
  • 2