C++ Add random characters into a string array -
so want create 1000 words length of 5 random characters. in main have word[1000] when try run code, gives me error saying "expression:string subscript out of range". i'm not sure why because thought string arrays 2 dimensional? if tell me why code wrong i'd appreciate it. thanks.
void createtext(string w[], int seed) { char ch; srand(seed); for(int = 0; < 1000; i++) { for(int j = 0; j < 5; j++) { ch = ('a' + rand() % 26); w[i][j] = ch; } } for(int = 0; < 1000; i++) { cout << w[i]; } cout << endl; }
i suppose array w not have 1000 elements, remember here copy of string w[]
. better passing pointer w (string* w)
, have wrong. remember cout writes string out untill reaches '\0'
character, might cause. quick session gdb help:
gdb program ... run bt full
should pinpoint problem. if it's kind of ide, learn how debug in it. valgrind or other memcheck visual leak detector or luke stackwalker show tips bad initialization or unmentioned memory leaks.
Comments
Post a Comment