I have problems with searching & sorting strings in a file...
I want to alphabetize the string so I did this

{for(x=0; x<100; x++)
	     {
 		for(c=x; c<15; c++)
	        fscanf(stdin, "%s", student[c]->surname);
	        if(strcmp(student[c]->surname,student[c+1]->surname)<0)
		  {strcpy(temp,student[c]->surname);
		   strcpy(student[c]->surname,student[c+1]->surname);
		   strcpy(student[c+1]->surname,temp);
		   strcpy(temp,student[c]->name);
		   strcpy(student[c]->name,student[c+1]->name);
		   strcpy(student[c+1]->name,temp);
		   strcpy(temp,student[c]->number);
		   strcpy(student[c]->number,student[c+1]->number);
		   strcpy(student[c+1]->number,temp);}
	      }
	 }

when i run the program, it just hangs. but i couldn't think of other ways on how to alphabetize it, other than comparing two strings then swapping. is there something wrong with my code above?

I also can't search properly...

printf("\nEnter first letter of surname to search\n");
		gets(search);
		 fscanf(r, "%s", student[c]->surname);
		 if(!strncmp(search, student[c]->surname,1))
		  {printf("\n%s, %s\t%s\n", student[c]->surname, student[c]->name, student[c]->number); break;}

nothing shows up when i enter a letter to search.. it just skips the above code and executes the next step (I didn't pasted it above, it's the printf("\nSearch again\n"); )

I'm really stumped on what to do. Any ideas?

Recommended Answers

All 5 Replies

I have problems with searching & sorting strings in a file...
I want to alphabetize the string so I did this

{for(x=0; x<100; x++)
	     {
 		for(c=x; c<15; c++)
	        fscanf(stdin, "%s", student[c]->surname);
	        if(strcmp(student[c]->surname,student[c+1]->surname)<0)
		  {strcpy(temp,student[c]->surname);
		   strcpy(student[c]->surname,student[c+1]->surname);
		   strcpy(student[c+1]->surname,temp);
		   strcpy(temp,student[c]->name);
		   strcpy(student[c]->name,student[c+1]->name);
		   strcpy(student[c+1]->name,temp);
		   strcpy(temp,student[c]->number);
		   strcpy(student[c]->number,student[c+1]->number);
		   strcpy(student[c+1]->number,temp);}
	      }
	 }

when i run the program, it just hangs. but i couldn't think of other ways on how to alphabetize it, other than comparing two strings then swapping. is there something wrong with my code above?

I also can't search properly...

printf("\nEnter first letter of surname to search\n");
		gets(search);
		 fscanf(r, "%s", student[c]->surname);
		 if(!strncmp(search, student[c]->surname,1))
		  {printf("\n%s, %s\t%s\n", student[c]->surname, student[c]->name, student[c]->number); break;}

nothing shows up when i enter a letter to search.. it just skips the above code and executes the next step (I didn't pasted it above, it's the printf("\nSearch again\n"); )

I'm really stumped on what to do. Any ideas?

You can't sort because you don't have a sorting algorithm - just some code you threw up that felt right at the time, imo. You need to use standard algorithms for sorting. Sorting algorithm's are typically "brittle" -- you change them a bit, and they "break" right away.

I'm just guessing that your gets(search), is just getting a newline still in the input buffer from a previous scanf(), perhaps. That's why it "skips" the code it seems.

put a getchar() ahead of the gets(), and see if that helps. (gets() itself is very unsafe due to buffer overruns. ASAP, learn and use fgets(), using stdin as the source, instead of a file, for your code.)

Go through all these tutorials first.

hi, can u plz send ur entire code so we can fix things up greatly,
thnx

commented: Cool use of colour and txt speek in ya msg! +15

Maybe you should have read some of the forum rules: we aren't going to "give you" the code. Then you aren't learning which kind of defeats the point, right? Jishnu gave you an excellent tutorial to read, I was just about to suggest a bubble sort myself. Once you understand the basic algorithm, you can apply it to entire strings. I would suggest using the strcmp() function which is part of the cstring.h header because it will tell you which string is "larger" alphabetically.

cstring.h (C++; should be cstring) -> string.h (C)

:)

ssharish

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.