scanf acting strange...

This is a discussion on scanf acting strange... within the C Programming forums, part of the General Programming Boards category; In my program, i ask for 1 number and 3 prices. Then i multiply each price with there respective number ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    49

    scanf acting strange...

    In my program, i ask for 1 number and 3 prices.

    Then i multiply each price with there respective number of articles.

    Then print out the amout of items and there cost.

    Here is my ouput.
    -------------------------------------------------------------------------------------------------
    Entrez votre besoin journalier d'energie maximale en kilo watt heure (kWh).
    25

    Entrez le prix ($) des batteries de 4.8 volt.
    1730.57

    Entrez le prix ($) des convertisseurs de 5 kilo watt.
    509.01

    Entrez le prix ($) des panneaux photovoltaique en metre carre.
    899.99

    2 6 48 //The amout of item 1, 2, and 3.
    1730.569946 ; 509.010010 ; 899.989990 ; //The cost of item 1, 2, and 3
    //The starnge thing is that those costs arent exactly the ones i entered.

    Afin d'installer un bloc d'alimentation electrique photovoltaique a panneaux pla
    ts pour l'utilisation residentielle a Ottawa, ON, vous aurez besoin de:

    6 Batteries (4.800000 V) = 10383.419922 $
    2 Convertisseurs (5.000000 KW) = 1018.020020 $
    48 Panneaux photovoltaique (m^2) = 43199.519531 $

    Pour un prix total de: 54600.960938 $
    //And then when i do the total cost, i have to many decimals....

    Press any key to continue . . .

    Here is the fonction that uses scanf, can somedy tell me how i can fix this problem or is there nothing that i can do about it?

    Code:
    float validate_input(int disperror)
    {
          float choice;
          int numchar = 0;
          char c;
    
          if(disperror == 1)
                 {
                       printf("\nErreur. Entrez une nouvelle valeur.\n");
                 }
    
          fflush(stdin);
          scanf("%f", &choice);
    
          do 
          {
          scanf("%c", &c);
          numchar += 1 ;
          } while(c != '\n');
    
          if(numchar != 1)
                 {
                       choice = validate_input(1);
                 }
    
          if(choice <= 0)
                 {
                       choice = validate_input(1);
                 }
    
          return choice;
    }

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    read a your decimal values uisng the double data type and read them uisng the &#37;ld format specifier. And about the no of decimal places

    Code:
    printf("%.2f", num);   // would get u to 2 decimal places after the point
    And dont use fflush function read FAQ to understand why.

    ssharish

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    49
    ty very much

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. code acting a bit strange
    By Labmouse in forum C++ Programming
    Replies: 6
    Last Post: 08-24-2007, 01:59 PM
  2. scanf with two inputs acting funny
    By yougene in forum C Programming
    Replies: 2
    Last Post: 08-19-2007, 05:17 PM
  3. Strange scanf behavior
    By exvor in forum C Programming
    Replies: 7
    Last Post: 07-25-2005, 12:54 PM
  4. Strange behaviour of scanf()
    By chris1985 in forum C Programming
    Replies: 3
    Last Post: 01-15-2005, 02:41 PM
  5. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 05:34 AM

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