0

I've been studying C for almost three months now and I neved had much trouble on the way. However, I have this task to create a program that will arrange an array of products (chosen by the user), either by their price or their available quantity.

I had to use a struct called Product to do so. Problem is, when I enter on any of the functions (arrangePrice or arrangeQuantity), the console prints the "What is the name of product?" printf command and IMMEDIATELY prints out the "What is the price of the product?" printf command. It simply seems to ignore the scanf function between those commands that would let the user write the name of the product on a string. Why is that happening???

Here is the entire code:

#include <stdio.h>
#include <string.h>

struct Product
{
    char name[80];
    double price;
    int quantity;
};

void arrangePrice(struct Product vet[], int n)
{
    int i, j, auxQuant;
    double aux, auxprice;
    char auxname[80];
    for (i = 0; i < n; i++)
    {
        printf("What is the name of the product?\n");
        scanf("%[^\n]", vet[i].name);
        printf("What is the price of the product?\n");
        scanf("%lf", &vet[i].price);
        printf("What is the available quantity of the product?\n");
        scanf("%d", &vet[i].quantity);
    }
    printf("\n\nArranging by price:\n\n");
    for (j = 0; j < n; j++)
    {
        aux = vet[j].price;
        for (i = j + 1; i < n; i++)
        {
            if (vet[i].price < aux)
            {
                auxprice = vet[i].price;
                strcpy(auxname, vet[i].name);
                auxQuant = vet[i].quantity;
                vet[i] = vet[j];
                strcpy(vet[j].name, auxname);
                vet[j].price = auxprice;
                vet[j].quantity = auxQuant;
            }
        }
    }
    for (i = 0; i < n; i++)
    {
        printf("%[^\n] -> %.2lf\n", vet[i].name, vet[i].price);
    }
}

void arrangeQuant(struct Product vet[], int n)
{
    int i, j, aux, auxQuant;
    double auxprice;
    char auxname[80];
    for (i = 0; i < n; i++)
    {
        printf("What is the name of the product?\n");
        scanf("%[^\n]", vet[i].name);
        printf("What is the price of the product?\n");
        scanf("%lf", &vet[i].price);
        printf("What is the available quantity of the product?\n");
        scanf("%d", &vet[i].quantity);
    }
    printf("\n\nArranging by available quantity:\n\n");
    for (j = 0; j < n; j++)
    {
        aux = vet[j].quantity;
        for (i = j + 1; i < n; i++)
        {
            if (vet[i].quantity < aux)
            {
                auxprice = vet[i].price;
                strcpy(auxname, vet[i].name);
                auxQuant = vet[i].quantity;
                vet[i] = vet[j];
                strcpy(vet[j].name, auxname);
                vet[j].price = auxprice;
                vet[j].quantity = auxQuant;
            }
        }
    }
    for (i = 0; i < n; i++)
    {
        printf("%[^\n] -> %d\n", vet[i].name, vet[i].quantity);
    }
}

int main()
{
    struct Product prod[51];
    int n;
    int choice;
    printf("How many products will be added? (Maximum of 50)\n");
    scanf("%d", &n);
    while (n < 1 || n > 50)
    {
        printf("Invalid value. Try again.\n");
        scanf("%d", &n);
    }
    printf("Do you want to arrange by price or by available quantity? (0 or 1)\n");
    scanf("%d", &choice);
    if (choice == 0) arrangePrice(prod, n);
    else if (choice == 1) arrangeQuant(prod, n);
    else printf("Invalid value.\n");
    return 0;
}

I must say that I actually still don't know if the code is right or not, since I couldn't type the name of the products. Thanks for any help!

  • 1
    It's blowing past the first scanf because there's no format specifier in the first argument. %[^\n] is not recognized. Are you trying to use a regular expression? – nicomp Oct 29 '15 at 00:11
  • Possible duplicate of [Simple C scanf does not work?](http://stackoverflow.com/questions/3744776/simple-c-scanf-does-not-work) – too honest for this site Oct 29 '15 at 00:14

2 Answers2

2

Your scanf calls are leaving newline characters in the input buffer, and those newlines are being read on subsequent calls.

You need to leave a space at the start of each scanf pattern to consume any newlines that may be left over.

Also, %[^\n] is not a valid format specifier for printf. Use %s instead.

Example:

    printf("What is the name of the product?\n");
    scanf(" %s", vet[i].name);    // use %s for strings
    printf("What is the price of the product?\n");
    scanf(" %lf", &vet[i].price);
    printf("What is the available quantity of the product?\n");
    scanf(" %d", &vet[i].quantity);

And:

printf("How many products will be added? (Maximum of 50)\n");
scanf(" %d", &n);
while (n < 1 || n > 50)
{
    printf("Invalid value. Try again.\n");
    scanf(" %d", &n);
}
printf("Do you want to arrange by price or by available quantity? (0 or 1)\n");
scanf(" %d", &choice);
dbush
  • 205,898
  • 23
  • 218
  • 273
0

struct product vet[] is a pointer. It doesn't actually point to an array unless you allocate such an array before you call arrangePrice. For example:

struct Product vet_array[ 2 ];

arrangePrice( vet_array, 2 );

Alternatively you could call malloc but just to get this started to work, allocate a local on the stack with a fixed number of elements.

Mike Crawford
  • 2,232
  • 2
  • 18
  • 28