Ok, so I am trying to create a program that takes as an input a binary number, 1-10 digits. My program will take this input, which will have no spaces between the 1's and 0's and return a decimal number. My current program, which is appended, fails my test to check whether or not the numbers are 1's and 0's. Anyone have any ideas as to where I have logic errors? Also, I get the error:
"In function 'conv'
105:warning: too many arguements for format"
It still compiles, but I dont know what this means.
Anyway, here it is, any tips and pointers would be appreciated. Also, if there is a better way to approach this problem, let me know.
Thanks again in advance,Code:#include <stdio.h>
#include <math.h>
#include <stdlib.h>
enum Status {VALID, NONVALID};
int conv(int x);
int main()
{
int binary,temp,temp2,i,orig;
enum Status bin=NONVALID;
while(NONVALID==bin)
{
printf("Enter a binary number(1-10 digits):");
scanf("%d",&binary);
temp=binary=orig;
for(i=1;i<11;i++)
{
temp2=temp%10;
if(temp2==0||temp2==1)
{
temp=floor(temp/10);
}
else
{
break;
}
}
if(i==11)
{
bin=VALID;
}
else
{
bin=NONVALID;
printf("Sorry: each digit must be a 1 or a 0!");
}
}
conv(binary);
return 0;
}
int conv(int x)
{
int decval=0;
int temp,j,orig,temp2,binary;
for(j=1;j<=10;j++)
{
if(1==temp)
{
switch(j)
{
case 1:
decval+=1;
break;
case 2:
decval+=2;
break;
case 3:
decval+=4;
break;
case 4:
decval+=8;
break;
case 5:
decval+=16;
break;
case 6:
decval+=32;
break;
case 7:
decval+=64;
break;
case 8:
decval+=128;
break;
case 9:
decval+=256;
break;
case 10:
decval+=512;
break;
}
}
binary=floor(binary/10);
}
printf("%%d in binary is %d in decimal",orig,decval);
return 0;
}
Evan