Thread: while loop help

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    14

    while loop help

    hi everyone

    i am just struggling a little on my while loops

    i want the loop to save the character so it can build up a name e.g.

    Code:
    char *pos = strchr(buffer, 'R');
    int posa = pos-buffer;
    while (buffer != posa)
          {
    this is where i have got now i want it so while it is not equal to posa ('R') then for it to store what the content of buffer currently is

    so buffer[0] might be f
    buffer [1] might be i
    buffer [2] might be l
    buffer [3] might be l
    buffer [4] might be R

    so now it will store the whole word 'fill'

    all replies are appreciated thanks

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The word already is in buffer. Did you want it somewhere else? If so, strncpy will move things around.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    buffer is a char * and posa is an integer. You can't compare them directly, and your compiler should be screaming about this. Turn on all the warnings when you compile and make sure they're all resolved.

    What you actually have is buffer pointing to some place in memory, say address 1000. pos is pointing to some place in buffer, the first location of an 'R', say address 1005. posa is then the difference of these two, or 5. This is effectively the length from the start of buffer to the 'R', but you don't really need this. Just do
    Code:
    while (buffer != pos) {
        ...
        buffer++;
    }
    Note you also fail to handle the case that there is no 'R' in buffer. strchr will return NULL and your program will seg fault.

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    14
    ok so strncpy worked its wonders but now i need to add a null character on the end
    is there a way to do this without knowing the string length?
    i could have just done headname[5]='\0';
    but i do not completely know whether there are only 4, 5, 6, 45 characters.
    is there a way of pointing to the end of the string so i could add the null?

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    destination[posa] = '\0';

  6. #6
    Registered User
    Join Date
    Dec 2010
    Posts
    14
    thank you for that damn you lot on here make c look like ... eating cake

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Poll event loop
    By rogster001 in forum C++ Programming
    Replies: 2
    Last Post: 09-17-2009, 04:28 AM
  2. need help with a loop
    By Darkw1sh in forum C Programming
    Replies: 19
    Last Post: 09-13-2009, 09:46 PM
  3. funny-looking while loop
    By Aisthesis in forum C++ Programming
    Replies: 3
    Last Post: 08-30-2009, 11:54 PM
  4. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM