infinite loop kicks in

This is a discussion on infinite loop kicks in within the C Programming forums, part of the General Programming Boards category; I can't see what's wrong here, any idea why when I enter q ( which should jump out of the ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    137

    infinite loop kicks in

    I can't see what's wrong here, any idea why when I enter q ( which should jump out of the loop ) does the program begin an infinite loop:

    Code:
    do {
          title (); // a self-defined function for printing text
          printf("\nPlease enter the cost of the item(s)\n");
          printf("(Press q to finish)\n:");
          scanf("%f",&price);
          clrscr();
          cost += price;
          }
          while ( price != 'q' );
    http://uk.geocities.com/ca_chorltonkids

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You can't store letters in a float variable.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,657
    You are scanning a float and then testing it for a character value? That just won't work.
    You will have to either:
    -scan a 'q' and test it for the value that scanf returns when expecting a float, and then use this value in place of the current 'while' condition,
    -use fgets, and test it for q, then transform it with a call to atof, (which is probably better, anyway).

    And actually, since in this case, we know noone will enter '0' for a price, we can exploit the fact that atof returns 0 if a char-containing string is passed into it.

    Like this:

    if( (price = atof( fgets(chPrice, 15, stdin)) == 0 )
    break;

    cost += price;



    ITSA
    Socket Library!

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >if( (price = atof( fgets(chPrice, 15, stdin)) == 0 )
    Be careful doing this, if fgets() returns NULL, then your program will crash.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,657
    Well, with file streams, this will probably happen, and often But with keyboard input, I have never had such a problem, EVER. You will always get *at least* a newline. But good eye



    ITSA
    Socket Library!

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    I agree Seb, but I never trust a user to do the right thing! They can hit CTRL+D or CTRL+Z on a blank line and it will force a NULL from fgets().
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,657
    Really? Well, in that case, the bottom line is: "Always perform a sanity check on your fgets() ret val"!!! Thanks for the tip, Hammer.



    ITSA
    Socket Library!

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, 03: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. stays in loop, but it's not an infinite loop (C++)
    By Berticus in forum C++ Programming
    Replies: 8
    Last Post: 07-19-2005, 11:17 AM

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