First I apologize for taking so much space. So this is my problem...
I am writing this little programm for a mock exam and i am supposed to create functions for bitwise operations (smth like a veeeeeery simple binary calculator). Anyway I used the fgets function within a while loop. Right after I call another function which is basicly a loop, and as far as I have understood the fgets reads somth byhereself and goes ino an infite loop.Here is the code (I have deleted the parts not interested...)
Code:
#include <stdio.h>
#include <stdlib.h>
#define bit_len 24
void decimal();
void binary();
main()
{
int choice;
printf("Will you use the binary(0) or the decimal(1) system: ");
scanf("%1d",&choice);
switch (choice) {
case 0: binary(); break;
case 1: decimal(); break;
default: printf ("Min ise malakas, vale 1 i 0... ;-(\n\n"); break;
}
system("pause");
}
void binary()
{
int i,check=0;
char operand1[bit_len],operand2[bit_len];
char *ptr;
do {
printf("\n\nOperand No1(Up to 24 bits)\nRemember,only '0' and '1': ");
fgets(operand1,sizeof(operand1),stdin);
check=check_bin(operand1,check);
} while(check!=1);
printf("%d",check);
}
int check_bin(char *opr,int check)
{
while(*opr!='\0') {
check=(*opr=='0')||(*opr=='1')?1:0;
if (check=0)
break;
}
return check;
}
void decimal()
{}
You can run it (It runs as itis) and see foryoureself.
If someone can help I would really appreciate it.