STRNCPY Weird Characters

Hey guys!

So, I was working on learning how to use strncpy, and strcpy, which are pretty easy, and when I made some code, and my output had a weird character that wasn't in the program. Here's the trouble code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
cout << "Enter text;\n";
	
	cin.get(myText, 999);
	
	cout << myText;
	
	char secondText[10000];
	
	strcpy(secondText, myText);
	
	char thirdText[Months + 1];
	
	strncpy(thirdText, secondText, Months);
	
	cout << "Success!\n";
	cout << thirdText;


The thing with my weird output though, is that I can't copy and paste it. When I
do, it appears as SOHD, with SOH outlined in black. here's a screenshot.

https://drive.google.com/file/d/0B6dAeZ95gNJuczctRndDd2g5eDg/edit?usp=sharing

Any ideas on what makes this thing pop up?
Your screen shot is private, so I can't see it. However, those characters you're referring to are special characters. If you open up your .exe with the same file editor, you'll notice similar characters.

Everything seems fine there otherwise. Is myText a character array? Why do you need thirdText to be Months + 1 large? Your issue is probably that + 1, or the program is reading the character array further into memory than it needs to go and is returning funky characters. You should really set your thirdText[Months] to '\0' so that doesn't happen, and that you're displaying some random characters.

If you could post your entire code, or make that screenshot public, I could possibly help you further.
oops. sorry. Can you see it now? Yes, myText is a character array. And you're right, I don't need thirdText to be that big. I changed it back and compiled, yet it still pops up with the same character. I also did
char thirdText[Months] = "\0";
but that character is still there. Here's some other code that might be helpful.
1
2
3
4
5
//My includes
#include <iostream>
#include <cstring>
#include <string>


1
2
3
4
//Month enum 
//EDIT: I changed monthNames so there were no assigned values
//but that didn't work either.
enum monthNames {January, February, March , April, May, June,  July, August, September, October, November, December, Months};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
char myText[1000] = "Blank!";
	
	cout << myText;
	
	cout << "Enter text;\n";
	
	cin.get(myText, 999);
	
	cout << myText;
	
	char secondText[1000];
	
	strcpy(secondText, myText);
	
	char thirdText[Months] = "\0";
	
	strncpy(thirdText, secondText, Months);
	
	cout << "Success!\n";
	cout << thirdText;
}


The other code just messes around with other types of arrays. Thanks in advance for suggestions!
Last edited on
strncpy(thirdText, secondText, Months);

strncpy() doesn't append a NULL character automatically to the end of destination if ever the source is longer than the limit. printing destination so will cause weird output since there is no null terminator to signal the end of the string, The insertion operator will just print every byte starting from &thirdText[ 0 ] up to whenever it found a null terminator. (the memory following &thridText[ Months ] could be some random data)

therefore, you should append NULL manually if ever source length is greater than the limit (3rd argument)

put this after strncpy()

thrirdText[ Months ] = '\0';

http://www.cplusplus.com/reference/cstring/strncpy/?kw=strncpy
Last edited on
Thanks a lot for your help guys! I appreciate it!
Topic archived. No new replies allowed.