Hi People,
In my project, I've been trying to do sorting information in ascending or decsending order..
User Enters these strings:
Angelina
Barbara
Terrel
Jamel
and it orders strings as below ascending order:
Terrel
Jamel
Barbara
Angelina
_________________________
I coded and what's wrong is, while ordering in ascending it gives as a first output some ascii chars instead of entered data(ascii?-jamel-barbara-angeline)...And I couldnt solve where I did something wrong, but I guess while program doing bubble sorting, it calculates somethings wrong, here is the code
Code:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
struct hospital{
char doctor[30];} hospital;
int size=0;
int menu();
void doctor(struct hospital* x, struct hospital temp);
struct hospital temp;
int main(){
struct hospital* patient;
int i=0;
int choice;
printf("Please give the number of data records:\n");
scanf("%d",&size);
patient=malloc(size*sizeof(hospital));
printf("Please give %d patient records in the form:\n",size);
while(i!=size){
printf("Enter the doctor's name:\n");
scanf("%s",patient[i].doctor);
i++;
}
printf("The Patient Information entered is shown below:\n");
i=0;
choice=menu();
switch (choice){
if(choice==0){return 0;}
case 1:
doctor(patient, temp);
break;
}
return 0;
}
int menu(){
int choice2;
printf("\nPlease give a Sorting Code according to menu below to Exit Press \"0\":\n");
printf("1-Surname ascending\n");
scanf("%d",&choice2);
return choice2;
}
void doctor(struct hospital* x, struct hospital temp){ //here trying to do kind of bubble sort to sort in ascending order.
int i=0;
int j=0;
for(i=0 ; i<size+1 ; i++){
for(j=0 ; j<size ; j++){
if((strcmp(x[j].doctor,x[j+1].doctor) < 0)){
temp=*(x+j);
*(x+j) = *(x+j+1);
*(x+j+1) = temp;
}
}
}
i=0;
i=0;
while( i != size ){
printf("%10s\n", x[i].doctor);
i++;
}
}