Thread: how do you check for a floating point vlaue in c??

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    17

    Post how do you check for a floating point vlaue in c??

    every time i enter a floating point, my program overflows , need to figure out how to test for floating point in while loop, any help is apprecited,,,,
    thnx

  2. #2
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    It sounds like you have an infinite loop, something like this maybe? :-)
    Code:
    while (scanf("%d", &some_int) != EOF)
    {
      /* Do stuff */
    }
    Post your code so I can be sure, it sounds like scanf is choking on a value other than you told it to take.
    *Cela*

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    17

    this is the code

    in the function getnum() when i enter a floating point value it infintley loops, i guess its because my value is declared as int, what i want to do is when they try to use the program and enter a f.p. it says invalid integer, also if someone has an idea on how to implement an array in factor() func that would be awesome,,,
    thnx

  4. #4
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    You avoid it by not using scanf, but if you have to, this is a fix :-)
    Code:
    int GetNum (int num1)
    {
      do
      {
        printf("Enter a positve real number to be computed\n");
        while (scanf("%d", &num1) != 1)
        {
          fprintf(stderr, "Invalid input, please try again: ");
          while (getchar() != '\n')
          {} /* Clear the bad stuff */
        }
      }
      while ( num1 <= 0 );
      
      return num1;
    }
    *Cela*

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    or use fgets() and strtol() to verify the input.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String and Floating Point Conversion Help
    By dfghjk in forum C++ Programming
    Replies: 14
    Last Post: 05-04-2008, 12:11 PM
  2. Getting a floating point exception
    By SnertyStan in forum C Programming
    Replies: 13
    Last Post: 03-25-2008, 11:00 AM
  3. floating point operators
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 10-22-2003, 07:53 PM
  4. Floating point numbers in a binary file
    By frenchfry164 in forum C++ Programming
    Replies: 6
    Last Post: 07-31-2003, 10:04 AM
  5. fixed point / floating point
    By confuted in forum Game Programming
    Replies: 4
    Last Post: 08-13-2002, 01:25 PM