//Form1.h (Windows Forms Application)
#pragma endregion
		cli::array <String^> ^sum;
	private: System::Void BtnComp_Click(System::Object^  sender, System::EventArgs^  e) {
			nocarrystate n;
		        carrystate c;
			char a;
		        states * curstate;

			states::init(&n, &c);
			String ^text1 = TxtBin1->Text;
			String ^text2 = TxtBin2->Text;

		        curstate = &n;
			int i = 0;
			int len1 = text1->Length;
         	        sum = gcnew cli::array <String^>(len1);

			while (i < len1)
		        	 curstate = curstate->next (text1[i], text2[i], a); /// a is passed by reference
				 sum[i] = a.ToString();

			 TxtSum->Text = sum.ToString();


My main point of interest is in and after the while loop. 'Variable a' is passed by reference to the next function, and I want to store the value of a in sum then display sum in a text box after the While loop is done. There are several problems I ran into:

-sum = a.ToString(); saves each char's astii code into sum, when I need the actual character. I've tried to test this by having 'variable a' displayed during each iteration of the while loop ( using this line: TxtSum->Text = a.ToString(); ), and the astii is indeed displayed.

-TxtSum->Text = sum.ToString(); displays this: System.String[]

I'd rather use a char array of 'len1' length, but I am unable to do so without several compiler errors.

Help would be greatly appreciated.

6 Years
Discussion Span
Last Post by Rhap

Rearrange it like this:

array<char> ^ sum = gcnew array<char>(text1->Length); 
//you may have missed the ^ above

//in the loop
  sum[i] = a; 
String ^ textout;
for(int i = 0;i<text1->Length;i++)
    textout += sum[i]+ " ";
TxtSum->Text = textout;

When not specified the ToString() method of classes simply returns the datatype. You just needed to output things the old fashioned way (if there's a way to do it all in one fell swoop I don't know it).

Edited by jonsca: n/a


Great thanks! This is a step in the right direction, but still one of my problems remain. The ascii code is being stored in the textout array, therefore the ascii code is being printed instead of the char value


In your function, pass in the 2 chars. Convert them to ints (via subtracting '0') take the sum of the int values and use Convert::ToChar (or really I think you can do a C style cast there) and return that char. It worked for me when I made up a little function to test the other code snippet. I imagine you'll have to modify your method to pass back carry information also.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.