Click here to Skip to main content
15,903,805 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
```
`#define _CRT_SECURE_NO_WARNINGS

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

#define MAX 5000

`struct Product { // declaring the structure
	char name[255];
	float price;
	int quantity;
};

void readdata(struct Product*);//prototyping the read function
void displaydata(struct Product*);//prototyping the display function

`int main()
{
	int biggest = 0;
	int i, n;
	struct Product dp[MAX];

	printf("Enter the number of products:");
	scanf("%d", &n);

	if (n<0 || n>MAX)
	{
		printf("Invalid numbers!");
	}```

	struct Product* p = (struct Product*)malloc(n * sizeof(struct Product)); // dinamically allocating memory for the structure

	for (i = 0;i < n;i++) // reading the data in main
	{
		printf("Product %d :\n", i + 1);
		readdata(&dp[i]);
		if (dp[i].quantity > biggest)
		{
			biggest = dp[i].quantity; // checking which product has the highest quantity
		}
	}

	printf("The product with the highest quantity is:\n");

	for (i = 0;i < n;i++) // displaying the data in main
	{
		if (dp[i].quantity = biggest)
		{
			displaydata(&dp[i]);//displaying the product with the highest quantity
		}
	}

	for (i = 0;i < n;i++) / freeing the memory
	{
		free(&dp[i]);
	}
      //no output at all
}`

void readdata(struct Product* p) // reading the data
{
	printf("\nName:");
	scanf("%s", p->name);
	printf("\nPrice:");
	scanf("%f", &p->price);
	printf("\nQuantity:");
	scanf("%d", &p->quantity);
}
void displaydata(struct Product* p)
{
	printf("\nName:%s", p->name);
	printf("\nPrice:%f", p->price);
	printf("\nQuantity:%d", p->quantity);
} // displaying the data```


What I have tried:

The problem I have is that this code displays nothing.It doesn't give any errors, and it returns nothing.Can someone explain where I've made mistakes?

Basically, what I have to do in this code is to display the product with the biggest quantity on stock.

I have a code that is simillar to this one, but it works without any problem, and I don't get why this one doesn't.

Also, did I correctly allocate memory for the structure? If not, I'd gladly accept some explanation on that as well.

Thanks in advance!
Posted
Updated 4-Jan-23 9:05am

Because it doesn't compile.
if (n<0 || n>MAX)
{
    printf("Invalid numbers!");
}```
"`" is not a valid character in C code, so the compiler doesn't like the final line of that code fragment.

If code doesn't compile, then no .EXE file is produced, so the chances are that the app you run doesn't include all that code - just the last successful compilation which didn't include the back-ticks.

Try removing them, and see if it compiles. If it does, see what happens when you run it this time. If it still doesn't do what you expect, use the debugger to find out exactly what it is doing while it runs.
 
Share this answer
 
Comments
hold my horse 4-Jan-23 8:29am    
Even without these characters, that I've probably introduced by accident when I was posting the question, it doesn't compile.I'll try using the debugger, thank you for the answer!.
Can you please also tell me whether i've properly allocated memory for the structure or not?
OriginalGriff 4-Jan-23 9:06am    
So fix the compilation problems first - you have other back-ticks in there, a missing "/" starting a comment, and so forth.
Fix all the compiler problems first, and see what happens - you can't use a debugger until you produce an EXE file for it to debug! :D

You malloc looks Ok to me, but you don't actually use the memory you allocated, and you try to free memory that you didn't allocate ... which is odd.
Change the definition of MAX, you are never going to type in the details for 5000 products. A more reasonable value would probably be 10. Also, you already allocate the space for your products at line 23:
C++
struct Product dp[MAX];

So there is no need for the call to malloc at line 33, or the call(s) to free at the end.
 
Share this answer
 
Try (look at the remarks in order to see the changes I made)
C
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

#define MAX 5000

struct Product { // declaring the structure
  char name[255];
  float price;
  int quantity;
};

void readdata(struct Product*);//prototyping the read function
void displaydata(struct Product*);//prototyping the display function

int main()
{
  int biggest = 0;
  int i, n;


  // you don't need this struct Product dp[MAX];

  printf("Enter the number of products:");
  scanf("%d", &n);

  if (n<0 || n>MAX)
  {
    printf("Invalid numbers!");
    return -1;
  }

  struct Product* dp = (struct Product*)malloc(n * sizeof(struct Product)); // dinamically allocating memory for the structure

  for (i = 0;i < n;i++) // reading the data in main
  {
    printf("Product %d :\n", i + 1);
    readdata(&dp[i]);
    if (dp[i].quantity > biggest)
    {
      biggest = dp[i].quantity; // checking which product has the highest quantity
    }
  }

  printf("The product with the highest quantity is:\n");

  for (i = 0;i < n;i++) // displaying the data in main
  {
    // not this way: if (dp[i].quantity = biggest)
    if (dp[i].quantity == biggest)
    {
      displaydata(&dp[i]);//displaying the product with the highest quantity
    }
  }

  free(dp);

  /* remove this
  for (i = 0;i < n;i++) / freeing the memory
  {
    free(&dp[i]);
  }
  */
      //no output at all
}

void readdata(struct Product* p) // reading the data
{
  printf("\nName:");
  scanf("%s", p->name);
  printf("\nPrice:");
  scanf("%f", &p->price);
  printf("\nQuantity:");
  scanf("%d", &p->quantity);
}
void displaydata(struct Product* p)
{
  printf("\nName:%s", p->name);
  printf("\nPrice:%f", p->price);
  printf("\nQuantity:%d", p->quantity);
} // displaying the data```
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900