Infinite Loop of Doom!

This is a discussion on Infinite Loop of Doom! within the C Programming forums, part of the General Programming Boards category; Code: int T; char file[30]; FILE *fp; if(argc <= 2) { while(1) { printf("Please enter the number of threads to ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    75

    Infinite Loop of Doom!

    Code:
    int T;
    char file[30];
    
    FILE *fp;
    
    
    if(argc <= 2)
    {
    	while(1)
    	{
    		printf("Please enter the number of threads to be created: ");
    		if(scanf("%d", &T) == 1)
    			{
    			 
                                //stuff
                                break;			
    
                              }//end of if(scanf("%d", &T) == 1)
    			else
    			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.
    WHY???

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

    --
    Mats
    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
    Posts
    75
    Quote Originally Posted by matsp View Post
    Because scanf will NEVER read anything other than what it accepts as valid input.

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

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,014
    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
    Posts
    312
    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.

    Greets,
    Philip
    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-15-2009, 12:24 AM
  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, 01:09 PM
  4. Switch statement = infinite loop
    By Lucid003 in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2005, 01:46 AM
  5. question ab an infinite loop...
    By JohnMayer in forum C Programming
    Replies: 10
    Last Post: 07-26-2002, 11:15 AM

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