# 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. ## 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("&#37;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. 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. ty very much