This is a part of the code not working properly, i first converted an array integer (containing a binary value) to an integer and convert it into an octal value: here is the code: Note: this section of code does not give an output at all.

/****************************************
 CONVERSION FROM DECIMAL TO OCTAL(DOUBLE)
*****************************************/
//we convert the binary valuie to octal value
	int temp = 0;
	double number_binary_copy = 0;

// conerts integer array to integer;
	for(int z=0; z < 64; z++)
	{
	temp =binary_double[z];
	number_binary_copy += temp;
	number_binary_copy *= 10;
	}

	cout <<number_binary_copy <<endl;

	long int number_binary = number_binary_copy;

	int v=0,w[5],x=0; 
	while(number_binary!=0)
	{
	   w[v]= number_binary %1000;
       number_binary= number_binary/1000;
	   if(w[v]>111)
	   x=1;
	   v++;
	}
	v--;
    if(x==0)
	for(;v>=0;v--)
	{
	   switch(w[v])
	      {
	            case 0:
	           cout<<"0";
	             break;
	            case 1:
	          cout<<"1";
	             break;
	            case 10:
	          cout<<"2";
	             break;
	            case 11:
	           cout<<"3";
	             break;
	            case 100:
	            cout<<"4";
	             break;
	            case 101:
	            cout<<"5";;
	             break;
	            case 110:
	           cout<<"6";
	             break;
	            case 111:
	             cout<<"7";
	             break;
	   }
	}

Any help would be greatly appreciated.

Dani AI

Generated

Short diagnosis (likely causes)
The snippet posted by mixes decimal-string building, floating-point arithmetic and tiny fixed buffers — a combination that commonly leads to undefined behaviour (no output, crash or garbage). Specific problems to check first: using a floating type to assemble an integer-length bit string (precision loss and an extra trailing zero), grouping with decimal modulo (e.g. %1000) while treating results as binary patterns, and writing past a small array (the group buffer must hold ceil(nbits/3) entries). As noted, the problem is hard to debug from a fragment; the points above are the most likely causes.

Robust approach
Do not try to turn bit digits into a decimal number and then re-parse them. Either:

  • accumulate an integer value with shifts and print in octal (only if the value fits in 64 bits), or
  • build octal digits directly by grouping three bits from the least-significant side and emit characters — this avoids floating point, avoids huge integers and scales to arbitrary bit lengths.

Example function (safe, works for any bit-length array)
This converts an int array of 0/1 bits (MSB at index 0) into a NUL-terminated octal string.

#include <stdio.h>
#include <string.h>

void bits_to_octal(const int *bits, int nbits, char *out, size_t outlen) {
    int octals[64]; /* ceil(64/3) is 22; 64 is safe upper bound */
    int octlen = 0, val = 0, count = 0;
    for (int i = nbits - 1; i >= 0; --i) {
        val |= (bits[i] & 1) << count;
        if (++count == 3) { octals[octlen++] = val; val = 0; count = 0; }
    }
    if (count) octals[octlen++] = val;
    int i = octlen - 1; while (i > 0 && octals[i] == 0) --i;
    size_t pos = 0;
    for (; i >= 0; --i) pos += snprintf(out + pos, outlen - pos, "%d", octals[i]);
    if (pos < outlen) out[pos] = '\0';
}

Troubleshooting tips
Compile with warnings on (-Wall -Wextra), check array bounds, print intermediate values, ensure bits are strictly 0/1 and confirm bit order (MSB vs LSB). If the input truly is up to 64 bits and you prefer a numeric route, collect into unsigned long long with shifts and print with printf("%llo", value).

Recommended Answers

All 3 Replies

people i still cant figure out the problem :O

people i still cant figure out the problem :O

We can't figure out what the problem is...Posting a chunk of code and stating that it doesn't work and then giving a brief but confusing explanation doesn't help us.

what you want exactly?

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.