Convert char* to string problem...

The output of string is different with char, during the convert of char to string..
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <string>
using namespace std;
int main(){
	char ic[50];
	string mystring = string (ic);
	cout<<"Please enter ic: ";
	gets_s(ic);
	cout<<"Your ic is "<<mystring<<endl;
	return 0;
}

ic = lance
Your ic is 北北北北

my output should be lance but why it turn to become another words?
Last edited on
No. mystring will have a copy of the content of ic (till the '\0').
However you did not initialized 'ic', so that will lead to undefined behaviour.

Also, I don't think that using gets_s is safe (by the way ¿where is defined?)
means i need to add something like ic[50] = '\0' right?
gets_s is a Microsoft function, I believe.

Lance: is there a reason you're trying to use both char arrays and strings for this? This is all you need:

1
2
3
4
5
6
7
int main(){
	string mystring;
	cout<<"Please enter ic: ";
	cin >> mystring;
	cout<<"Your ic is "<<mystring<<endl;
	return 0;
}
actually it is a testing for converting the char to string. i want to try it....
OK, then...so you're aware that you have two distinct data structures, right? You have ic, which is your character array, and you have mystring, your C++ string.

I'm not sure what you're trying to do with the assignment in line 6, but it doesn't somehow automatically associate the two structures. If you want to copy a C++ string into a char array, you can use a line like:

strcpy(ic, mystring.c_str());

The "c_str()" function returns a pointer to a C-style string (AKA a character array).

Again, with ic and mystring, you have two distinct structures, so you have to do the assignment manually to get them to be the same. I hope this makes sense.
well....i am trying to convert the char array into string data type...i'll show u another way to convert char into string data type....but the output is different with what i enter...i dunno why...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main(){
	char ic[50];
	stringstream ss;
	string ic2;
	ss << ic;
	ss >> ic2;
	cout<<"Please enter ic: ";
	gets_s(ic);
	cout<<"Your ic is "<<ic2<<endl;
	return 0;
}
Er... you are making this way too complicated.

The reason it wasn't working before was because you were assigning ic to a string before ic had any value in it. You have to do it after ic has been filled.

1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
  char ic[50];
  string ic2;

  cout << "Please enter ic: ";
  gets_s(ic);

  // now that 'ic' has been filled, you can put it in a string
  ic2 = ic;  // copies the string to ic2

  cout << "Your ic is " << ic2 << endl;
}


Of course... there is no reason at all to use a char array here in the first place. Just use a string and get rid of the silly gets_s function in place of getline:

1
2
3
4
5
string ic;

cout << "Please enter ic: ";
getline( cin, ic );
cout << "Your ic is " << ic << endl;
gets_s isn't portable. I don't know whether that matters to you, but...lots of installations won't support it.

Look at this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>
using namespace std;
int main() {
	char ic[50];
	string mystring;

	cout << "Please enter ic: ";
	cin >> ic;
//	strcpy(ic, mystring.c_str());
	mystring = ic;
	cout << "Your ic is " << mystring << endl;
	return 0;
}


Line 11 does what you want, I think.
your guys really helped me a lot, thanks!!!
Topic archived. No new replies allowed.