H-E-L-P!!!question about "copy" )-:

Hi, In the attached code, I used the function "copy" of the library <string>.
The problem is that I want that in the end of the string "h_char" I will have a NULL character, rather than junk values​​.

What should I do for it?
Does anyone know whether there is a function built for it?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
using namespace std;
int main () {
		
		cout<<"please insert yours string"<<endl;
		string str;
		cin>>str;
			
			//hour
			char ch[3];
			char* h_char=ch;
			str.copy(h_char,2,0);
			puts(h_char);
			cout<<endl;
		return 0;
}
Last edited on
h_char[2] = '\0' ;

Although I'm not sure what you're doing with h_char there anyway.

1
2
3
4
char ch[3] ;
str.copy(ch,2,0);
ch[2] = '\0' ;
cout << ch << endl ;
Last edited on
thank!
its work!
Topic archived. No new replies allowed.