Thread: infinite loop kicks in

  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,708
    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;
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  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,708
    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
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  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,708
    Really? Well, in that case, the bottom line is: "Always perform a sanity check on your fgets() ret val"!!! Thanks for the tip, Hammer.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

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. 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