I am having problems with my code it says that the value compare
is not being intitalized please help. I need to enter seven words total then sort them out.please help.
// it might work2.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
/* Inputs a list of strings from the keyboard, sorts them */
/* in ascending or descending order, and then displays them */
/* on the screen. */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MAXLINES 25
int get_lines(char *lines[]);
void sort(char *p[], int n, int sort_type);
void print_strings(char *p[], int n);
int alpha(char *p1, char *p2);
char *lines[MAXLINES];
int _tmain(int argc, _TCHAR* argv[])
{
int number_of_lines, sort_type;
int f=0;
/* Read in the lines from the keyboard. */
number_of_lines = get_lines(lines);
if ( number_of_lines < 0 ){
puts("Enter no to exit");
exit(-1);
}
puts("\nDo You Have Any more words\n" );
scanf_s("%d", &sort_type);
sort(lines, number_of_lines, sort_type);
print_strings(lines, number_of_lines);
return 0;
}
int get_lines(char *lines[])
{
int n = 0;
char buffer[80]; /* Temporary storage for each line. */
puts("Do You Have Any More Words:");
while (n < MAXLINES && gets(buffer) != 0 && buffer[0]!=0)
{
if ((lines[n] = (char *)malloc(strlen(buffer)+1)) == NULL)
return -1;
strcpy( lines[n++], buffer );
}
return n;
} /* End of get_lines() */
void sort(char *p[], int n, int sort_type)
{
int a, b;
char *x;
/* The pointer to function. */
char(*compare)(char *s1, char *s2);
/* Initialize the pointer to point to the proper comparison */
/* function depending on the argument sort_type. */
for (a = 1; a < n; a++)
{
for (b = 0; b < n-1; b++)
{
if (compare(p[b],p[b+1]) > 0)
{
x = p[b];
p[b] = p[b+1];
p[b+1] = x;
}
}
}
} /* end of sort() */
void print_strings(char *p[], int n)
{
int count;
for (count = 0; count < n; count++)
printf("%s\n ", p[count]);
}
int alpha(char *p1, char *p2)
/* Alphabetical comparison. */
{
return(strcmp(p2, p1));
}