Binary to Decimal code c++ help

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 become weird numbers when the program reaches the " printf("The decimal value is... " part? Aren't they supposed to use the array values from the earlier portion of the code? Is there something I'm missing?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#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;  


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);

 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("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;
}


Thanks for any assistance in advance!
You never initialize x,v,z,d,e,f,g,h.
Didn't I already initialize them in the earlier part of the code?(Lines 7-15)
it's declaring
In lines 8-15 you are using the variables but you haven't set them yet.

Also in line 15 you use Binary[7], however this doesn't exist yet. You have only 7 elements (indexes 0-6).

Actually, you can delete every line with Binary[] in it because you never use that.

You do need to put line 18 below line 24 or you are using uninitialized values.
Topic archived. No new replies allowed.