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!