Amateaur Problem

Hi all, this is my first post and I am new to C++.

I am trying to write a simple program to convert binary numeral system to decimal. While I am aware of the open-source binary converter available on this site, I chose to write my own for a challenge and to practise some new skills.

Here is the code. It may appear to be longer than necessary but because I am a beginner I could only use the syntax I know, and I assume there is an easier and more efficient method of doing this. Excuse my silly notes, they aren't there to insult your intelligence, I wrote them for myself.

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
using namespace std;

int main()
{
	int length, index;
	int digit1, digit2, digit3, digit4, digit5, digit6, digit7, digit8;
	int translation;

	char string[8];
	cout << "Enter binary string: ";
	gets(string);  // enter string

	length = strlen(string);
	cout << length;  // declare string size

	int pwrs[8];
		pwrs[0]=128; pwrs[1]=64; pwrs[2]=32; pwrs[3]=16;
		pwrs[4]=8; pwrs[5]=4; pwrs[6]=2; pwrs[7]=1;

	switch(length){								
		case '1':	//The string size determines the switch
			if(string[0]='1'){	//Each char is assigned to a var so that the last number always=digit8
				digit8=pwrs[7];	//For example, with string 10, digit7=1 and digit8=0
			}else digit8=0;	//If digit8 is 1, digit8 = pwrs[7](1)
			break;	//If digit8 isn't 1, digit8 = 0
			translation=digit8;	//The translation is a sum of all the digits
												
		case '2':
			if(string[0]='1'){
				digit7=pwrs[6];
			}else digit7=0;
			if(string[1]='1'){
				digit8=pwrs[7];
			}else digit8=0;
			translation=digit7+digit8;
			break;

		case '3':
			if(string[0]='1'){
				digit6=pwrs[5];
			}else digit6=0;
			if(string[1]='1'){
				digit7=pwrs[6];
			}else digit7=0;
			if(string[2]='1'){
				digit8=pwrs[7];
			}else digit8=0;
			translation=digit6+digit7+digit8;
			break;

		case '4':
			if(string[0]='1'){
				digit5=pwrs[4];
			}else digit5=0;
			if(string[1]='1'){
				digit6=pwrs[5];
			}else digit6=0;
			if(string[2]='1'){
				digit7=pwrs[6];
			}else digit7=0;
			if(string[3]='1'){
				digit8=pwrs[7];
			}else digit8=0;
			translation=digit5+digit6+digit7+digit8;

		case '5':
			if(string[0]='1'){
				digit4=pwrs[3];
			}else digit4=0;
			if(string[1]='1'){
				digit5=pwrs[4];
			}else digit5=0;
			if(string[2]='1'){
				digit6=pwrs[5];
			}else digit6=0;
			if(string[3]='1'){
				digit7=pwrs[6];
			}else digit7=0;
			if(string[4]='1'){
				digit8=pwrs[7];
			}else digit8=0;
			translation = digit4+digit5+digit6+digit7+digit8;

		case '6':
			if(string[0]='1'){
				digit3=pwrs[2];
			}else digit3=0;
			if(string[1]='1'){
				digit4=pwrs[3];
			}else digit4=0;
			if(string[2]='1'){
				digit5=pwrs[4];
			}else digit5=0;
			if(string[3]='1'){
				digit6=pwrs[5];
			}else digit6=0;
			if(string[4]='1'){
				digit7=pwrs[6];
			}else digit7=0;
			if(string[5]='1'){
				digit8=pwrs[7];
			}else digit8=0;
			translation=digit3+digit4+digit5+digit6+digit7+digit8;

		case '7':
			if(string[0]='1'){
				digit2=pwrs[1];
			}else digit2=0;
			if(string[1]='1'){
				digit3=pwrs[2];
			}else digit3=0;
			if(string[2]='1'){
				digit4=pwrs[3];
			}else digit4=0;
			if(string[3]='1'){
				digit5=pwrs[4];
			}else digit5=0;
			if(string[4]='1'){
				digit6=pwrs[5];
			}else digit6=0;
			if(string[5]='1'){
				digit7=pwrs[6];
			}else digit7=0;
			if(string[6]='1'){
				digit8=pwrs[7];
			}else digit8=0;
			translation=digit2+digit3+digit4+digit5+digit6+digit7+digit8;

		case '8':
			if(string[0]='1'){
				digit1=pwrs[0];
			}else digit1=0;
			if(string[1]='1'){
				digit2=pwrs[1];
			}else digit2=0;
			if(string[2]='1'){
				digit3=pwrs[2];
			}else digit3=0;
			if(string[3]='1'){
				digit4=pwrs[3];
			}else digit4=0;
			if(string[4]='1'){
				digit5=pwrs[4];
			}else digit5=0;
			if(string[5]='1'){
				digit6=pwrs[5];
			}else digit6=0;
			if(string[6]='1'){
				digit7=pwrs[6];
			}else digit7=0;
			if(string[7]='1'){
				digit8=pwrs[7];
			}else digit8=0;
			translation=digit1+digit2+digit3+digit4+digit5+digit6+digit7+digit8;
	}

	cout << "\n" << translation;

	return 0;
}


The program does compile on MS VC++2008 express. My issue is that my translation variable is equating to an unexpected -858993460, and I don't know why.

If you would be so kind as to help me find that I did wrong, that would benefit me more than to simply suggest an alternative. Thanks in advance.
First thing I notice is that strlen returns a number, and your switch is comparing it to a character.
You shouldn't hardcode the conversion like that.
And in the switch, you are comparing length ( which is an int ) with characters.
I agree with Zhuge and Bazzy. If you check each case for just the number without the quotes this should work. Right now, none of the cases are fullfilled thus your translation variable is not initialised.
Check out the default case for dealing with an issue like this.

try using a for loop to check each specific digit.
If it is equal to '1' then get the power of 2 you need.

tip: pow(double x, double y); //use <math.h>

Also read up on debugging in MS VC++2008. It can be quite a timesaver.
Thanks for the quick response.

Changed
1
2
switch(length)
	case '1':
to
1
2
switch(length)
	case 1:
and if(string[0]='1') to if(string[0]=='1') and my program now works as intended.

Thanks for the tips joeriMJ
Topic archived. No new replies allowed.