I'm very new to C++ programming and I'm having a little trouble with a function I'm making. It's supposed to take a char array containing a sentence and output the first word.
In line 14, it allocates the amount of space needed to hold the word plus the null terminating character, but for some reason it's allocating too much space so garbage characters are at the end of the word. I've tried putting in constants (e.g. 1, 3, 5) but it always seems to be allocating the same amount of space instead of the amount I'm asking for. What am I doing wrong?
You're not allocating enough space for the text and the null. And you're not placing the null at the end of the string. You need these changes: char* fWord = newchar[fWordLen + 1];
... fWord[fWordLen] = '\0';
You also need to return something if you have leading spaces, null probably.
there are many bad C++ books out there. And we are not talking about bad style, but things like sporting glaringly obvious factual errors and promoting abysmally bad programming styles