...

View Full Version : C name bubble sort



nero0102
11-22-2004, 05:05 AM
My main problem here is I have an error message that isnt helping me very much. I am trying to figure out the strings in C. Why cant they just work like every other flipping language ....

My error message is

illegal implicit conversion from char [9][200] to char*

However I would like to have the strings size allocated dynamically for the size of whatevers in the datafile.dat. However if I try to leave either column blank (with [] ) I recieve a linker error. Which are even more wonderfully vague.

Basically what I want to do is learn how to swap with an array of strings.


#include <stdio.h> /* standard library, printf etc... */
#include <errno.h> /* Library for perror() and errno */
#include <string.h>
#include <stdlib.h>

/* Macro statements */
#define DATAFILE "datafile.dat"
#define ANSWERFILE "answerfile.dat"
#define STRSIZE 200

/* Function Prototypes */
int read_file(); // Attach to Master Teams shared memory
int sort_age(); // Read Master Teams shared memory
int sort_name(); // Detach from Master Teams shared memory
int write_file();

/* Global Variable List */
char names[9][STRSIZE];
char temp_name[9][STRSIZE];
int ages[9];
int end_data; // marker for the end of the array

/*** Start of main ***/
main()
{
read_file();
write_file();
return 0; // Exit Cleanly
}
/*** End of main ***/


/*** Start of read_file ***/
int read_file()
{
FILE *datafile; // Pointer to open a file

if ((datafile = fopen(DATAFILE, "r")) == NULL) // If the file can be opened, open it, else report the error and close
{
printf( "Error: fopen() failed, errno = %i\n", errno );
perror( "fopen()" );
printf( "Cannot open file: %s\n", DATAFILE);
exit( -1 );
}
else
{
end_data = 0;
if (fscanf( datafile,"%s\t%d\n", &names[end_data], &ages[end_data])!= EOF) // Check for an empty file
{
end_data++;
while (fscanf( datafile,"%s\t%d\n", &names[end_data], &ages[end_data])!= EOF)
{
end_data++;
}
}
else // Report an error and close
{
printf( "Error: fscanf() failed, errno = %i\n", errno );
perror( "fscanf()" );
printf( "Cannot read the open file: %s\n", DATAFILE);
exit( -1 );
}
fclose(datafile); // We are done with the file so we close it
sort_age(); // Allow the data to be sorted by age and written
sort_name(); // Allow the data to be sorted by names and appended
}
return 0; // Return to main()
}
/*** End of read_file ***/


/*** Start of sort_age ***/
int sort_age()
{
FILE *answerfile; // Pointer to open a file

int i, j;
int temp_age;
// Sort raw_data by each persons age
for (i = 0; i<=(end_data -1); i++)
{
for (j = (end_data -1); j >i; j--)
{
if (ages[j-1] > ages[j])
{
temp_age = ages[j-1];
strcpy(temp_name, names[j-1]);
ages[j-1] = ages[j];
strcpy(names[j-1], names[j]);
ages[j] = temp_age;
strcpy(names[j], temp_name);
}
}
}
if ((answerfile = fopen(ANSWERFILE, "w"))==NULL) // If the file can be opened, open it, else report the error and close
{
printf( "Error: fopen() failed, errno = %i\n", errno );
perror( "fopen()" );
printf( "Cannot open file to write: %s\n", ANSWERFILE);
exit( -1 );
}
else
{
for (i=0; i < end_data; i++)
{
fprintf( answerfile,"%s\t%d\n", names[i], ages[i]);
}
fclose(answerfile); // We are done with the file so we close it
}
return 0; // Return to read_file()
}
/*** End of sort_age ***/


/*** Start of sort_name ***/
int sort_name()
{
FILE *answerfile; // Pointer to open a file

if ((answerfile = fopen(ANSWERFILE, "a"))==NULL) // If the file can be opened, open it, else report the error and close
{
printf( "Error: fopen() failed, errno = %i\n", errno );
perror( "fopen()" );
printf( "Cannot open file: %s\n", ANSWERFILE);
exit( -1 );
}
else
{
if (fprintf( answerfile,"%s\t%d\n", &ages, &names)!= EOF) // Check for an empty file
{
//while (fprintf( answerfile,"%s\t%d\n", &ages, &names)!= EOF)
//{

//}
}
else // Report an error and close
{
printf( "Error: fscanf() failed, errno = %i\n", errno );
perror( "fscanf()" );
printf( "Cannot read the open file: %s\n", ANSWERFILE);
exit( -1 );
}
fclose(answerfile); // We are done with the file so we close it
}
return 0; // Return to read_file()
}
/*** End of sort_name ***/


/*** Start of write_file ***/
int write_file()
{

return 0; // Return to main()
}
/*** End of write_file ***/

nero0102
11-22-2004, 05:08 AM
LOL

I feel deficient now. Right after i submitted I went back to work and got the program to compile right away by removing [9] in line 24 to wind up with a 1 dimensional array instead of a 2 dimensional array.

And all this time I was looking in the wrong place when all a long copy/paste screwed me :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum