First, tell us what errors do you get
* Compiler errors? If so, copy-paste them all, with line numbers.
* Run-time errors? If so, what input do you give? What output do you get? What output do you expect to get?
* Does it crash? What messages do you see? How far does it seem to get?
Then, fix your indentation and formatting. If your code is hard to read, it's easy to make mistakes and hard to find or fix them. It should look more like this:
Code:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define CONVERSION_ERROR -1
#define NO_ERROR 0
int main (void) {
char byte[9], numeral[2], hex[3];
short bit;
int i, decimal_value = 0;
char hex_look_up[6] = {'A', 'B', 'C', 'D', 'E', 'F'};
numeral[1] = 0;
for (i = 7, bit = 0; i >= 0; i--, bit++) {
printf("Enter 1 or 0 for bit number %d: ", bit);
gets(numeral);
if (numeral[0] == '0' || numeral[0] == '1') byte[i] = numeral[0];
else {
printf("Error on input: not a 1 or 0\n");
exit(1);
}
printf("\n");
}
byte[8] = 0;
printf ("The binary value in the byte is %s\n", byte);
int convertBinToHex (const char * byte, char * hexNo);
int error = NO_ERROR;
int lower_end = convertBinToDec(byte,7,4);
if (lower_end>9)
hexNo[0] = hex_look_up[10-lower_end];
int upper_end = convertBinToDec(byte,3,0);
if (upper_end>9)
hexNo[1] = hex_look_up[10-upper_end];
hexNo[2] = '\0';
return error;
printf("Hexadecimal value is %s",hexNo);
}
Next, compile at the maximum warning level (e.g. for gcc use the -Wall flag). If you do, you should get the following:
Code:
$ make foo
gcc -Wall -ggdb3 -std=c99 -O0 -o foo foo.c -lm -lpthread -lrt
foo.c: In function ‘main’:
foo.c:34:3: warning: implicit declaration of function ‘convertBinToDec’ [-Wimplicit-function-declaration]
foo.c:36:5: error: ‘hexNo’ undeclared (first use in this function)
foo.c:36:5: note: each undeclared identifier is reported only once for each function it appears in
foo.c:15:10: warning: unused variable ‘decimal_value’ [-Wunused-variable]
foo.c:13:29: warning: unused variable ‘hex’ [-Wunused-variable]
make: *** [foo] Error 1
1. The error ('hexNo' undeclared) actually prevents your code from compiling. If you want to use a variable, you must declare it.
2. The first warning is because convertBinToDec doesn't exist. Also, you only have a prototype for convertBinToHex, and the prototype should be outside any functions. Usually near the top of the file, just before you actually define your functions.
3. The last 2 warnings are because
Also, note that the last printf will never execute, since it's after the return statement.
Work on all that, and when you get back with specifics on your error, I'll be able to help you more.