Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

error with my bubble sort program dont know where im going wrong

mrGatesmrGates irelandPosts: 1Member
edited November 2013 in Beginner C/C++

hi , i am suppost to read in a user defined text file then, sort it using bubble sort then print the results into a newfile called sorted.txt created within the program, but when i compile i get error with strcpy, and sayas its expects char** but argument is of type char here is my code

include <stdio.h>

include <string.h>

include <stdlib.h>

define SIZE 100 /* max size of array */

define MAX_FILENAME 20 /* max length of filename */

void Bubble_sort(char name [], int n);

typedef enum {false,true} bool;

{
int main() {

FILE *pFile; FILE *fptr; int i; char FileName[MAX_FILENAME+1]; char name[SIZE]; int last;

printf("Enter the name of the student records to be sorted\n"); scanf("%s",&FileName);

pFile == fopen(FileName,"r"); /* read from the file */ if(pFile!=NULL) { printf("File Open Successful\n");

for(i = 0 ; !feof(pFile ) ;i++ ) { /* Read the number of elements in the input file / fscanf(pFile,"%s",name[i]); last = i-1;
if (i > SIZE){/
exit(EXIT_FAILURE); */ exit(0); } } } fclose (pFile);

    fptr == fopen("C:\\sorted.txt","w");  
    if(fptr==NULL){
    printf("Error!");
    exit(1);            
    }

BUBBLESORT ALGORITHM

void Bubble_sort(char name [SIZE], int i);

{ int i,j; char temp; bool done = false ; while (!done)

  {   done = true;
          for (i = last; i > 0; i--)   
          for (j = 0; j <= i; j++)   
      if (strcmp(name[j],name[j-1]) < 0)  
    {   strcpy(temp,name[j]);                   
          strcpy(name[j],name[j - 1]);          
          strcpy(name[j - 1], temp);              
         done = false;                     
}
  }

for (i = 0 ; i <= last ; i++)
fprintf(fptr,"%s",name[i]); }

fclose(fptr);
return (0);
}

}

Comments

  • pseudocoderpseudocoder Posts: 673Member ✭✭

    The str cpy/cmp error is caused by name being an array of characters. You're trying to compare single characters, but the functions want strings.

    If you're supposed to read many names and store them in an array in order to sort them, then you need another dimension in the declaration of name and in the function prototype.

    char name[MAX_NAMES][MAX_LENGTH] = { { 0 } };
    

    If you are just sorting an array of characters, then don't use str cmp/cpy

  • mrGatesmrGates irelandPosts: 1Member

    yeah im trying to sort an array of names thanks for your help

Sign In or Register to comment.