string to char?

i want to store the string into char but it wont let me. how do i get around it please ?
1
2
3
4
5
6
7
8
9
10
11
12
int main (){
string UserText;
	int TextSize;
	char UserChar;
	cout << "input your text: ";
	getline(cin, UserText);
	TextSize = UserText.length();
	for (int TextCounter; TextCounter <= TextSize; TextCounter++)
	{
	cout << UserText[TextCounter] << endl;
   UserChar = UserText;
}
i want to store the string into char


Line 8: TextCounter initial value is undefined.
Assuming you initialize TextCounter to 0, Your loop is going to execute one too many times. You termination condition should be TextCounter < TextSize.

Line 11: You can't store a multi-character string into a single character.
You can store any single character of the string simply by indexing it.
 
  UserChar = UserText[TextCounter];

Not clear from the snippet you posted what that accomplishes.
A char can only store one character of your UserText. You can convert your string into a C style string, but why do you want to do it?

#include <string.h>
1
2
3
4
5
  
char *copyOfString = new char[UserText.size () + 1];
strcpy (copyOfString, UserText.c_str ());
// use copyOfString
delete[] copyOfString;
Topic archived. No new replies allowed.