why is my program crashing

closed account (EAk1vCM9)
Hi guys so I stored a file into an array of char pointer and allocated memory to it and right now Im trying to make the words lower case and I ran into a problem.
whenever my program gets into the loop I commented out it makes my program crash and im not sure why,considering one of my peers has the same exact line of code but for him it works. So why doesnt it work for me?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
  #include <iostream>
#include <fstream>
#include <cstring>
#include <cctype>

using namespace std;



const int SIZE=23907;
int main() {


	ifstream infile,infile2;
	char * wordFile[SIZE];
	char temp[16];


	infile.open("unsorted_words.txt");

	if(!infile)
	{
		cout<<"error opening file" <<endl;
	}
	else
	{
		for(int i=0; i <SIZE; i++)

		{

			infile >> temp;
			wordFile[i]= new char[strlen(temp)+1];

			strcpy(wordFile[i],temp);
			cout <<wordFile[i] <<endl;

		}
		for( int i =0; i <SIZE; i++)
		{
		/*	for(unsigned int j=0; j < strlen(wordFile[i]); j++)
			{
			wordFile[i][j]= tolower(wordFile[i][j]);
			}*/
		}

	}





	return 0;
}
When I tried that, using the data file which I have, it failed when the word "transcontinental" was encountered - it is too large for the buffer char temp[16] so I increased the size to 50. (This is why in C++ the use of std::string is preferred instead of character arrays).

The rest of the code - the code should be checking whether or not the file access is successful - there's no point looping 23000 times if the file read failed after just three words or something. Make use of the actual count in the second loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <fstream>
#include <cstring>
#include <cctype>
#include <iomanip>

using namespace std;

const int SIZE = 23907;

int main() 
{    
    char * wordFile[SIZE];

    ifstream infile("unsorted_words.txt");
    
    if (!infile)
    {
        cout<<"error opening file" << endl;
        return 1;
    }
    
    int count = 0;  // keep track of how many words have been read
    char temp[50];  // don't know how long a word might be
        
    while (  count < SIZE && infile >> temp)
    {
        // only enter loop body after successfully reading a word from the file
        
        wordFile[count] = new char[strlen(temp)+1];

        strcpy(wordFile[count],temp);
        
        cout << setw(8) << count << setw(20) << wordFile[count] << endl;
        
        count++;             // increment number of words read
    }
    
    cout << "\nNumber of words read from file = " << count << '\n';
    
    for ( int i = 0; i < count; i++) // loop based on how mant words were actually read:
    {
        for (unsigned int j=0; j < strlen(wordFile[i]); j++)
        {
            wordFile[i][j] = tolower(wordFile[i][j]);
        }
    }
 
    cout << "\nDone\n";
    return 0;
}
Last edited on
closed account (EAk1vCM9)
Ah I see, yea I didnt realize that my file had words more than 16 character thanks for the help !
Topic archived. No new replies allowed.