MoreFoam
MoreFoam

Reputation: 689

Why does my float print out as 0 even though I input 13.5 for it using scanf?

I'm having trouble figuring out why my float variable keeps printing out 0 when I input it as a number.

Code:

int num, month, day, year;
float price[10000];


printf("Enter item number: \n");
scanf("%d", &num);
printf("Enter unit price: \n");
scanf("%f", &price);
printf("Enter purchase date (mm/dd/yyyy): \n");
scanf("%d/%d/%d", &month, &day, &year);

printf("Item\t\tUnit\t\tPurchase\n");
printf("    \t\tPrice\t\tDate\n");
printf("%d      ", num);
printf("$%.2f     ", price); 
printf("      %d/%d/%d\n", month, day, year);
return 0;

I input 555 for my item number, 13.5 for my price, and 10/24/2010 for my date. When I did this it printed out that my price was $0.00. It does this for any number I input. Why?

Upvotes: 6

Views: 2410

Answers (4)

Iharob Al Asimi
Iharob Al Asimi

Reputation: 53006

You have declared price as an array you need to do it this way

int num, month, day, year;
float price[10000];


printf("Enter item number: \n");
scanf("%d", &num);
printf("Enter unit price: \n");
scanf("%f", &price[0]); /* <---- it's not &price it's &price[0] */
printf("Enter purchase date (mm/dd/yyyy): \n");
scanf("%d/%d/%d", &month, &day, &year);

printf("Item\t\tUnit\t\tPurchase\n");
printf("    \t\tPrice\t\tDate\n");
printf("%d      ", num);
printf("$%.2f     ", price[0]); /* <---- it's not price it's price[0] */
printf("      %d/%d/%d\n", month, day, year);
return 0;

you have to store the value in the first element of the array, and then print the first element too, i.e. price[0].

If you just want to read a single value, then you don't need to declare price as an array, so this would be the solution

int num, month, day, year;
float price/* [10000] it doesn't need to be an array */;

printf("Enter item number: \n");
scanf("%d", &num);
printf("Enter unit price: \n");
scanf("%f", &price);
printf("Enter purchase date (mm/dd/yyyy): \n");
scanf("%d/%d/%d", &month, &day, &year);

printf("Item\t\tUnit\t\tPurchase\n");
printf("    \t\tPrice\t\tDate\n");
printf("%d      ", num);
printf("$%.2f     ", price);
printf("      %d/%d/%d\n", month, day, year);
return 0;

You was printing the address to the first element of the array instead of the value at that address, and it was being converted to unsigned int or unsigned long int, so when you used the "%f" specifier, it was printing 0.

In both cases:

To prevent this kind of mistake, you should turn your compilers warnings ON, if using gcc

gcc -Wall -Wextra -Werror ${SOURCE_FILES} -o ${OUTPUT_FILE}

would do it.

And also, on invalid input, your program will have undefined behavior, you need to check that scanf() did actually read the value you instructed it to read, that is achieved by checking the return value from scanf() which equals the number of matched items, in your case

if (scanf("%d", num) != 1)
    errorInputWasInvalid();

since you are requesting 1 item, then scanf() has to return 1.

Upvotes: 2

Sourav Ghosh
Sourav Ghosh

Reputation: 134336

There are two things to notice.

  1. change float price[10000]; to float price; as you'll be using only a single float variable, so you don't need an array.

  2. You need to check the return value of scanf() to ensure proper input.

Also, as a note, you might want to initialize the local variables, as they are not automatically initialized.

Upvotes: 2

Razib
Razib

Reputation: 11163

You can not insert array value like this -

scanf("%f", &price);

Use a for loop to insert values into the array price -

for(i=0; i<sizeWhatYouWant; i++){
 scanf("%f", &price[i]);
}

Or just change the declaration float price[10000] to -

float price;

Upvotes: 4

Rizier123
Rizier123

Reputation: 59691

Just change this:

float price[10000];

to this:

float price;

Because you use it as a single variable and not as an array

Upvotes: 2

Related Questions