Hi guys,
I hate having to ask, because its probably a fairly noobish question, but Im having a problem with char arrays and passing them between functions, and its driving me crazy.
Basically Im asking the user for an input string, creating a char array out of it, and then passing it back to my main function - however the output is always a random string of unicode characters like this - ╠╠╠╠╠╠╠╠↑
I have recreated the problem in something a bit less messy than my actual project file, so code is in the spoilers.
Can anyone explain why?
Any help would be greatly appreciated.
I hate having to ask, because its probably a fairly noobish question, but Im having a problem with char arrays and passing them between functions, and its driving me crazy.
Basically Im asking the user for an input string, creating a char array out of it, and then passing it back to my main function - however the output is always a random string of unicode characters like this - ╠╠╠╠╠╠╠╠↑
I have recreated the problem in something a bit less messy than my actual project file, so code is in the spoilers.
const int BUFFER = 50;
char* getWord()
{
char word[BUFFER];
cout << "Please enter a word: ";
cin.getline(word, BUFFER);
cout << word << "\n"; // Returns the input string no problem.
return word;
}
int main()
{
char* wordPtr;
wordPtr = getWord();
cout << "Your word is: " << wordPtr << "\n"; // Returns unicode characters. Why?
system("PAUSE");
return 0;
}
char* getWord()
{
char word[BUFFER];
cout << "Please enter a word: ";
cin.getline(word, BUFFER);
cout << word << "\n"; // Returns the input string no problem.
return word;
}
int main()
{
char* wordPtr;
wordPtr = getWord();
cout << "Your word is: " << wordPtr << "\n"; // Returns unicode characters. Why?
system("PAUSE");
return 0;
}
Any help would be greatly appreciated.