Thread: Can Anyone...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    385

    Can Anyone...

    The following program is supposed to sort eight strings in alphabetical order, but its not functioning properly. Can anyone please debug this

    Code:
    void main()
    {
    	char *p[8], *s;
    	int i,j,t;
    	clrscr();
    	printf("Enter the strings:\n");
    	for(i=0;i<8;i++)
    		gets(p[i]);
    	for(i=0;i<8;i++)
    	{
    	       for(j=i+1;j<8;j++)
    	       {
    			t=strcmp(p[i],p[j]);
    			if(t>0)
    			{
    				s=p[i];
    				p[i]=p[j];
    				p[j]=s;
    			}
    	       }
    	}
    	printf("\nSorted strings:\n");
    	for(i=0;i<8;i++)
    		puts(p[i]);
    	getch();
    }
    Last edited by juice; 09-25-2011 at 11:20 AM.

  2. #2
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Is there absolutely no one who can find the fault?
    The program is supposed to sort eight strings in alphabetical order, but it is printing one out of the eight strings, twice, and ommiting one of the other seven.....

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    I am curious as to why you have an array of 10 pointers, of which only 8 are used, and then you print using all 10 pointers in the end.

    Incidentally, you should not be using gets, but rather should use something safer like fgets. Also, remember to include the proper headers. You don't need getch. void main should be int main.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    sorry it was a typing mistake...

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Please help me. Its a spooky program. It works fine if we initialize the string p where it is declared, but does not work properly if we get the strings by gets() or scanf().

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    *facepalm* duh. You did not allocate any space for the strings.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    I don't need to. I have used an array of pointers to strings...

  8. #8
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by juice View Post
    I don't need to. I have used an array of pointers to strings...
    Yes, you do. An array of pointers to strings that you need to allocate space for with malloc. Or alternatively you could just create large buffers such as char mystring[8][100];
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Can anyone be under any impression that you're paying the slightest bit of attention to whatever anyone says?

    This is your FOURTH thread with a stupid "can anyone" title - consider this a warning.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Thanx laser, thanx andrew. You r right.
    And I wonder Wats wrong with "can anyone"...

  11. #11
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by juice View Post
    Thanx laser, thanx andrew. You r right.
    And I wonder Wats wrong with "can anyone"...
    You were already told why that is wrong. You really should learn How to ask questions.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    "Falling out of the stupid tree, and hitting every branch on the way down" springs to mind.

    > And I wonder Wats wrong with "can anyone"...
    Visit the doc 4 times in a row with the same "it hurts" tag line for every different ailment, and see how far it gets you.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    You should allocate memory for your strings.
    Where do you do this?

    Code:
    int main()
    {
    	char p[8][100];
    	int i,j,t;
    	printf("Enter the strings:\n");
    	for(i=0;i<8;i++)
    		{scanf("%99[^\n]",p[i]);while(getchar()!='\n');}
            qsort(p,8,100,strcmp);
    	printf("\nSorted strings:\n");
    	for(i=0;i<8;i++)
    		puts(p[i]);
            return 0;
    }

  14. #14
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Thanx billy. I got that...

Popular pages Recent additions subscribe to a feed