Hi there have been working on a project where I am asked to create a program that can accept input and give output.

I need to be able to reconise input as binary or decimal or error..

then out put the answer.

If input is Decimal then output is Binary equivalent.

If input is Binary then output is Decimal equivalent.

I have managed everything Up till the Decimal to Binary.. I Know how to do this on paper however I am at a point where I just cannot get the correct answer for this conversion.

Any Suggestions or nudge in the right direction would be greatly appreciated..

Here is my code so far... sorry its a little messy ill get round to cleaning it up after i have figured out this issue.. lol

Code:

#include <stdio.h>
#include <string.h>
int main() {
char s[80];
char i[80];
char type=0;
int len;
int two;
int counter=1 ;
int sum = 0;
int index=0;
printf ("Type in a number: ");
gets (s);
if (s[0] == '0'){
type = 'b';
} else if (s[0]>='2' && s[0]<='9'){
type = 'd';
} else if (s[0] == '1') {
type = '\0';
} else {
type = 'i';
}
if (type != 'i') {
while (s[counter]!='\0'){
if (s[counter]>='2' && s[counter]<='9'){
type = 'd';
}else {
if (s[counter]!='0' && s[counter]!='1') {
type = 'i';
break;
} else {
if (type != 'b') {
type = '\0';
}
}
}
if (type=='d' && s[0]=='0'){
type = 'i';
break;
}
counter++;
}
}if (type=='\0'){
printf ("is this number decimal (d) or binary (b)");
gets (i);
type = i[0];
}
// print number into variable, if n > 255 or n < 0 type == 'i'
if(type=='b') {
len = strlen (s)-1;
two = 1;
while (len >= 0) {
if (s[len] == '1') {
sum += two;
}
two = two*2;
len--;
}
printf("Converting binary to decimal. Answer is: %d\n", sum);
} else if(type=='d') {
/*this is where the conversion for decimal to binary will happen..
when figured out... lol */
}
printf("number: %d", sum);
} else if(type=='i') {
printf("That is an invalid number!");
}
return 0;
}

Again any help would be appreciated.

regards