Jump to content
  • Advertisement
Sign in to follow this  
pascalosti

string to char...

This topic is 4353 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Can someone tell me how to get single characters from a string to a char array. This book by deitel is driving me crazy. Its asking for a struct but the book doesnt use one. It has to be in .NET .
void EnterName(){
String ^x;
String ^tmp, ^y;
	Console::WriteLine("Enter your name: ");
	
	x = (Console::ReadLine());
	for(int i = x->Length; i > 0; i--){
		y =  String::Concat(tmp,x->Chars.ToString());}
		
		Console::WriteLine(" {0}",y);
}



Error	1	error C3293: 'Chars': use 'default' to access the default property (indexer) for class 'System::String'	c:\Documents and Settings\posti\Desktop\String Assignment\String\String\String.cpp	15	
Error	2	error C2228: left of '.ToString' must have class/struct/union	c:\Documents and Settings\posti\Desktop\String Assignment\String\String\String.cpp	15	




Share this post


Link to post
Share on other sites
Advertisement
the error tells you, you should use indexing to get the character from the Chars property

in code:

x

returns a character at position i


another thing:
in the for loop, you start at i=x->Length
note that indexing starts at 0 and ends at Length-1, so you're trying to get a character beyond the length of the string.

Share this post


Link to post
Share on other sites
thank you that worked, the stuff from before is right out of a book, I have to stop buying cheap drugs...


void EnterName(){
String ^x;
String ^tmp, ^y;
int counter = 0;
Console::WriteLine("Enter your name: ");

x = (Console::ReadLine());
Console::WriteLine(" size {0}", x->Length);
for(int i = x->Length; i > 0; i--){

Console::Write("{0}", y = String::Concat(tmp,x[counter].ToString()));
counter++;}

Console::WriteLine(" The result:{0}",x);
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!