#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    2

    Start printing from somewhere in the middle of an array


    I am trying to build this program where a first and last name will be entered, and the last name will be printed first, then a comma, and then the initial letter of the first name. Here's the code till now:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int i = 0, j = 0;
    	char a[40], c;
    	
    	printf("Enter a first and last name: ");
    	
    	while (i < 40) {
    		c = getchar();
    		if (c == '\n')
    			break;
    		a[i] = c;
    		i++;
    	}
    	
    	printf("\nYou entered the name: ");
    		
    	for (i = 0; i < 40; i++) {
    		if (a[i] == ' ') { 
    			for (j = i + 1; j < 40; j++) {
    				while (a[j] != '\n') {
    					printf("%c", a[j]);
    				}
    			}
    		}
    	} 
    	
    	printf(", %c", a[0]);
    	
    	return 0;
    }
    The last two loops, I am sure something is wrong with that. What I am trying to do is, if a ' ' character is hit while scanning through the array, a new loop will scan the rest of the array and print them until it hits a newline character. But this doesn't work. If I enter, for example, "Arman Khandaker", it just keeps on printing K forever. What's exactly wrong, and how can it be fixed?
  2. #2
  3. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,117
    Rep Power
    1803
    There is plenty wrong with that loop, it should be apparent just from a code walk through, but you could (and probably should) step the code in your debugger so you can see exactly how it is executing. Using the debugger will mean that you never have to post this kind of question again, and can solve your own problems in minutes.

    When you enter the inner loop, you switch to a different control variable, so when the inner loop terminates, the outer loop continues. At least it would except you have an inner-inner while loop, where I think you intended an 'if'.

    You have unnecessarily overcomplicated the output in any case; this exercise does not suggest a nested loop at all; all you need is a loop to find the start of the last name, then another to output it:
    Code:
    i = 0 ;
    while( a[i] != ' ' && a[i] != '\n' ) 
    {
        i++ ;
    }
    
    while( a[i] != '\n' )
    {
        putchar( a[i] ) ;
        i++ ;
    }
    
    printf( ", %c", a[0] ) ;
    Of course you could just:
    Code:
    char* last_name = strchr( a, ' ' ) + 1 ;
    printf( "%c, %s", a[0], last_name ) ;
    but that probably misses the point of the exercise perhaps?
    Last edited by clifford; October 12th, 2013 at 03:07 AM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    2
    Originally Posted by clifford
    There is plenty wrong with that loop, it should be apparent just from a code walk through, but you could (and probably should) step the code in your debugger so you can see exactly how it is executing. Using the debugger will mean that you never have to post this kind of question again, and can solve your own problems in minutes.

    When you enter the inner loop, you switch to a different control variable, so when the inner loop terminates, the outer loop continues. At least it would except you have an inner-inner while loop, where I think you intended an 'if'.

    You have unnecessarily overcomplicated the output in any case; this exercise does not suggest a nested loop at all; all you need is a loop to find the start of the last name, then another to output it:
    Code:
    i = 0 ;
    while( a[i] != ' ' || a[i] != '\n' ) 
    {
        i++ ;
    }
    
    while( a[i] != '\n' )
    {
        putchar( a[i] ) ;
    }
    
    printf( ", %c", a[0] ) ;
    Of course you could just:
    Code:
    char* last_name = strchr( a, ' ' ) + 1 ;
    printf( "%c, %s", a[0], last_name ) ;
    but that probably misses the point of the exercise perhaps?
    Okay I understood your code. But the program simply crashes after entering the name. This time, I also tried using the debugger. The error comes up as a segmentation fault, and it highlights this line:
    Code:
    while (a[i] != ' ' || a[i] != '\n') {
    I can't see what's wrong with it..
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Location
    Saint-Petersburg, Russia
    Posts
    240
    Rep Power
    29
    You are looping while the character is not space or is not carriage return.

    It is always either not space or not carriage return. It could not be both at once.

    So your loop is endless.

    Could you understand now what is wrong?

    By the way, you can use debugger to check the value of expressions. For example try to inspect three expressions:
    Code:
    a[i] != ' '
    a[i] != '\n'
    a[i] != ' ' || a[i] != '\n'
    Inspecting values and conditions is quite important while debugging.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    2
    Originally Posted by rodiongork
    You are looping while the character is not space or is not carriage return.

    It is always either not space or not carriage return. It could not be both at once.

    So your loop is endless.

    Could you understand now what is wrong?

    By the way, you can use debugger to check the value of expressions. For example try to inspect three expressions:
    Code:
    a[i] != ' '
    a[i] != '\n'
    a[i] != ' ' || a[i] != '\n'
    Inspecting values and conditions is quite important while debugging.
    I am not sure whether I got you right. I think you mean that I should either use
    Code:
    a[i] != ' '
    or use
    Code:
    a[i] != '\n'
    . If I only use
    Code:
    a[i] != ' '
    I just get "You entered the name: " as output. I tried to inspect the three expressions, but all that comes up in all these cases is that it's a segmentation fault. I am really confused.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    2
    Just to let you know, I forgot to reset the value of i to 0 after the first loop. Now that I did, it just keeps printing blank characters infinitely. Here's the code:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int i = 0;
    	char a[40], c;
    	
    	printf("Enter a first and last name: ");
    	
    	while (i < 40) {
    		c = getchar();
    		if (c == '\n')
    			break;
    		a[i] = c;
    		i++;
    	}
    	
    	printf("\nYou entered the name: ");
    	
    	i = 0;
    	
    	while(a[i] != ' ') {
        	i++;
    	}
    
    	while(a[i] != '\n') {
        	putchar(a[i]);
    	}
    	
    	printf(", %c", a[0]);
    	
    	return 0;
    }
  12. #7
  13. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,117
    Rep Power
    1803
    Originally Posted by arman.khandaker
    Just to let you know, I forgot to reset the value of i to 0 after the first loop. Now that I did, it just keeps printing blank characters infinitely.
    My error - the risk to entering code without compiling an testing - that said you could do that, the errors are trivial and should be spotted in the debugger.

    I omitted the i++ in the second loop, and used || rather than && in the first loop. I have edited the original answer - take a look.

    You need to test for '\n' as well as ' ' in the first loop in case your user enters a single word with no spaces. To be honest though that is the minimal amount of error checking/defensive coding necessary. If you don't check for '\n' you will have to check for the the length limit instead; you might check for both.

    Comments on this post

    • arman.khandaker agrees
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    2
    Originally Posted by clifford
    My error - the risk to entering code without compiling an testing - that said you could do that, the errors are trivial and should be spotted in the debugger.

    I omitted the i++ in the second loop, and used || rather than && in the first loop. I have edited the original answer - take a look.

    You need to test for '\n' as well as ' ' in the first loop in case your user enters a single word with no spaces. To be honest though that is the minimal amount of error checking/defensive coding necessary. If you don't check for '\n' you will have to check for the the length limit instead; you might check for both.
    Thanks, and yes I get your point for using both '\n' and ' '. This time, the output is "Khandaker" and then some garbage data. As for debugging, after I enter the first and last name, the debugging execution simply stops. I don't know how to interpret these debugging processes :/
  16. #9
  17. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Location
    Saint-Petersburg, Russia
    Posts
    240
    Rep Power
    29
    I am not sure whether I got you right. I think you mean that I should either use
    No, I meant that you can freely use them both, but aggregate them with and operator instead of or.

    As for debugger, try to find tutorial specific to your IDE and learn the following things:
    - execution of the program step-by-step;
    - breakpoints and "run to cursor";
    - "inspecting" values or expressions and "watch" them.

    Perhaps, some video-tutorial, like this:

    http://www.youtube.com/watch?v=kHFpzxMFB3E

    Comments on this post

    • arman.khandaker agrees
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    2
    Originally Posted by rodiongork
    No, I meant that you can freely use them both, but aggregate them with and operator instead of or.

    As for debugger, try to find tutorial specific to your IDE and learn the following things:
    - execution of the program step-by-step;
    - breakpoints and "run to cursor";
    - "inspecting" values or expressions and "watch" them.

    Perhaps, some video-tutorial, like this:

    http://www.youtube.com/watch?v=kHFpzxMFB3E
    Wow just learned how to use breakpoints. Used it to solve a very complicated problem(at least to me). I'll use it whenever I need, and I guess I need to post on DevShed less often from now on! Thanks a lot sir! :)
  20. #11
  21. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,117
    Rep Power
    1803
    Originally Posted by arman.khandaker
    As for debugging, after I enter the first and last name, the debugging execution simply stops. I don't know how to interpret these debugging processes :/
    It stops because your program has finished and terminates. Set a breakpoint on the return statement.

IMN logo majestic logo threadwatch logo seochat tools logo