Thread: program not running + need explanation

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    73

    program not running + need explanation

    Hey this program is running but not giving any result. its about to rearrange few strings alphabetically -



    Code:
     //REORDERING A LIST OF STRINGS 
    
    
    
                       #include<stdio.h>
                       #include<stdlib.h>
                       #include<string.h>
    
                      
    
                       void reorder (int n, char x[][12]);
    
    				   void main()
    
    				   {
    
    					   int i,n=0;
    
    				   char x[10][12];
    
    				   printf(" Enter each string on a seperate line below\n\n");
    				   printf(" Type\'END\' when finished\n\n");
    
    				   do 
    				   
    				   {
    					   printf("string %d:", n+1);
    					   scanf("%s", x[n]);
    				   }
    
    				   while (strcmp( x[n++], "END"));
    
    				   n--;
    
    				   reorder(n,x);
    
    				   printf("\n\nReordered List of Strings:\n") ;
    
    				   for( i=0;i<n;++i)
    
    					   printf("\nstring %d: %s", i+1, x[i]) ;
    
    				   }
    
    
    
    				   void reorder ( int n, char x[][12])
    
    				   {
    				    char temp[12];
    					int i, item;
    
    					for( item=0; item= n-1; ++ item)
    
    						for(i=item+1; i<n; ++i)
    
    							if (strcmp(x[item] , x[i])>0)
    
    							{
    							 strcpy(temp, x[item]);
    							 strcpy(x[item], x[i]);
    							 strcpy(x[i], temp);
    
    							}
    
    							return;
    
    				   }

    and explanation of this part will be helpful-

    Code:
                      do 
    				   
    				   {
    					   printf("string %d:", n+1);
    					   scanf("%s", x[n]);
    				   }
    
    				   while (strcmp( x[n++], "END"));
    
    				   n--;

    at first n=0( and later it will increase), to me scanf("%s", x[n]); will take only a letter . How can it take entire string or I'm guessing wrong ?

    and as far i know , strcmp function accepts two strings as arguments and returns an integer. whats its doing here , specially with END ?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    73
    thanks to all for reading me this long

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    I think with 30 posts, your code formatting could be better.
    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.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Plus you must use int main and not void main. Begin by fixing these two problems.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    73
    I practicing the program from Schaum's outline book. However, if you don't want to be in my side , its your matter

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Let me rephrase that: if you want any help from this board, then fix those two outlined issues.
    If you don't want to fix it, well, then go find another board to post at.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    If you are so lazy to change your program indentation. There are tools like indent.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    73
    Even I don't know that's called indentation. However, i changed the program . and used int main -

    Code:
    //REORDERING A LIST OF STRINGS 
    
    
    
                       #include<stdio.h>
                       #include<stdlib.h>
                       #include<string.h>
    
                      
    
                       void reorder (int n, char x[][12]);
    
    				   int main()
    
    				   {
    
    					   int i,n=0;
    
    				   char x[10][12];
    
    				   printf(" Enter each string on a seperate line below\n\n");
    				   printf(" Type\'END\' when finished\n\n");
    
    				   do 
    				   
    				   {
    					   printf("string %d:", n+1);
    					   scanf("%s", x[n]);
    				   }
    
    				   while (strcmp( x[n++], "END"));
    
    				   n--;
    
    				   reorder(n,x);
    
    				   printf("\n\nReordered List of Strings:\n") ;
    
    				   for( i=0;i<n;++i)
    
    					   printf("\nstring %d: %s", i+1, x[i]) ;
    
    				   }
    
    
    
    				   void reorder ( int n, char x[][12])
    
    				   {
    				    char temp[12];
    					int i, item;
    
    					for( item=0; item= n-1; ++ item)
    
    						for(i=item+1; i<n; ++i)
    
    							if (strcmp(x[item] , x[i])>0)
    
    							{
    							 strcpy(temp, x[item]);
    							 strcpy(x[item], x[i]);
    							 strcpy(x[i], temp);
    
    							}
    
    							return;
    
    				   }

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Then how about some simple research? There's even an article on wikipedia.
    Here is a sample:
    Code:
    //REORDERING A LIST OF STRINGS 
    
    
    
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    void reorder (int n, char x[][12]);
    
    int main()
    {
    	int i,n=0;
    	char x[10][12];
    
    	printf(" Enter each string on a seperate line below\n\n");
    	printf(" Type\'END\' when finished\n\n");
    
    	do 
    	{
    		printf("string %d:", n+1);
    		scanf("%s", x[n]);
    	}
    	while (strcmp( x[n++], "END"));
    
    	n--;
    	reorder(n,x);
    	printf("\n\nReordered List of Strings:\n") ;
    	for( i=0;i<n;++i)
    		printf("\nstring %d: %s", i+1, x[i]) ;
    }
    
    void reorder ( int n, char x[][12])
    {
    	char temp[12];
    	int i, item;
    
    	for( item=0; item= n-1; ++ item)
    		for(i=item+1; i<n; ++i)
    			if (strcmp(x[item] , x[i])>0)
    			{
    				strcpy(temp, x[item]);
    				strcpy(x[item], x[i]);
    				strcpy(x[i], temp);
    			}
    			return;
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    73
    Elysia @ I read that from wikipedia . Its only a paragraph (In context of programming) and you just copied the program. I'm a beginner. I guess before to come here that experts will be more wide. Im almost clueless .

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Honestly, there are links on Wikipedia.
    Then there's google.
    We're mostly here to guide you; not to do all the work for you.
    Usually it's expected that you look stuff up yourself. Like, if someone says indentation, then you enter that word into google and find out what it means. If you DID that, but can't find any answers, then you would post back saying you TRIED to find it, but failed. Then people will be more open to you.

    As for the original question, x is a 2D-array. A matrix, if you will. You can see it as that it contains 10 strings of 12 characters each. n controls which of the strings to read into.
    Visualize them like a box inside a bigger box. You choose first which of the bigger boxes to choose. Inside that are several smaller boxes. Each small box has room for one character. So, as you see, by choosing a big box, we can store entire strings in it. That's why scanf and strcmp works.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program running long
    By smarta_982002 in forum Windows Programming
    Replies: 3
    Last Post: 03-27-2008, 05:24 AM
  2. Running my program in the background
    By shoobsie in forum Windows Programming
    Replies: 4
    Last Post: 10-09-2005, 02:38 AM
  3. Replies: 3
    Last Post: 09-05-2005, 08:57 AM
  4. Why is my program running away?
    By badkitty in forum C++ Programming
    Replies: 4
    Last Post: 09-19-2001, 04:27 PM
  5. Running program
    By muffin in forum C Programming
    Replies: 5
    Last Post: 08-30-2001, 10:57 AM