Problem with scanf float..

This is a discussion on Problem with scanf float.. within the C Programming forums, part of the General Programming Boards category; I am creating a database that stores details of newspapers.. When I am trying to add a record, I want ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    2

    Problem with scanf float..

    I am creating a database that stores details of newspapers..

    When I am trying to add a record, I want to allow the user to enter the price of the newspaper.

    This is the code that I am using:

    Code:
       printf("\n\n\tPlease enter the weekly price of the newspaper: ");
       fflush(stdin);
       scanf("%f",paper[toprec2].price);
    However, whenever the program reaches this section the program just closes.


    Can anyone suggest why this is happening and what can be done to solve it?


    Thanks

  2. #2
    Registered User
    Join Date
    Mar 2005
    Posts
    18
    hi guy!
    Code:
       printf("\n\n\tPlease enter the weekly price of the newspaper: ");
       fflush(stdin);
       scanf("%f",&paper[toprec2].price);

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    2
    Someone else found out that this was the problem: http://www.faqs.org/faqs/msdos-progr...section-5.html

    After adding this:

    Code:
      static void forcefloat(float *p)
       {
         float f = *p;
         forcefloat(&f);
       }
    It works

  4. #4
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Please don't use fflush(stdin) it's undefined and it was discussed number of times. Please read FAQ on this.
    I think you could just add this
    Code:
    float dummy = 1.0f;
    instead of function and it will work.
    Last edited by Micko; 03-06-2005 at 08:53 AM.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by aydin1
    Someone else found out that this was the problem: http://www.faqs.org/faqs/msdos-progr...section-5.html

    After adding this:

    Code:
      static void forcefloat(float *p)
       {
         float f = *p;
         forcefloat(&f);
       }
    It works
    And what exactly is this supposed to do? Run you out of stack space? You know this will never end until that happens, right? And when that happens, your program dies.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by quzah
    And what exactly is this supposed to do? Run you out of stack space? You know this will never end until that happens, right? And when that happens, your program dies.
    http://www.faqs.org/faqs/msdos-progr...section-5.html
    To do that, define this function somewhere in a source file but don't call it:
    Code:
      static void forcefloat(float *p)
       {
         float f = *p;
         forcefloat(&f);
       }
    Not the best workaround, but at least it has a disclaimer.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    They never said they were getting an error, but that the "program just closes". The link provided was to "get around" a floating point not linked error. Nothing in their post says anything about that. It just says the program closes.

    Quzah.
    Last edited by quzah; 03-06-2005 at 01:37 PM.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Promblem with code
    By watchdogger in forum C Programming
    Replies: 18
    Last Post: 01-31-2009, 06:36 PM
  2. Need help with program
    By HAssan in forum C Programming
    Replies: 8
    Last Post: 06-10-2007, 09:05 PM
  3. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-24-2007, 12:05 AM
  4. Debug Error Really Quick Question
    By GCNDoug in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 01:05 PM
  5. help me
    By warthog89 in forum C Programming
    Replies: 11
    Last Post: 09-30-2006, 09:17 AM

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