Thread: fgets reads byherself data

  1. #1
    Registered User Phoenix_Rebirth's Avatar
    Join Date
    Dec 2005
    Location
    Cyprus
    Posts
    68

    fgets reads byherself data

    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.

  2. #2
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    Quote Originally Posted by Phoenix_Rebirth
    Code:
    int check_bin(char *opr,int check)
    {
     	while(*opr!='\0')	{
    		check=(*opr=='0')||(*opr=='1')?1:0;
    		if (check=0)		
    			break;
    	}
    	return check;
    }
    I'm not going to say much, as this is supposed to be teaching you something...

    Your loop here never increments the pointer, so it always looks at the same character, so if it is valid (that is, a '0' or a '1'), you'll never exit the loop.
    Insert obnoxious but pithy remark here

  3. #3
    Registered User Phoenix_Rebirth's Avatar
    Join Date
    Dec 2005
    Location
    Cyprus
    Posts
    68
    Hmmmm.... You are right. Actually I must have missed it in the hurry but the fact remains that for some reason the fgets reads something the first time and the program goes on and calls the function and therefore the loop. What I want to know is why it does that. I even tried fflush(stdout) in case of smth in the buffer but nothing. If i run it the
    " printf("\n\nOperand No1(Up to 24 bits)\nRemember,only '0' and '1': "); "
    command is executed twice meaning it has already done one loop.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659

  5. #5
    Registered User Phoenix_Rebirth's Avatar
    Join Date
    Dec 2005
    Location
    Cyprus
    Posts
    68
    Yes. You are right I completely forgot about that.... Thanks a lot

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    int check_bin(char *opr,int check)
    {
     	while(*opr!='\0')	{
    		check=(*opr=='0')||(*opr=='1')?1:0;
    		if (check=0)
    			break;
    	}
    	return check;
    }
    Your if(check=0) should be if(check==0). And why do you pass in check? It looks like it could be a local variable.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Where's the EPIPE signal?
    By marc.andrysco in forum Networking/Device Communication
    Replies: 0
    Last Post: 12-23-2006, 08:04 PM
  2. Errors
    By Rhidian in forum C Programming
    Replies: 10
    Last Post: 04-04-2005, 12:22 PM
  3. Binary Tree, couple questions
    By scoobasean in forum C Programming
    Replies: 3
    Last Post: 03-12-2005, 09:09 PM
  4. Replies: 1
    Last Post: 07-31-2002, 11:35 AM
  5. C Programming Question
    By TK in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 07-04-2002, 07:11 PM