Why does it hang?

This is a discussion on Why does it hang? within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> unsigned int gcd(unsigned int a, unsigned int b) { unsigned int t; while (b) { t = ...

  1. #1
    Big & Little Wong Tin-Bar Jackie Chan's Avatar
    Join Date
    May 2008
    Location
    Hong Kong
    Posts
    23

    Why does it hang?

    Code:
    #include <stdio.h>
    
    unsigned int gcd(unsigned int a, unsigned int b) {
    	unsigned int t;
    	while (b) {
    		t = b;
    		b = a&#37;b;
    		a = t;
    	}
    	return a;
    }
    
    void inFlush(FILE *in) {
    	int ch;
    	while ((ch = fgetc(in)) != EOF && ch != '\n') {
    		/* f l u s h i n g */
    	}
    }
    
    char *skipSpace(char *inStr) {
    	while (*inStr == ' ' || *inStr == '\t') {
    		++inStr;
    	}
    	return inStr;
    }
    
    int main() {
    	char buff[1024];
    	char *p;
    	unsigned int a, b;
    	while (1) {
    		printf("Enter value for a & b (q will quit): ");
    		fgets(buff, sizeof(buff), stdin);
    		inFlush(stdin);
    		p = skipSpace(buff);
    		if (*p == 'q') break;
    		sscanf(p, "%u %u", &a, &b);
    		printf("GCD of %u & %u is: %u\n\n", a, b, gcd(a, b));
    	}
    	return 0;
    }
    This program just hangs even when I enter 'q' just after running
    Edit: it needs two <enter>s in a row to run further.
    Last edited by Jackie Chan; 05-24-2008 at 02:28 AM. Reason: A little typo corrected.

  2. #2
    Big & Little Wong Tin-Bar Jackie Chan's Avatar
    Join Date
    May 2008
    Location
    Hong Kong
    Posts
    23
    I fixed that myself.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unkown Hang
    By Bladactania in forum C Programming
    Replies: 31
    Last Post: 04-22-2009, 10:33 AM
  2. Hang man program hanging
    By pieisgood in forum C++ Programming
    Replies: 14
    Last Post: 02-21-2009, 06:08 PM
  3. pointers to struct, can't get the hang of it....
    By msshapira in forum C Programming
    Replies: 6
    Last Post: 01-16-2009, 03:48 AM
  4. C# and the WebClient class - application hang?
    By Devils Child in forum Networking/Device Communication
    Replies: 1
    Last Post: 01-09-2009, 11:24 AM
  5. HANG MAN i need deperate help
    By cprogrammer22 in forum C Programming
    Replies: 4
    Last Post: 11-26-2008, 11:20 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21