0

The print item.price(printf("\t%f",item1.price); and printf("\t%f",item2.price);)at the bottom always returns a value of 0.000 as shown in the image.What can i do to fix it?

struct item { //declare the structure
  int itemNo;
  double price;
  int quantity;
}item1,item2;
#include <stdio.h>
int main(void)//start of main function
{
    struct item item1,item2; //declare structure variables
    printf("Enter first item number:"); //prompt
    scanf("%d",&item1.itemNo); //input
    printf("Enter first item price:"); //prompt
    scanf("%f",&item1.price); //input
    printf("Enter first item quantity:"); //prompt
    scanf("%d",&item1.quantity); //input
    printf("Enter second item number:"); //prompt
    scanf("%d",&item2.itemNo); //input
    printf("Enter second item price"); //prompt
    scanf("%f",&item2.price); //input[enter image description here][1]
    printf("Enter second item quantity:");//prompt
    scanf("%d",&item2.quantity); //input
    printf("ItemNo\tPrice\tQuantity\n");//display heading
    printf("%d",item1.itemNo);
    printf("\t%f",item1.price);
    printf("\t%d",item1.quantity,"\t");//display item1 values
    printf("\n%d",item2.itemNo);
    printf("\t%f",item2.price);
    printf("\t%d",item2.quantity);//display item1 values
    
    
    return 0;
    
} //end of main function

Error image

Axii
  • 9
  • 2
  • This is probably not the cause of the problem, but there is an extra `"\t"` parameter in the `printf` that displays the item1 quantity. – Ian Abbott Feb 03 '22 at 10:15

2 Answers2

2

You should try like this.

%lf : Scan as a double floating-point number. "Float" format with the "long" specifier.

scanf("%lf",&item1.price);
printf("\t%f",item1.price);
tmsbrndz
  • 1,297
  • 2
  • 9
  • 23
0

Since you're using a double variable for price, you need to scanf with %lf modifier like this:

    scanf("%lf",&item1.price); //input
    scanf("%lf",&item2.price); //input
Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292