result will not print to the screen

Hi, I am writing this program for my class that converts decimal numbers to binary, binary to decimal, and finally decimal to hex...however i have come across a problem with my program. The code complies, but the result doesn't print to the screen. can someone help me out with this? thanks! the code is below.

#include<iostream>
#include<cmath>
#include<string>
using namespace std;

int main()
{
int run=1;
while(run=1)
{

int c;
cout<<"Enter the type of conversion you wish to perform. 1=Decimal to Binary, 2=Binary to Decimal, 3=Decimal to Hex: ";
cin>>c;

//Decimal to Binary
switch(c)
{
case 1:
{
int quotent, remainder, counter=0, binary[32];
cout<<"Enter a decimal number: ";
cin>>quotent;
while(quotent > 0)
{
remainder=quotent % 2;
quotent=quotent / 2;
binary[counter]=remainder;
counter++;
}
for(int x=counter-1; x<0; x--)
{
cout<<binary[x];
}
}
break;

//Binary to Decimal
case 2:
{
string binary;
double place,total;
int ones=0;
cout<<"Enter a binary number: ";
cin>>binary;
for(int x=0; x=binary.length(); x++)
{
place=binary.length()-1-x;
total=total+pow(2,place);
}
cout<<"The number in decimal form is: "<<total<<endl;
}
break;

//Decimal to Hex
case 3:
{
int quotent, remainder, counter=0, hex[32];
cout<<"Enter a decimal number: ";
cin>>quotent;
while(quotent > 0)
{
remainder=quotent % 16;
quotent=quotent / 16;
hex[counter]=remainder;
counter++;
}
for(int x=counter-1; x<0; x--)
{
if(hex[x]=10)
cout<<"A";
if(hex[x]=11)
cout<<"B";
if(hex[x]=12)
cout<<"C";
if(hex[x]=13)
cout<<"D";
if(hex[x]=14)
cout<<"E";
if(hex[x]=15)
cout<<"F";
else
cout<<hex[x];
}
}
break;

default:
{
cout<<"INVALID OPTION!";
}
break;
}
cout<<endl;
cout<<"if you wish to run the program again, press 1: ";
cin>>run;
cout<<endl;
}
}
closed account (D80DSL3A)
I fixed your code somewhat. There is output now. This should make it easier for you to troubleshoot the rest of your programs issues. I think the decimal to binary part works right now.
The Decimal to Hex part suffers from a logical problem with the use of if/else in the output( eg. I tried it for c = 30. Output should be 1E but it gives 1E14, do you see why?)
I didn't try to troubleshoot the Binary to Decimal part, except to correct the warning I got from the compiler because total was being used without being initialized. Hope this helps!

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
#include<iostream>
#include<cmath>
#include<string>
using namespace std;

int main()
{
int run=1;
//while(run=1)
while(run==1)// fix 4 above
{

int c;
cout<<"Enter the type of conversion you wish to perform. 1=Decimal to Binary, 2=Binary to Decimal, 3=Decimal to Hex: ";
cin>>c;

//Decimal to Binary
switch(c)
{
case 1:
{
int quotent, remainder, counter=0, binary[32];
cout<<"Enter a decimal number: ";
cin>>quotent;
while(quotent > 0)
{
remainder=quotent % 2;
quotent=quotent / 2;
binary[counter]=remainder;
counter++;
}
//for(int x=counter-1; x<0; x--)
for(int x=counter-1; x>=0; x--)// fix 4 above
{
cout<<binary[x];
}
}
break;

//Binary to Decimal
case 2:
{
string binary;
//double place,total;
double place,total=0;// fix 4 above
int ones=0;
cout<<"Enter a binary number: ";
cin>>binary;
for(int x=0; x=binary.length(); x++)
{
place=binary.length()-1-x;
total=total+pow(2,place);
}
cout<<"The number in decimal form is: "<<total<<endl;
}
break;

//Decimal to Hex
case 3:
{
int quotent, remainder, counter=0, hex[32];
cout<<"Enter a decimal number: ";
cin>>quotent;
while(quotent > 0)
{
remainder=quotent % 16;
quotent=quotent / 16;
hex[counter]=remainder;
counter++;
}
for(int x=counter-1; x>=0; x--)// fixed
{
if(hex[x]==10)// fixed
cout<<"A";
if(hex[x]==11)// fixed
cout<<"B";
if(hex[x]==12)// fixed
cout<<"C";
if(hex[x]==13)// fixed
cout<<"D";
if(hex[x]==14)// fixed
cout<<"E";
if(hex[x]==15)// fixed
cout<<"F";
else
cout<<hex[x];// logic problem here
}
}
break;

default:
{
cout<<"INVALID OPTION!";
}
break;
}
cout<<endl;
cout<<"if you wish to run the program again, press 1: ";
cin>>run;
cout<<endl;
}
}
thanks :) yes it did, and I think I can work out the rest of the problems on my own!
It works!
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include<iostream.h>
#include<conio.h>
int power(int,int);
void dectobin();
void bintodec();
void dectohex();
void hextodec();
void main()
{
clrscr();
int x;
char y;
start:
cout<<"What conversion do you want to do?";
cout<<"\nEnter 1 for Decimal to Binary";
cout<<"\nEnter 2 for Binary to Decimal";
cout<<"\nEnter 3 for Decimal to Hexadecimal";
cout<<"\nEnter 4 for Hexadecimal to Decimal\n";
cin>>x;
switch(x)
{
case 1:
dectobin();
break;
case 2:
bintodec();
break;
case 3:
dectohex();
break;
case 4:
hextodec();
break;
default:
cout<<"\nPlease Enter Number from 1-6";
break;
}
cout<<"\nDo you want to continue:\nEnter Y for Yes\nEnter N for No\n";
cin>>y;
if(y=='Y'||y=='y')
{
goto start;
}
getch();
}
int power(int x,int y)
{
int z=1;
while(y>0)
{
z=z*x;
y--;
}
return z;
}
void dectobin()
{
int n,i,j,a,b[100];
cout<<"Enter the Number to be converted into Binary:";
cin>>n;
cout<<"\nThe Binary of "<<n<<" is:";
if(n==0)
{
cout<<0;
}
cout<<1;
for(i=0;n!=1;i++)
{
a=n%2;
b[i]=a;
n=n/2;
}
for(j=i-1;j>=0;j--)
{
cout<<b[j];
}
}
void bintodec()
{
int n,i,j,a[100],b[100],sum=0;
cout<<"Enter Binary Number\n";
cin>>n;
for(i=0;i<1;i++)
{
a[i]=n;
}
for(i=0;a[i]!=0;i++)
{
b[i+1]=a[i]%10;
a[i+1]=a[i]/10;
}
while(i>0)
{
j=i-1;
sum=sum+(b[i]*power(2,j));
i--;
}
cout<<"\nThe no. in decimal is:"<<sum;
}
void dectohex()
{
int n,i,j,a,b[100];
char c[100];
cout<<"Enter the Number to be converted into Hexadecimal:";
cin>>n;
cout<<"\nThe Hexadecimal of "<<n<<" is:";
for(i=0;n!=0;i++)
{
a=n%16;
b[i]=a;
n=n/16;
}
for(j=i-1;j>=0;j--)
{
if(b[j]==10)
{
cout<<"A";
}
else if(b[j]==11)
{
cout<<"B";
}
else if(b[j]==12)
{
cout<<"C";
}
else if(b[j]==13)
{
cout<<"D";
}
else if(b[j]==14)
{
cout<<"E";
}
else if(b[j]==15)
{
cout<<"F";
}
else
{
cout<<b[j];
}
}
}
void hextodec()
{
int n,i,j,sum=0,b[100];
char a[100];
cout<<"\nEnter number of digits in your hexadecimal number.\n";
cin>>n;
cout<<"\nEnter hexadecimal number.\n";
for(i=1;i<=n;i++)
{
cin>>a[i];
}
for(i=1;i<=n;i++)
{
if(a[i]=='0')
{
b[i]=0;
}
if(a[i]=='1')
{
b[i]=1;
}
if(a[i]=='2')
{
b[i]=2;
}
if(a[i]=='3')
{
b[i]=3;
}
if(a[i]=='4')
{
b[i]=4;
}
if(a[i]=='5')
{
b[i]=5;
}
if(a[i]=='6')
{
b[i]=6;
}
if(a[i]=='7')
{
b[i]=7;
}
if(a[i]=='8')
{
b[i]=8;
}
if(a[i]=='9')
{
b[i]=9;
}
if(a[i]=='A'||a[i]=='a')
{
b[i]=10;
}
if(a[i]=='B'||a[i]=='b')
{
b[i]=11;
}
if(a[i]=='C'||a[i]=='c')
{
b[i]=12;
}
if(a[i]=='D'||a[i]=='d')
{
b[i]=13;
}
if(a[i]=='E'||a[i]=='e')
{
b[i]=14;
}
if(a[i]=='F'||a[i]=='f')
{
b[i]=15;
}
}
j=n-1;
for(i=1;i<=n;i++)
{
sum=sum+(b[i]*power(16,j));
j--;
}
cout<<"\nThe number in decimal is "<<sum;
}
Topic archived. No new replies allowed.