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 !

Dani AI

Generated

Two separate issues explain the observed behavior.

First, a name/usage mismatch: the buffer that is allocated (the code shows perma = new char[arraySize];) is not the same buffer that gets written to and/or printed (perm[i] = ... is assigned and perma[r] is printed). That leaves the printed buffer uninitialized or unchanged. Make allocation, assignment and printing use the same identifier (and ensure the declared pointer is the one allocated).

Second, converting integers to char is not the same as converting them to their digit characters. Casting an int to char stores the numeric byte value (often non‑printable for small integers); to get printable digits either add '0' for single digits or convert the whole integer to text and append that text. Using a string or a dynamic text buffer makes this much simpler and safer than manual char[] management. Example approach:

std::string out;
int opIndex = 0;
for (int i = 0; i < arraySize; ++i) {
    if (ChoiceArray[i] == 0)
        out.push_back(ArrOperator[i]);
    else
        out += std::to_string(ArrOperand[opIndex++]);
}
std::cout << out << '\n';

Quick checklist:

  • Make the pointer name consistent (allocate the exact pointer that is later filled and printed).
  • Verify ChoiceArray contents and print them as integers while debugging.
  • Confirm counter/index math (starting at 0 vs 1) and that ArrOperand accesses are in-bounds.
  • If a C-string is required, allocate one extra byte for '\0' and set the terminator.
  • Prefer std::string/std::vector<char> and std::to_string to avoid manual memory bugs and to keep intent clear.

Both and pointed toward casting/assignment issues; after fixing the pointer/name mismatch and converting integers to text (or using the '0' + n rule for single digits) the missing operand output should appear.

Recommended Answers

All 2 Replies

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.