HELP!!...i need an option to display all the items i have entered in alphabetic order .

i need one more option which it can display all the items i have entered in alphabetic order in my program..
this is my coding......

#include<stdio.h>
#include<string.h>
#include<ctype.h>
#define capacity 1000


void cal_item(struct item[capacity]);
void input(struct item[capacity]);


struct item
{
int code;
float price;
char label[50];
char brand[50];
};


void main(void)
{
struct item my_items[capacity];
int code = 0, x = 1;
printf("Please make a selection\n");
printf("1 ~ To key in data\n");
printf("2 ~ To calculate\n");
printf("4 ~ To exit\n");
while (x == 1)
{

printf("Your selection:");
scanf("%d", &code);

switch (code)
{
case 1:
input(my_items);
break;

case 2:
cal_item(my_items);
break;

case 3:
printf("\n");
printf("Thank You!Please come again,have a nice day ahead.\n");
printf("exiting in[3]\n"
"exiting in[2]\n"
"exiting in[1]\n"
"exiting in[0]\n");
x = 0;
break;


}
}
}

void input(struct item my_items[capacity])
{
int code = 0, option = 1, search_code, quantity, a = 1, b = 1, c = 0, k ;
char user_selection;



while (option == 1)
{

printf("Enter code :");
scanf("%d", &my_items[c].code);
printf("\n");
printf("Enter unit price:");
scanf("%f", &my_items[c].price);
printf("\n");
printf("Enter label:");
scanf("%s", my_items[c].label);
printf("\n");
printf("Enter brand:");
scanf("%s", my_items[c].brand);

fflush(stdin);
printf("\n");
printf("More item?<Y/N>:");
user_selection = getchar();

switch (user_selection)
{
case 'y':
case 'Y':
c++;
option = 1;
break;

case 'n':
case 'N':
c++;
option = 0;
break;
}
}
}

void cal_item(struct item my_items[capacity])
{

int code = 0, option = 1, search_code, quantity, a = 1, b = 1, i;
float amount = 0 ,total = 0;
char user_selection;

while (a == 1)
{
printf("\n");
printf("Please enter the item code:");
scanf("%d", &search_code);
for (i = 0; i < capacity; i++)
{

if (my_items[i].code == search_code)
{
printf("\n");
printf("Enter quantity:");
scanf("%d", &quantity);
printf("\n");
amount = quantity * my_items[i].price;
printf("%d x %s<%s> = RM%.2f\n", quantity, my_items[i].brand, my_items[i].label, amount);
total = total + amount;
fflush(stdin);
printf("More item?<Y/N>:");
user_selection = getchar();

switch (user_selection)
{
case 'Y':
case 'y':
a = 1;
break;

case 'N':
case 'n':
printf("\n");
printf("\a");
printf("Total amount: RM %.2f \n", total);
a = 0;
}

}
}
}
}
Last edited on
closed account (j3Rz8vqX)
Add a case 4 that does what your current case 3 would do and implement a print function.
1
2
3
4
5
6
7
8
9
10
11
case 3:
   output(my_items);
case 4:
   printf("\n");
   printf("Thank You!Please come again,have a nice day ahead.\n");
   printf("exiting in[3]\n"
      "exiting in[2]\n"
      "exiting in[1]\n"
      "exiting in[0]\n");
   x = 0;
   break;


Your print out function should look similar to this:
1
2
3
4
5
6
7
8
void output(struct item my_items[capacity])
{
   int i = 0;
   for(i;i<capacity;i++)
   {
      printf("%d\n%f\n%s\n%s",my_items[i].code,my_items[i].price,my_items[i].label,my_items[i].brand);
   }
}


In order to print them in alphabetical order, you will have to add a sorting algorithm before printout. Possibly after they were input.

My suggestion would be use a quick sort algorithm or a bubble sort algorithm.

Topic archived. No new replies allowed.