Hey there,I'm looking for assistance with my simple code here.

Why do my integers ( x,v,z,d,e,f,g,h ) suddenly change when the program reaches the " printf("The decimal value is... " part? Is there something I'm missing?

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

int main()
{
int Binary[7],x,v,z,d,e,f,g,h;
Binary[0]=x;
Binary[1]=v;
Binary[2]=z;
Binary[3]=d;
Binary[4]=e;
Binary[5]=f;
Binary[6]=g;
Binary[7]=h;
int decimal,numm0,numm1,numm2,numm3,numm4,numm5,numm6,numm7;

decimal = x*128+v*64+z*32+d*16+e*8+f*4+g*2+h*1;  
 numm0=x*128;
 numm1=v*64;
 numm2=z*32;
 numm3=d*16;
 numm4=e*8;
 numm5=f*4;
 numm6=g*2;
 numm7=h*1;

printf("Binary to Decimal conversion");
printf("\n==========================\n");
printf("Please enter a 8-digit Decimal:");
scanf("%d,%d,%d,%d,%d,%d,%d,%d",&x,&v,&z,&d,&e,&f,&g,&h);
printf("Your entered value is %d\n",x,v,z,d,e,f,g,h);
printf("The decimal value is\n(%dx128=%d)+(%dx64=%d)+(%dx32=%d)+(%dx16=%d)+(%dx8=%d)+(%dx4=%d)+(%dx2=%d)+(%dx1=%d)=%d\n",x,numm0,v,numm1,z,numm2,d,numm3,e,numm4,f,numm5,g,numm6,h,numm7,decimal);




system("pause");
return 0;
}

Dani AI

Generated

There are three root causes here that produce the “mystery” changes: uninitialized variables, an out‑of‑bounds array write, and doing the math before reading input. The line int Binary[7]; ... Binary[7] = h; writes past the declared array (valid indices 0..6) and can clobber nearby stack memory. You also compute decimal and the numm* values before the scanf() so they’re calculated from garbage. Finally, the scanf/printf format issues make the program fragile ("%d,%d,..." forces commas; the earlier printf("Your entered value is %d\n", x, v, ...) only has one %d).

Fix strategy (concise):

  • Don’t use variables before you read/initialize them.
  • Make the array big enough (or don’t use the array at all).
  • Read input in a predictable way (read one string of 8 chars or eight separate ints with matching scanf format).
  • Compute the decimal after successfully parsing input.
  • Compile with warnings enabled (e.g. -Wall -Wextra) to catch many mistakes.

A simple, safe C++ approach (read one token, validate, compute left-to-right):

#include <iostream>
#include <string>

int main() {
    std::string s;
    std::cout << "Enter 8 binary digits (e.g. 01010101): ";
    if (!(std::cin >> s) || s.size() != 8) return 1;
    int decimal = 0;
    for (char c : s) {
        if (c != '0' && c != '1') return 1;
        decimal = (decimal << 1) + (c - '0');
    }
    std::cout << "Decimal = " << decimal << '\n';
}

Notes: this addresses ’s point about timing, acknowledges ’s C/C++ distinction, and explains why ’s “what changed?” question is reasonable — undefined behavior (OOB or uninitialized use) makes values appear to “change” unpredictably. To debug similar problems, print values immediately after input and enable compiler warnings.

Recommended Answers

All 4 Replies

Hey there,I'm looking for assistance with my simple code here.

If you can't figure it out, it's not that simple, is it? :icon_wink:

Why do my integers ( x,v,z,d,e,f,g,h ) suddenly change when the program reaches the " printf("The decimal value is... " part? Is there something I'm missing?

Define "suddenly change"? What did they start out as and what do you think they became? Since you never output any of those variables until after you scanf() 'd them, how do you know anything happened to them?

Sorry, but based on the code, the question is nonsensical. Think through the question again and restate.

Strictly this is more like C than C++. :X

It appears that you are using variables x,v,z,etc before assigning valid values to them.
You should compute num1,num2,etc only after the scanf(...) statement..

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.