How to covert Textbox to integer array in c++/cli - c++-cli

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.

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.

Related

Force decimal places to a string

How can I add a trail of six 0 of decimal places to a number in string type?
I tried PadRight but didn't get the result that I wanted. For example:
125 ===to===> 125,000000
14,5 ===to===> 14,500000
so on..
Is there any function in VB.NET can do that or do I need to split the string then combine them?
I'm not completely sure what you're asking, but if you want to add six 0's to a string then I would make a function, something like this:
private static string AddZeroes(string myString)
{
//Makes an array that is 6 elements higher than the original string
string[] myStringArray = new string[myString.Length + 6];
//Sets the array to have all elements of myString inside of it
for (int i = 0; i < myString.Length; i++)
{
myStringArray[i] = Convert.ToString(myString[i]);
}
//Adds the six 0's to the array
for (int i = myString.Length; i < myString.Length + 6; i++)
{
myStringArray[i] = "0";
}
//Sets myString to an empty string, then adds all the contents of the myStringArray to it
myString = "";
for (int i = 0; i < myStringArray.Length; i++)
{
myString += myStringArray[i];
}
//Returns the string that should now have six 0's added to the end
return myString;
}
If you wanted a comma or decimal, it shouldn't be too hard to just add one right before the for loop that adds the 0's, and also make sure that the array is the length of the string + 7 instead, to fit that extra character.

getch is beeing skipped in a while loop

I have this function, while checks for correct input which need to be between 1-6 but when I call this function it just skips all the getch and all the putchar. What am I doing wrong?
int firstNum = 0;
int secondNum = 0;
int thirdNum = 0;
int fourthNum = 0;
int counter = 0;
int counterMiss = 0;
int counterInPlace = 0;
int condition = 1;
while(firstNum<0||firstNum>6||secondNum<0||firstNum>6||secondNum<0||secondNum>6||thirdNum<0||thirdNum>6||fourthNum<0||fourthNum>6) //loop that checks for input correct
{
if(counter>0)
{
printf("enter ONLY numbers beween 1-6\n"); //if there were any incorrect input it will trigger
}
firstNum=getch();
putchar(firstNum);
secondNum=getch();
putchar(secondNum);
thirdNum=getch();
putchar(thirdNum);
fourthNum=getch();
putchar(fourthNum);
firstNum-=48;
secondNum-=48;
thirdNum-=48;
fourthNum-=48;
counter++;
}
If you want a number of identical variables, use an array, not separate scalar variables. Separate variables like this look ugly and will not scale.
Also, you're initializing the variables to zero, and then testing if they are less than zero or greater than six. They aren't, so your loop doesn't run.

Visual C++ Statement + Vars

How Can I bind for example int value to the statement below?
System::String^ Content = "just example";
int iAValue = 5;
lblOutput_{iValue}->Text = Content;
You don't; you use an array or collection of some sort. This sort of thing is often attempted by beginners. It is not possible, nor is it a good idea to tie your program logic to the names of your variables.
auto labels = gcnew List<Label>();
labels->Add(lblOutput1);
labels->Add(lblOutput2);
labels->Add(lblOutput3);
labels->Add(lblOutput4);
labels->Add(lblOutput5);
// ...
String^ Content = "just example";
int iAValue = 4;
labels[iAValue].Text = Content;
And then later you can iterate over all of them easily:
for(int i = 0; i < labels->Count; ++i) {
// i is the label "number"
// labels[i] is the label
}

Using memcpy and malloc resulting in corrupted data stream

The code below attempts to save a data stream to a file using fwrite. The first example using malloc works but with the second example the data stream is %70 corrupted. Can someone explain to me why the second example is corrupted and how I can remedy it?
short int fwBuffer[1000000];
// short int *fwBuffer[1000000];
unsigned long fwSize[1000000];
// Not Working *********
if (dataFlow) {
size = sizeof(short int)*length*inchannels;
short int tmpbuffer[length*inchannels];
int count = 0;
for (count = 0; count < length*inchannels; count++)
{
tmpbuffer[count] = (short int) (inbuffer[count]);
}
memcpy(&fwBuffer[saveBufferCount], tmpbuffer, sizeof(tmpbuffer));
fwSize[saveBufferCount] = size;
saveBufferCount++;
totalSize += size;
}
// Working ***********
if (dataFlow) {
size = sizeof(short int)*length*inchannels;
short int *tmpbuffer = (short int*)malloc(size);
int count = 0;
for (count = 0; count < length*inchannels; count++)
{
tmpbuffer[count] = (short int) (inbuffer[count]);
}
fwBuffer[saveBufferCount] = tmpbuffer;
fwSize[saveBufferCount] = size;
saveBufferCount++;
totalSize += size;
}
// Write to file ***********
for (int i = 0; i < saveBufferCount; i++) {
if (isRecording && outFile != NULL) {
// fwrite(fwBuffer[i], 1, fwSize[i],outFile);
fwrite(&fwBuffer[i], 1, fwSize[i],outFile);
if (fwBuffer[i] != NULL) {
// free(fwBuffer[i]);
}
fwBuffer[i] = NULL;
}
}
You initialize your size as
size = sizeof(short int) * length * inchannels;
then you declare an array of size
short int tmpbuffer[size];
This is already highly suspect. Why did you include sizeof(short int) into the size and then declare an array of short int elements with that size? The byte size of your array in this case is
sizeof(short int) * sizeof(short int) * length * inchannels
i.e. the sizeof(short int) is factored in twice.
Later you initialize only length * inchannels elements of the array, which is not entire array, for the reasons described above. But the memcpy that follows still copies the entire array
memcpy(&fwBuffer[saveBufferCount], &tmpbuffer, sizeof (tmpbuffer));
(Tail portion of the copied data is garbage). I'd suspect that you are copying sizeof(short int) times more data than was intended. The recipient memory overflows and gets corrupted.
The version based on malloc does not suffer from this problem since malloc-ed memory size is specified in bytes, not in short int-s.
If you want to simulate the malloc behavior in the upper version of the code, you need to declare your tmpbuffer as an array of char elements, not of short int elements.
This has very good chances to crash
short int tmpbuffer[(short int)(size)];
first size could be too big, but then truncating it and having whatever size results of that is probably not what you want.
Edit: Try to write the whole code without a single cast. Only then the compiler has a chance to tell you if there is something wrong.

Retrieve a char from a char array

Does anyone know how to retrieve a char value from a char array:
char* alphaChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (int rowIndex = 0; rowIndex < 12; rowIndex++)
{
char* text = (char*)alphaChars[0]; //this throws an error
//CGContextShowTextAtPoint(context, 10, 15, text, strlen(text)); //This is where I wanna use it
}
If you just want one character, you don't want to assign it to a pointer:
char text = alphaChars[0];
Then you would call your next function:
CGContextShowTextAtPoint(context, 10, 15, &text, 1);
If you want the whole string, which is sort of what your code looks like it's doing, you don't need to have an intermediate variable at all.