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();
  }
}
8
  • Are you using MS-WIN? Commented Dec 20, 2013 at 9:35
  • 1
    I believe fflush(stdin); is undefined behavior Commented Dec 20, 2013 at 9:37
  • lots of problem in your code Commented Dec 20, 2013 at 9:48
  • 1
    Your question is two (unrelated) questions. So better split it. Commented Dec 20, 2013 at 10:06
  • Compile this with all compiler warnings switched on (-Wall -Wextra -pedantic for gcc). And then fix the code until no more warnings are issued. Commented Dec 20, 2013 at 10:17

1 Answer 1

2

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

scanf("%f", &proinfo[i].pr);
Sign up to request clarification or add additional context in comments.

3 Comments

please explain the reason for that.
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.
%.2f is used to set a display to be x.xx and can't be used to formating user input...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.