I created a 'sort by date' function that works correctly. However, when trying to create the 'sort by name' function, I assumed by that copying the 'sort by date' function and changing the variable yearTmp to nameTmp it would sort by name. It does not. Program runs but I am seeing these errors in the compiler...
[Warning] assignment makes integer from pointer without a cast
[Warning] assignment makes pointer from integer without a cast
Code I have so far...
#include <stdio.h>
#include <string.h>
#define MAX 30
void sortByDate( int year[], char *name[], char *states[], int count);
void sortByName(int year[], char *name[], char *states[], int count);
int main()
{
int year[MAX];
int i, a;
int count = 0;
int choice;
char *name[MAX],
*states[MAX];
char b[MAX], c[MAX];
FILE *inp = fopen("hurricanes.txt","r"); /* defining file input */
for(i=0;i<MAX;i++)
{
if( feof(inp) )
{
break;
}
fscanf(inp, "%d %s %29[^\n]", &a, b, c);
year[i]=a;
name[i] = strdup(b);
states[i] = strdup(c);
++count;
printf("%d %s %s\n", year[i], name[i], states[i]);
}
printf("Press 0 to sort by date or 1 to sort by name: ");
scanf("%d", &choice);
if (choice == 0)
{
sortByDate(year, name, states, count);
}
else if ( choice == 1)
{
sortByName(year, name, states, count);
}
getch();
return 0;
}
void sortByDate( int year[], char *name[], char *states[], int count )
{
int d = 0;
int c = 0;
int yearTmp;
int order[count];
int tmp = 0;
FILE *outp = fopen("report.txt","w"); /* defining file output */
for (c = 0; c < count; ++c)
{
order[c] = c;
}
for (c = 0 ; c < ( count - 1 ); c++)
{
for (d = 0 ; d < count - c - 1; d++)
{
if (year[d] > year[d+1])
{
yearTmp = year[d];
year[d] = year[d+1];
year[d+1] = yearTmp;
tmp = order[d];
order[d] = order[d+1];
order[d+1] = tmp;
}
}
}
for (c = 0; c < count; ++c)
{
printf("%d %-10s %s\n", year[c], name[order[c]], states[order[c]]);
}
}
void sortByName(int year[], char *name[], char *states[], int count)
{
int d = 0;
int c = 0;
char nameTmp;
int order[count];
int tmp = 0;
FILE *outp = fopen("report.txt","w"); /* defining file output */
for (c = 0; c < count; ++c)
{
order[c] = c;
}
for (c = 0 ; c < ( count - 1 ); c++)
{
for (d = 0 ; d < count - c - 1; d++)
{
if (name[d] > name[d+1])
{
nameTmp = name[d];
name[d] = name[d+1];
name[d+1] = nameTmp;
tmp = order[d];
order[d] = order[d+1];
order[d+1] = tmp;
}
}
}
for (c = 0; c < count; ++c)
{
printf("%d %-10s %s\n", year[order[c]], name[c], states[order[c]]);
}
}
The hurricanes.txt file....
1960 Donna FL, NC
1969 Camille MS
1972 Agnes FL
1983 Alicia TX
1989 Hugo SC,NC
2005 Katrina FL, LA, MS
2005 Rita TX, LA
2005 Wilma FL
2008 Ike TX
2009 Ida MS
2011 Irene NC, NJ, MA, VT
2012 Isaac LA
1992 Andrew FL, LA
1995 Opal FL, AL
1999 Floyd NC
2003 Isabel NC, VA
2004 Charley FL, SC, NC
2004 Frances FL
2004 Ivan AL
2004 Jeanne FL
nameTmp = name[d];: Type ofname[d]ischar*, but Type ofnameTmpischar. Alsoif (name[d] > name[d+1]), Usestrcmpinstead of.C. Read the manpage forstrcmp