recently i have wrote a program. But unfortunately the program doesn't run well and i have no idea what should i do.....can any one so kind to help me? Thank!
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 7
typedef struct Date
{
unsigned day;
unsigned month;
unsigned year;
}DATE;
typedef struct Video
{
unsigned id;
char title[90];
char producer[60];
DATE releaseDate;
} VIDEO;
VIDEO v[N]={};
unsigned noOfVideo=0;
void purgeKBBuffer() /* delete characters left in the keyboard buffer */
{
int dummychar;
/* read and drop characters from the buffer
until the closing return ('\n') is reached */
do dummychar = getchar();
while (dummychar != '\n');
}
char* inputString(char x[], int SIZE)
{
int i=-1;
do
{
x[++i] =getchar();
}
while(x[i]!='\n'&&i<SIZE);
x[i]='\0';
return x;
}
DATE inputDate()
{
DATE d;
printf("Enter Day:");
scanf("%u", &d.day);
purgeKBBuffer();
printf("Enter Month:");
scanf("%u", &d.month);
printf("Enter Year:");
scanf("%u", &d.year);
putchar('\n');
return d;
}
VIDEO inputVideo()
{
VIDEO v;
printf("ID:");
scanf("%u",&v.id);
purgeKBBuffer();
printf("Title:");
inputString(v.title,90);
printf("Producer:");
inputString(v.producer,60);
printf("The released date:\n");
v.releaseDate=inputDate();
return v;
}
void addVideo(VIDEO v[N])
{
if(noOfVideo<N)
{
v[noOfVideo]=inputVideo();
noOfVideo++;
}
else
{
printf("The database can only store up to 7 videos and there is no space left!\n\n");
}
}
void printVideo(VIDEO v)
{
printf("\nID:%u\tTitle:%s\t\tProducer:%s\tRelease Date:%u.%u.%u", v.id, v.title, v.producer, v.releaseDate.day, v.releaseDate.month, v.releaseDate.year);
printf("\n");
}
void listVideos(VIDEO v[noOfVideo])
{
int i;
for(i=0;i<noOfVideo;i++)
{
printVideo(v[i]);
}
}
void sortById(VIDEO v[N])
{
VIDEO temp;
int lower,Min,i;
for (lower=0;lower<=N-2;lower++)
{
Min=lower;
for (i=lower+1;i<N;i++)
{
if (v[i].id<v[Min].id)
{
Min=i;
}
}
temp=v[lower];
v[lower]=v[Min];
v[Min]=temp;
}
}
void findAndPrintVideoByTitle(VIDEO v[N])
{
int i, j = 0;
char title[90];
printf("\tPlease enter title to search: ");
inputString(title,90);
putchar('\n');
for(i = 0; i <= N-1; i++)
{
if (strcmp (v[i].title, title) == 0)
{
j = 1;
printVideo(v[i]);
}
}
if (j == 0)
{
printf("\tVideo not found!\n\n");
}
}
char actionMenu()
/* show the menu and return a valid selection of the menu */
{
char z;
printf("\t\t\t**************************\n\n");
printf("\t\t\t* Video Database *\n\n");
printf("\t\t\t**************************\n\n");
printf ("\t(a) Add a video.\n");
printf ("\t(b) List all videos\n");
printf ("\t(c) Sort videos by ID\n");
printf ("\t(d) Search a video by title\n");
printf ("\t(e) Quit\n\n\t");
// get user input
do z = (char) getchar();
// continue while c not found in the string of menu chars
while (strchr("abcde", z) == 0);
printf("\n");
purgeKBBuffer();
return z;
}
int main()
{
VIDEO v[N];
char actionMenu();
char z;
unsigned Count = 0;
while (z != 'e')
{
z = actionMenu();
switch (z)
{
case 'a': addVideo(v); // TODO add a new video
break;
case 'b': listVideos(v); // TODO list all videos
break;
case 'c': sortById(v); // TODO sort video by id
break;
case 'd': findAndPrintVideoByTitle(v); // TODO find video by title
break;
case 'e': printf("\tBye!\n\n"); //end program
break;
default: printf("invalid input\n");break;
}
}
return 0;
}
in order to sort you need to enter "c" and to print out you need to type in "b" again. But that's the problem....