From: "Lark" Newsgroups: comp.os.msdos.djgpp Subject: Weird char * mishaps Date: Thu, 1 Apr 1999 09:36:49 -0800 Organization: MindSpring Enterprises Lines: 166 Message-ID: <7e00s5$g94$1@camel0.mindspring.com> NNTP-Posting-Host: d1.8a.3b.f9 X-Server-Date: 1 Apr 1999 14:46:29 GMT X-Newsreader: Microsoft Outlook Express 4.72.3110.1 X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3 To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com I made a hangman game the other day because I was extremely, extremely bored. It works fine, except for one thing: after I print out the remaining letters, 4 or 5 weird random letters are printed. This is a total stumper. I am positive I initialized all of my char *s to the correct number of elements, and I just can't think of anything else that could cause an error like this. I am almost absolutely sure that I never access an element out of bounds or anything. This is how the program works: It uses a class called answer, ie the answer to the puzzle, to control what the user can see and what he/she can't. It has a member char * word, which stores the answer, and a member char * mask, which holds a bunch of '-'s or spaces. Then it has an unmask function, whose only parameter is a single character, and it takes the '-'s out of mask that are covering that character in word. It returns the number of letters it uncovered. Here is the source code: file://header file containing answer class: file://header file for hangman.cpp #include file://strlen(char *), returns length of string class answer { public: answer(char *); ~answer(); int unmask(char); int unmasked(); operator char *(); private: char * word; char * mask; int length; int masked; }; answer::answer(char * string) file://PRECONDITIONS : none file://POSTCONDITIONS : word = string, mask is the length of string with all alphabetic // characters '-'d out, length = length of string, masked = #masked // characters. { masked = 0; length = strlen(string); word = new char[length]; word = string; mask = new char[length]; for (int i = 0; i < length; i++) if (((word[i]>='a') && (word[i]<='z')) || ((word[i]>='A') && (word[i]<='Z'))) { mask[i] = '-'; masked++; } else mask[i] = ' '; } answer::~answer() { delete mask; mask = 0; delete word; word = 0; length = 0; } int answer::unmask(char index) file://PRECONDITONS: this has been initialized(answer()) file://POSTCONDITIONS: every index of word which has index is taken out of mask, masked // is updated { int numberOfOccurences = 0; char indexlow, charlow; file://To make sure that 'I' unmasks 'i' indexlow = index; if ((index >= 'A') && (index <= 'Z')) indexlow = index - 'A' + 'a'; file://gets the lowercase of index for (int i = 0; i < length; i++) { charlow = word[i]; if ((word[i] >= 'A') && (word[i] <= 'Z')) charlow = word[i] - 'A' + 'a';//gets the lowercase of word[i] if (charlow == indexlow) { numberOfOccurences++; masked--; mask[i] = ' '; } } return numberOfOccurences; } answer::operator char *() file://PRECONDITIONS: this is initialized file://POSTCONDITIONS: word is returned, with mask superimposed on top of it. { char * final = new char[length]; for (int i = 0; i < length; i++) if (mask[i] == '-') final[i] = '-'; else final[i] = word[i]; cout << "||||||||||" << length << "||"; return final; } int answer::unmasked() file://returns true if word is completely unmasked { if (masked == 0) return 1; else return 0; } ///////////////////////////////////////////////// hangman.cpp ///////////////////////////////////////////////// #include #include "hangman.h" int main() { answer word("I like this. It even does capitals!"); int trysLeft = 10; char guess; while ((trysLeft > 0) && (!word.unmasked())) { cout << word << " : You have " << trysLeft << " trys left. Pick a letter: "; cin >> guess; if (word.unmask(guess) == 0) { cout << "Sorry, wrong" << endl; trysLeft--; } } return 0; } Thank you so much for even reading this far. Has this happened to anyone else? A similar error is happening in another program that uses a char *. Any help at all will be greatly appreciated. Thanks again!