0

I'm trying to convert the textbox to an integer array assuming that every character of the textbox is a digit.

//textbox is named input
int size = this->input->Text->Length;
int * num = new int[size];
int Counter = 0;

//for loop used since textbox inputs like a calculator
//Ex: the number: 234 is inputed: 2, then 23, then 234
for (int i = size; i > 0; i--)
{
    num2[Counter] = System::Convert::ToInt32(input->Text[i-1]);
    Counter += 1;
}

Array of numbers should be:

num[0] = 4, num[1] = 3, num[2] = 2 

Upon research though it seems that it's finding the integer unicode value instead.

  • 1
    You need to iterate over the textbox characters, presumably via input->Text[i]. But i should go from size-1 to 0. – Malcolm McLean May 22 '17 at 23:19
  • @MalcolmMcLean: Best not, or your loop will run forever. OP is doing it right, if we ignore the fact that it doesn't matter because `i` is never used. – Lightness Races in Orbit May 23 '17 at 00:00
  • 1
    To @sergiol: one would generally use C++/CLI *or* Managed C++. These are different iterations of C++ with .Net Interop, and Managed C++ is only in Visual Studio 2005. – crashmstr May 23 '17 at 12:01
  • @crashmstr: I upvoted you for clarifying me. I never knew what was the difference between Managed C++ and C++/CLI ! – sergiol May 23 '17 at 13:27
  • 1
    Possible duplicate of [How to convert char to int?](https://stackoverflow.com/questions/3665757/how-to-convert-char-to-int) – crashmstr May 23 '17 at 14:42
  • You are using a method that gets you the integer unicode value, not the numerical value of the digit: [Convert.ToInt32 Method (Char)](https://msdn.microsoft.com/en-us/library/ww9t2871(v=vs.110).aspx) (linked duplicate shows how to do this) – crashmstr May 23 '17 at 14:43

1 Answers1

0

Code input->Text[i-1] returns a single Unicode character value of the wchar_t type. That is implicitly cast to Int32, i.e. the symbol code.

You have to convert the char to a string, before converting to the number. You can use the Substring method or the ToString method for this purpose.

You can do it as follows:

String^ text = this->input->Text;
int size = text->Length;
int * num = new int[size];

for (int i = size - 1; i >= 0; i--) {
    num[i] = Convert::ToInt32(text->Substring(size - i - 1, 1));
}

However, you should not mix managed and unmanaged code.

There is a better way. Use a generic collection instead of an array.

String^ text = this->input->Text;
int size = text->Length;
List<int>^ nums = gcnew List<int>();

for (int i = size - 1; i >= 0; i--) {
    nums->Add(Convert::ToInt32(text[i].ToString()));
}

Don't forget

using namespace System::Collections::Generic;

The list can be accessed by index like an array:

nums[i]

So it is convenient to work with. And most importantly, do not need to worry about freeing memory.

Alexander Petrov
  • 13,457
  • 2
  • 20
  • 49
  • You've posted code. How about adding an explanation of what this code does and how it answers the question to make this a better answer? – crashmstr May 23 '17 at 14:09
  • @crashmstr - I'm not native english speaker, so it's hard for me to write in English. I do it slowly, tormenting translator. So easy to write the code, so hard to write in English. – Alexander Petrov May 23 '17 at 14:34