> Why function decimal_binary skips the switch statement?? what is wrong with the codes?

Why function decimal_binary skips the switch statement?? what is wrong with the codes?

Posted at: 2014-12-18 
Continuing where husoski left off because comments are limited in length:

Whitespace character: the function will read and ignore any whitespace characters encountered before the next on-whitespace character (whitespace characters include spaces, newline and tab characters -- see isspace). A single whitespace in the format string validates any quantity of whitespace characters extracted from the stream (including none).

Non-whitespace character, except format specifier (%): Any character that is not either a whitespace character (blank, newline or tab) or part of a format specifier (which begin with a % character) causes the function to read the next character from the stream, compare it to this non-whitespace character and if it matches, it is discarded and the function continues with the next character of format. If the character does not match, the function fails, returning and leaving subsequent characters of the stream unread.

Format specifiers: A sequence formed by an initial percentage sign (%) indicates a format specifier, which is used to specify the type and format of the data to be retrieved from the stream and stored into the locations pointed by the additional arguments.

There are problems in general with using scanf("%c",...) for option input, mainly due to the fact that C input does not process single keystrokes. It reads whole lnes. Using fgets() into a buffer and parsing the result takes more code, but is more reliable.

However, if the user is well-behaved, you can get "good enough" results with a small change:

scanf(" %c", &choice);

That adds a single space between " and %c in the format string. That will skip over any number of spaces, tabs or newlines to find a nonblank character. In particular, it will skip over the \n character than ended the previous input line when you have multiple menu prompts.

Function decimal_binary should access another function in which the calculations are made but it is skipping the switch statement. what is wrong with the codes???

#include

#include

extern decimal_binary(void);

extern decimal_octal(void);

extern decimal_hexadecimal(void);

extern binary_octal(void);

extern binary_hexadecimal(void);

extern octal_hexadecimal(void);

extern binary1(int);

int main(void)

{

char choice;





printf("\t SELECT AN OPTION \n\n\t 1.DECIMAL_BINARY\n\n\t 2.DECIMAL_OCTAL\n\n\t 3.DECIMAL_HEXADECIMAL\n\n\t 4.BINARY_OCTAL\n\n\t 5.BINARY_HEXADECIMAL\n\n\t 6.OCTAL_HEXADECIMAL\n\n\t");

scanf("%c",&choice);

switch(choice)

{

case'1':

decimal_binary();

break;



case'2':

decimal_octal();

break;



case'3':

decimal_hexadecimal();

break;



case'4':

binary_octal();

break;



case'5':

binary_hexadecimal();

break;



case'6':

octal_hexadecimal();

break;



}

system("pause");

return;

}

****************************************************************************************

#include

#include

extern decimal_binary(void)

{

system("cls");



char choice;

int bin1;

printf("\t SELECT AN OPTION\n\n\n\t 1. CONVERT DECIMAL TO BINARY\n\n\n\t 2. CONVERT BINARY TO DECIMAL\n\n\n\t");

scanf("%c",&choice);

switch(choice)

{

case'1':

binary1(bin1);

break;



}

}