Tablica hashujaca nadpisywanie wskaznikow

0

Witam mam problem typu takiego ze podczas wczytywanie danych do tablicy haszujacej wszystkie wartosci key i value podmieniaja sie na ostatni dodany element, oto czesc kodu w ktorej jest blad ale nie wiem dlaczego, z poczatku myslalem ze wadzi strcpy kopiujac tylko wskaznik ktory sie nie zmieni a wartosc pod nim tak, jednak po zamianie na for ktory przepisze stringa problem nie znikl:

 
void Dictionary::read(char *letter, char *rest_of_letter) {
	while (EOF) {
		while (cin>>letter) {
			change_letter = false;
			if (!start_translate) {
				if (letter[0] >= SMALL_LETTER_MIN && letter[0] <= SMALL_LETTER_MAX) {
					strcpy(word,letter);
					cin>>letter;
					strcpy(translation,letter);
					hash.put(word, translation);
				} else {
					for(int i = 0; i < 10; i++){
						if( hash.table[i] != NULL)
						cout << "test2: " << hash.table[i]->key << " " << hash.table[i]->value << endl;
					}
					search_letters(letter, rest_of_letter);
					translate(letter,rest_of_letter);
					start_translate= true;
					if(getchar() == '\n')
						break;
				}
			} else { 
					search_letters(letter, rest_of_letter);
					translate(letter,rest_of_letter);
					start_translate= true;
					if (getchar() == '\n')
						break;
			}
		}

		show_all(Dictionary_acc,Dictionary_bad);
		for(int i = 0; i < 10; i++){
			if( hash.table[i] != NULL)
			cout << hash.table[i]->key << " " << hash.table[i]->value << endl;
		}
		index_acc = 0;
		index_bad = 0;
		start_translate = false;
		show_accept_Dictionary = true;

	}

}
 
void HashMap::put(char *key, char *value) {
	int hash = hashString(key);
	if (table[hash] == NULL)
		table[hash] = new LinkedHashEntry(key, value);
	else {
		LinkedHashEntry *entry = table[hash];
		while (entry->getNext() != NULL)
			entry = entry->getNext();
		if (entry->getKey() == key)
			entry->setValue(value);
		else
			entry->setNext(new LinkedHashEntry(key, value));
	}
	cout << "test: " << table[hash]->key << " " << table[hash]->value << endl;
}

Przykladowy in:
aa xy (slowo , tlumaczenie)
bb ji
Out wyswietlenie hash mapy
aa xy
bb ji

Dla
cout << "test: " << table[hash]->key << " " << table[hash]->value << endl;
pokazuje mi prawidlowy out ale dla

	for(int i = 0; i < 10; i++){
		if( hash.table[i] != NULL)
		cout << "test2: " << hash.table[i]->key << " " << hash.table[i]->value << endl; 

out juz jest:
bb ji
bb ji

0

eti pg. Idź na konsultacje kolego, albo napisz na naszym forum.
:D

0

Prawdopodobnie robisz coś takiego:

char *tb[5];
char bufor[100];
for(int i=0;i<5;++i) { cin.getline(bufor,100); tb[i]=bufor; }

Kiedy powinieneś zrobić to tak:

char *tb[5];
char bufor[100];
for(int i=0;i<5;++i) { cin.getline(bufor,100); tb[i]=strdup(bufor); }

Uwaga! strdup() działa na malloc()/free() możesz sobie go przepisać na new/delete lub zwalniać poprzez free()

1 użytkowników online, w tym zalogowanych: 0, gości: 1