Thread: another for loop problem

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    18

    Question another for loop problem

    hi folks could someone guide me on this
    ive written a program that prints a character array backwards, if i state the character array it works fine
    insert
    Code:
    #include<stdio.h>
    #include<string.h>
    int main()
    {
    	char name[25]= "arther";
    	int letters;
    
    	letters = (int)strlen(name);
    
    	for(name; letters>=0; letters--)
    		{
    			printf("%c",name[letters]);
    		}
    	return 0;
    }
    However... if i try and get the character array from the user it compiles but does not work??
    include
    Code:
    #include<stdio.h>
    #include<string.h>
    int main()
    {
    	char name[25];
    	int letters;
    
    	printf("Please enter your name ");
    	scanf_s("%s",name);
    
    	letters = (int)strlen(name);
    
    	for(name; letters>=0; letters--)
    		{
    			printf("%c",name[letters]);
    		}
    	return 0;
    }
    any pointers would be very grateful!
    Last edited by Rasher; 10-14-2009 at 10:09 AM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You did not say what you meant by "does not work", but here's one issue:

    "MK27\n"
    "\n72KM"

    Also, you are the first person I have ever seen cast strlen() -- totally unnecessary.

    And why does your for loop look like this:
    Code:
    for(name; letters>=0; letters--)
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    18
    Ok my bad when i compile the second lot of code it asks for your name then the program terminates it does not then print it backwards.
    as for the (int)strlen(name) ive only done c for about a week and that was the way i was tought. Just the same as my for loop just the way we were shown as i understand the for(name; letters>=0; letters--) does not need to be there just the ;?

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    18

    Smile

    ok it works now i used gets() instead of scanf although im still not too sure why scanf wouldnt work? i think its because scanf() stops processing at the first space so it wouldnt read both names, where gets() can read more than one word?
    sorry i know my 1st example only showed one word but ive decided to use two. ie first name and last name
    Last edited by Rasher; 10-14-2009 at 10:57 AM.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Rasher View Post
    ok it works now i used gets() instead of scanf although im still not too sure why scanf wouldnt work? i think its because scanf() stops processing at the first space so it wouldnt read both names, where gets() can read more than one word?
    No, scanf("%s") will include spaces (and newlines ).

    The problem with your for loop is this:
    for (one;two;three);
    "one" is a statement executed before the loop iterates. Generally, it is used to set the value of a counter, eg. "i=0".

    "name" (a predefined variable) by itself is a meaningless statement in this context. What the loop should look like is:
    Code:
    for(letters = strlen(name)-1; letters>=0; letters--);
    The reason for the "-1" is that the last element of "name" will be '\0', which there is no point in printing that. Obviously, this replaces the previous strlen call.

    Also, use fgets() and not gets() -- gets is unsafe (since you were using the "safer" scanf_s before). I've never used scanf_s (just scanf) so I suspect your problem is that it wants an additional parameter to indicate the length of name (for safety) but I dunno -- check your docs!
    Last edited by MK27; 10-14-2009 at 11:29 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Oct 2009
    Posts
    18

    Thumbs up

    Thanks the for loops are actually starting to make sense now, i thought i would have to declare the variable letters before the loop

    I understand about the -1 being the terminating zero and now understand why there was no point in name; being in the loop.

    I tried to use fgets and gets_s but the compiler said too few arguments to call so i have stuck with gets().

    Thanks for the explanation.

    If i use scanf my program only prints the first word backwards, when i use scanf_s my program doesnt print anything, how can i make scanf print both words or is that something im likely to come across when i do strings and arrays more indepth?
    Last edited by Rasher; 10-15-2009 at 05:41 AM.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Despite what MK says, scanf with %s will stop at spaces.

  8. #8
    Registered User
    Join Date
    Oct 2009
    Posts
    18
    Ok thanks thats what i thought

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM