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>
usingnamespace 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;
}
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;