0

I am trying to implement sorting of array of structures using quick sort but its not even taking input correctly. The exact question is: We have to sort grocery items according to their names whose names and sales price is given to us, in increasing order.

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
struct comp
{
    int salep;
    char pname[50];

};
void swap(struct comp *x,struct comp *y)
{
   struct comp temp=*x;
   *x=*y;
   *y=temp;
}
int partition(struct comp item[50],int lb,int ub)
{
    char pivot[50];
    strcpy(pivot,item[lb].pname);
    int start=lb;
    int end=ub;
    while(start<end)
    {
        while(item[start].pname<=pivot)
            start++;
        while(item[end].pname>pivot)
            end--;
        if(start<end)
            swap(&item[start],&item[end]);
    }
}
quickSort(struct comp item[50],int lb,int ub)
{
    if(lb<ub)
    {
        int loc=partition(item,lb,ub);
        quickSort(item,lb,loc-1);
        quickSort(item,loc+1,ub);
    }
}
int main()
{
    struct comp item[50];
    int n;
    printf("Enter number of items:\n");
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        printf("Enter product %d name:\n",i);
        scanf("%[^\n]",item[i].pname);
        printf("Enter sales percentage:\n");
        scanf("%d",&item[i].salep);
    }
    quickSort(item,0,n);
    printf("Sorted List:\n");
    for(int i=0;i<n;i++)
    {
        puts(item[i].pname);
        printf(item[i].salep);
    }

    return 0;
}

I am getting the following result

Enter number of items:
2
Enter product 0 name:
Enter sales percentage:
oguz ismail
  • 1
  • 16
  • 47
  • 69
  • Please add a space in `scanf("%[^\n]", item[i].pname)` to make it `scanf(" %[^\n]", item[i].pname)`. The space is before `%` to instruct it to filter leading whitespace. The `scanf` conversion stops at the first character it cannot convert, which is typically (but not necessarily) a space or a newline, and that character remains in the input buffer. It will be read by the *next* `scanf()`. Format specifiers `%d` and `%s` and `%f` automatically filter such leading whitespace characters, but `%c` and `%[]` and `%n` do not. You can instruct `scanf` to do so by adding a space just before the `%`. – Weather Vane Jun 13 '21 at 08:08
  • Or you can also do a `scanf("%c",&buf)` after `scanf("%d",&item[i].salep);` and `scanf("%d",&n);`, that way the `\n` won't be there in the input buffer. Also you cannot just `printf(item[i].salep);` in C, you'll have to `printf("%d",item[i].salep);` – JASLP doesn't support the IES Jun 13 '21 at 08:18
  • @JustASimpleLonelyProgrammer that is a horrible kludge and removes only one character (if present). It will also indiscriminately remove a non-whitespace character which might be wanted. Using `scanf` in the way it is intended to be used, it will remove *any amount* of leading whitespace, such as when a user has carelessly tapped twice. – Weather Vane Jun 13 '21 at 08:32
  • Please don't change the question after it has been answered: ask another - this isn't a rolling interactive site. Please make the question more specific than "how do I ...?" – Weather Vane Jun 13 '21 at 08:35

0 Answers0