Program hex to binary, err coming!

i am begineer in c++, please tell why the following program is not working.


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
//Program to convert hexadecimal to binary .
#include<iostream.h>
#include<conio.h>
void main(){
clrscr();
//Code starts from here



//Function binary;
char[] binaryFunction(char[10] hex){
char binary[8];
for(int i=2;i<10;i++){
if(hex[i]=='0')binary[i-2]="0000";
if(hex[i]=='1')binary[i-2]="0001";
if(hex[i]=='2')binary[i-2]="0010";
if(hex[i]=='3')binary[i-2]="0011";
if(hex[i]=='4')binary[i-2]="0100";
if(hex[i]=='5')binary[i-2]="0101";
if(hex[i]=='6')binary[i-2]="0110";
if(hex[i]=='7')binary[i-2]="0111";
if(hex[i]=='8')binary[i-2]="1000";
if(hex[i]=='9')binary[i-2]="1001";
if(hex[i]=='a')binary[i-2]="1010";
if(hex[i]=='b')binary[i-2]="1101";
if(hex[i]=='c')binary[i-2]="1100";
if(hex[i]=='d')binary[i-2]="1101";
if(hex[i]=='e')binary[i-2]="1110";
if(hex[i]=='f')binary[i-2]="1111";



}
    return binary;

}
int age[5];int i=0;

while(i<5){

cout<<"Binary for the memory address :"<<&age[i]<<" is :"<<binaryFunction(&age[i])<<endl;
i++;

	 }


getch();
}
You can't return arrays from functions.
@rondo

do you know the difference between a string and a character?
@kfmfe04
ya i do know the difference, string is the array of characters.

so is there any alternative i can solve the above problem...............if I cannot return array from functions.


PS : I am also getting the error when I am writing this &age[i], but according to me this shudn't be a err. ! WHy the compiler is saying it an err.
ok, then for Lines 14-29, what is the type of binary[i-2] and what is the type of "0000"?

are they the same type?

you are obviously not compiling your program as you go along - I highly recommend that you start over clean, and add small bits of your program at a time, making 100% sure that they compile before you go on...

the problem is, the C++ that's in your head is nothing like the C++ that the compiler is expecting
"ok, then for Lines 14-29, what is the type of binary[i-2] and what is the type of "0000"?

are they the same type?"

yes i think they are the same type, both are arrays of characters as far as i know, actually i had learnt java first and then now i am learning c++, so sometimes its bit confused .... !
sorry if i am wrong ...............

And ya i write the whole program first and then compile in the end, he he
will try to follow your suggestion to compile bit by bit !
no, they are not the same type (hence, you can't assign them like that)

binary[i-2] is a char
"0000" is a const char*

you will have to unlearn many things you learned in Java, but that stuff hasn't come up yet - you are still working on the C part of C++
Last edited on
Topic archived. No new replies allowed.