Thread: Infinite Loop of Doom!

  1. #1
    Registered User
    Join Date
    Oct 2008

    Infinite Loop of Doom!

    int T;
    char file[30];
    FILE *fp;
    if(argc <= 2)
    		printf("Please enter the number of threads to be created: ");
    		if(scanf("%d", &T) == 1)
                              }//end of if(scanf("%d", &T) == 1)
    			T = -2;
    	}//end of while(true)
    }//end of if(argc <= 2)
    Ok, the probem is with scanf("%d", &T) == 1).
    T is an integer.
    If I input a string instead of an int, my program goes into an inifinit loop.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    Because scanf will NEVER read anything other than what it accepts as valid input. So if you have a %d format, "abcd" as input will stay in the input buffer forever. If you want to continue reading valid data, then you need to clean the input buffer. See the FAQ for an example of how to do that.

    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Quote Originally Posted by matsp View Post
    Because scanf will NEVER read anything other than what it accepts as valid input.

    If I input an integer I can still read another valid input.
    FAQ it is.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Rishon LeZion, Israel
    Quote Originally Posted by jordanguyoflove View Post
    If I input an integer I can still read another valid input.
    FAQ it is.
    What is your point?

    if scanf returns 0 - there is junk in the stream - get rid of it. FAQ should have samples.

    if scanf returns EOF - nothing more to read - get out of your loop
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  5. #5
    Join Date
    Oct 2007
    Inside my computer
    Also improve your indentation. The if statement is borked.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Complete Beginner
    Join Date
    Feb 2009
    A closing brace doesn't need to reference its opening control statement in a comment. It's a bad idea for at least three reasons:

    1) It makes no sense to put code into comments. Use comments for explanations, not redundancy. You also wouldn't use "exit(1); // exit(1)".
    2) Indentation already accomplishes the same thing for free.
    3) Not everyone uses a fancy IDE with syntax highlighting and comment hiding. If comments are not distinguishable from code by their color, your code is hard to read. This will probably do more harm to you than to the other readers.

    To put it short, you have more work and gain nothing.

    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-14-2009, 11:24 PM
  2. Cosine fucntion and infinite loop.
    By youareafever in forum C Programming
    Replies: 2
    Last Post: 11-07-2008, 04:45 AM
  3. Infinite Loop with GetAsyncKeyState
    By guitarist809 in forum Windows Programming
    Replies: 1
    Last Post: 04-18-2008, 12:09 PM
  4. Switch statement = infinite loop
    By Lucid003 in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2005, 12:46 AM
  5. question ab an infinite loop...
    By JohnMayer in forum C Programming
    Replies: 10
    Last Post: 07-26-2002, 10:15 AM