Dynamic array allocation problem

Hey guys. I'm having a problem with dynamic strings.
This is my code for entering words into an array of strings called "table":

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
#include <iostream>
#include <cstring>

using namespace std;


int main()
{
    char **table;
    int *length;    //pointer to an array in which the word lengths will be placed
    int nof_words;

    cout << "Enter the number of words: ";
    cin >> nof_words;

    table = new char*[nof_words];
    length = new int[nof_words];

    for(int i = 0, temp; i < nof_words; i++){  //this loop is a system which
                                               //enters words into a "table"
        cout << "Enter the length of a " << i + 1 << ". word: ";
        cin >> temp;
        if(temp){
            length[i] = temp;
            table[i] = new char[temp];
            cout << "Enter the word: ";
            char word[temp + 1];
            cin >> word;
            while(strlen(word) > (unsigned)temp){
                cout << "Word is too long, try again: ";
                cin >> word;
            }
            table[i] = word;
        }
    }

    for(int i = 0; i < nof_words; i++)
        cout << table[i] << endl;


    delete [] table;
    for(int i = 0; i < nof_words; i++)
        delete [] table[i];
    delete [] length;

    return 0;
}


However, whatever words I enter I always get some wild output such as:

'xG
'xG
'xG


What gives?
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
#include <iostream>
#include <cstring>

using namespace std;


int main()
{
    char **table;
    int *length;    //pointer to an array in which the word lengths will be placed
    int nof_words;

    cout << "Enter the number of words: ";
    cin >> nof_words;

    table = new char*[nof_words];
    //length = new int[nof_words]; ///not required!

    for(int i = 0, temp; i < nof_words; i++){  //this loop is a system which
                                               //enters words into a "table"
        cout << "Enter the length of word no. " << i + 1 << ": ";
        cin >> temp;
        if(temp>0){ /// >0
            //length[i] = temp;
            table[i] = new char[temp+1]; // temp+1
            cout << "Enter the word: ";
			cin >> table[i]; ////// use a loop (or getline) for not overflowing 
            /*char word[temp + 1];
            cin >> word; /// how long will it take ?? overflow may occur!
            while(strlen(word) > (unsigned)temp){
                cout << "Word is too long, try again: ";
                cin >> word;
            }
            table[i] = word;*/
        }
    }

    for(int i = 0; i < nof_words; i++)
        cout << table[i] << endl;


    //delete [] table; //not here
    for(int i = 0; i < nof_words; i++)
        delete [] table[i];
	delete [] table; //here
    //delete [] length;

    return 0;
}
Thank you kind sir. =)


Line 17:

Why is it not requied? How else does a program know how many ints I'm gonna store? Wouldn't some problems eventually occur?
Last edited on
your program works without "length" variable.
you are providing char array dynamic size by "temp+1"
a
'\0'
is automatically appended after all words.


in your code you used
1
2
for(int i = 0; i < nof_words; i++)
        cout << table[i] << endl;

to print the words. without mentioning "length" variable.
so how the program knows where to stop ?

and if you enter a word of lesser length - it still works!

EDIT: be careful of line 28 cin >> word;, extra length can corrupt your system !!
Last edited on
Ah, I see. I must have forgot what I wanted to use the "length" variable for.

Thank you for help!
Topic archived. No new replies allowed.