0

I'm a college student learning code C and I have some problem with my C assignment (Managing products). My code below has problem with the output. The prices of products always are bunch of 0.00 no matter what are the input. What's wrong with that?? and BTW, how to apply this How to sort an array of structs in C? into my code?? My program would sort the products by price. Could someone help me plz?? That's would be awesome, sorry if the code is long

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

void input();
void menu();
void read();

struct product
{
  char code[20];
  char name[50];
  int quan;
  float pr;
};

void menu()
{
  int k;
  printf("___________MENU________\n");
  printf(
      "1. Enter the info of your products which is saved in      Products.txt\n");
  printf("2. Read the file Products.txt & sort by price.\n");
  printf("3. Exit");
  printf("________________________\n");
  printf("Enter your option: ");
  fflush(stdin);
  scanf("%d", &k);

  switch (k)
  {
  case 1:
    input();
    break;
  case 2:
    read();
    break;
  case 3:
    printf("\nTerminating");
    exit(0);
    break;
  default:
    printf("\nError! Wrong Number is Entered\nPlease Try Again\n");
    break;
  };
}

void input()
{
  struct product proinfo[50];
  FILE *fp;
  int n, i;

  printf("How many products need imported?\n");
  scanf("%d", &n);

  if ((fp = fopen("Products.txt", "wb")) == NULL )
  {
    printf("Error opening file!\n");
    exit(0);
  }

  for (i = 0; i < n; i++)
  {
    printf("Code of product # %d: ", i + 1);
    fflush(stdin);
    gets(proinfo[i].code);
    printf("Name: ");
    gets(proinfo[i].name);
    printf("Quantity: ");
    scanf("%d", &proinfo[i].quan);
    printf("Price: ");
    scanf("%.2f", &proinfo[i].pr);

  }

  if (fp != NULL )
  {
    for (i = 0; i < n; i++)
      fwrite(&proinfo[i], sizeof(struct product), 1, fp);
    fclose(fp);
  }

}

void read()
{
  struct product a[50];
  int len, t, r;

  FILE *fp;
  fp = fopen("Products.txt", "rb");

  if (fp != NULL )
  {
    fseek(fp, 0, SEEK_END);
    len = ftell(fp);
    t = len / sizeof(struct product);
    rewind(fp);
    fread(&a[0], sizeof(struct product), t, fp);

    for (r = 0; r < t; r++)
    {
      printf("%s \t %s \t %d \t %.2f\n", a[r].code, a[r].name, a[r].quan,
          a[r].pr);
    }
    fclose(fp);
  }
  else
    printf("No data!");
}

int main(void)
{
  int a;
  for (a = 0;; a++)
  {
    menu();
  }
}
Community
  • 1
  • 1
D.T
  • 437
  • 8
  • 20

1 Answers1

2

You have to use %f instead of %.2f when adding a product:

scanf("%f", &proinfo[i].pr);
DontVoteMeDown
  • 21,122
  • 10
  • 69
  • 105
Kuro13
  • 105
  • 1
  • 9
  • 2
    please explain the reason for that. – 0xF1 Dec 20 '13 at 09:54
  • The format for `scanf` has a maximum field width, but no precision as `printf` has. The format specifier doesn't allow a dot between the ´%` sign and the conversion specifier. The conversion spec `%.2f` is malformed and the compiler warns about it when warnings are on. – M Oehm Dec 20 '13 at 09:59
  • %.2f is used to set a display to be x.xx and can't be used to formating user input... – Kuro13 Dec 20 '13 at 10:00