Hi All!

I've got a weird problem here...It doesn't want to add my int values to a char array even though I static_cast the value to char.

If you could just look at my code please!

void AltBoolean::print(){
  
 perma = new char[arraySize];
 int temp;
 int counter = 1;

  for(int i = 0 ; i < arraySize ;i++)
  {
      if(ChoiceArray[i] == 0)
      {
	perm[i] = ArrOperator[i];
      }
      else if(ChoiceArray[i] == 1) //it doesn't go into this part of the function it seems!
      {
	cout << "this doesn't even print" << endl;
	temp =  ArrOperand[counter];
	static_cast<char>(temp);
	perm[i] = temp;
	counter++;
      }
  }
  
  for (int d=0; d<arraySize; d++)
    cout << ArrOperator[d];

    for (int e=1; e<arraySize; e++)

	cout << ArrOperand[e];

    
    for (int r=0; r<arraySize; r++)
    cout << perma[r];

}

Declared the array's as follow:

bool *ChoiceArray;
int *ArrOperand;
char *ArrOperator;
char *perm;

The above is initialized correctly! and I checked for that...think its something with the static cast/converting area.

my arraySize is 3 here and the bottom for-loop for the ArrOperator and ArrOperand prints fine! but in the perma arr it doesn't print the ArrOperand part!

Also in my ChoiceArray it only consists of 0's and 1's!

Any help will be appreciated...thanks !

Hi All!

I've got a weird problem here...It doesn't want to add my int values to a char array even though I static_cast the value to char.

If you could just look at my code please!

void AltBoolean::print(){
  
 perma = new char[arraySize];
 int temp;
 int counter = 1;

  for(int i = 0 ; i < arraySize ;i++)
  {
      if(ChoiceArray[i] == 0)
      {
	perm[i] = ArrOperator[i];
      }
      else if(ChoiceArray[i] == 1) //it doesn't go into this part of the function it seems!
      {
	cout << "this doesn't even print" << endl;
	temp =  ArrOperand[counter];
	static_cast<char>(temp);
	perm[i] = temp;
	counter++;
      }
  }
  
  for (int d=0; d<arraySize; d++)
    cout << ArrOperator[d];

    for (int e=1; e<arraySize; e++)

	cout << ArrOperand[e];

    
    for (int r=0; r<arraySize; r++)
    cout << perma[r];

}

Declared the array's as follow:

bool *ChoiceArray;
int *ArrOperand;
char *ArrOperator;
char *perm;

The above is initialized correctly! and I checked for that...think its something with the static cast/converting area.

my arraySize is 3 here and the bottom for-loop for the ArrOperator and ArrOperand prints fine! but in the perma arr it doesn't print the ArrOperand part!

Also in my ChoiceArray it only consists of 0's and 1's!

Any help will be appreciated...thanks !

static_cast<char>(temp) does not change the value of temp, rather it creates a temporary char value that you need to assign.

char somechar;
somechar = static_cast<char>(temp)

static_cast is for use with related classes. I think it is unusual to see it being used to convert between primitive data types like char/int. Instead use explicit casting.

char somechar = (char) temp;
temp =  ArrOperand[counter];
	static_cast<char>(temp);
	perm[i] = temp;

Why all that hard work??? All you need is this: perm[i] = (char) ArrOperand[counter]; -- that is, assuming the values in ArrOperand can be held in a char variable. If not, then it won't work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.