C Programming issues

This is a discussion on C Programming issues within the C Programming forums, part of the General Programming Boards category; I am currently trying to test and debug so that I can implement some C code that another person wrote ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    8

    Question C Programming issues

    I am currently trying to test and debug so that I can implement some C code that another person wrote 3 years ago. The code is not very well written but I am trying . . . my problem for today is a while loop. here is syntax

    while(strlen(prodtag))
    {...
    }
    [the variable is populated with a string]
    Written like this the while loop never gets entered.
    I added an ==0) to say if it is true come on in, and now the loop is infinite. I have tried several ways to increment or decrement prodtag so that once I get the first entrance the code will move on. I get lvalue errors when I try to add a
    prodtag++ or prodtag-- [i'd prefer prodtag--]
    [I'm working on a unix platform].

    Thanks for any help you can provide.
    Have a great day!

    Vicky

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    I'm not sure what you mean. Does this help? -

    Code:
    #include <string.h>
    #include <stdio.h>
    
    
    int main()
    {
    	char name[]="zen";
    	while(strlen(name))
    	{
    		printf("%s\n",name);
    		name[strlen(name)-1]=0;
    	}
    
    	return 0;
    }
    if you added a ==0 to the while conditional the loop will never be entered if the string has a length.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    8

    Smile while loop using strlen

    strangely enough adding the ==0 actually allowed the code to go into the while loop. Without anything it never did go into the while loop. This while loop is the heart of my program the purpose of which is to insert product structure tags into a document based on specific parameters.

    I have been trying several ways to get the strlen to see that it can move on, but as yet it gets to this while loop, goes through all the steps and even out of the module and back but never leaves this [i believe because it is infinitely true because the string length remains constant therefore it is always true, unless I am not understanding while loops and strlen functions]

    am i making any sense?? I need to find a way to get the program to move to the next function without too much additional code because this while loop has more before and after it, making for long nested ifs and such. Thanks for the quick reply. hopefully this will clarify my question for you. Have a great day!! weather here is very nice, I wish I were outside 65 degrees and sunny . . . if i could just get this loop fixed . . .

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    You need to post more code - like how prodtag is declared and initialised.

    > I get lvalue errors when I try to add a
    Suggests that prodtag is an array, rather than a pointer.

    > while(strlen(prodtag))
    Without seeing how prodtag gets modified, the program will either never get into the loop, or if it does, it will never get out of it.

    And
    &nbsp; while(strlen(prodtag))
    is equivalent to
    &nbsp; while(strlen(prodtag) != 0 )
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    8

    Smile while(strlen) response / reply

    I will post more code here, just didn't want to bog down the message board. I will get my code on screen and cut and paste the declarations below.

    Thanks for the quick reply. here's more of the code:

    prodtag is a global variable declared in a seperate header file as follows:

    char prodtag [maxstringsize]; {maxstringsize=37,267}

    The program populates another variable and then copies that to prodtag here's the fgets to the first variable

    fgets(producttag, maxstringsize, producttagfileptr);

    Next place prodtag is touched is in an if statement:

    if (feof(producttagfileptr))
    {

    prodtag[0]='\0';
    return(0);
    }
    else strcpy(prodtag, producttag);

    then it hits the while loop I listed in original question. These are the only places in the program where prodtag is touched. The places where prodtag is used are just strpy using it to build other variables. but none of them change the contents of prodtag. Does this help you to help me?? Thanks very much. Have a great day!!!
    Vicky

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    > while(strlen(prodtag))
    The only time this would exit would be when you did
    &nbsp; prodtag[0]='\0';
    But since that happens outside the while loop, and is followed by a return statement, this is moot.

    > These are the only places in the program where prodtag is touched.
    In which case, strlen(prodtag) is a constant which always returns true (all strings returned by a successful call to fgets are non-zero length), so you might as well say while ( 1 )

    This being the case, there needs to be either a break or a return inside that while loop, otherwise it will loop forever

    > just didn't want to bog down the message board.
    Use the attach feature....
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    You really shouldn't use strlen inside a loop.
    I don't really see too many situations
    where you would ever use strlen inside a loop.

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    8

    Wink while(strlen) loop . . .

    Thanks for the information.

    I will check on putting some type of a break in the loop, I have a similar program that runs against a different type of document, I checked how this program was written and it is using a while loop set against the value of the first char in prodtag and then using an if strlen(tempfile) call within the while loop [which works for that program].

    This question was my first time with the cprogramming.com board. I think it's a a good way to get some tips, information and help when you need it.

    Have a great day.

    If anyone wants to suggest a simple way to modify the code to break I am open to suggestions.

    Thanks.

    Vicky B.

  9. #9
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    Code:
    #include <string.h>
    #include <stdio.h>
    
    int main(void)
    {
         char name[] = "zen";
         int n;
    
         n = strlen(name);         /* assume any length */
         while(n) {
                printf("%s\n", name);
                name[n - 1] = '\0';
                --n;
          }
    }

  10. #10
    Registered User
    Join Date
    Sep 2001
    Posts
    8

    Smile while(strlen)

    Good Afternoon,

    Thanks for the reply[ies] to my question about using strlen in a while loop. I added a break; statement at the very end and it seems to have done the trick, I am presently trying to compare my test data to the production output to confirm the tags were inserted correctly . . . I have been working on this for wayyy to long. Zen if I have any problems with the break statement I will try your suggestion. I had already tried the break when I saw your message on the board. I will keep this board up and running from now on. Sometimes just putting the data/question in print helps you figure something out on your own . . . The value of black and white print

    Have a great day!

    Vicky B.

  11. #11
    Registered User
    Join Date
    Sep 2001
    Posts
    8

    Unhappy ooops, while(strlen(prodtag)) not fully functional yet

    while(strlen(prodtag)==0)
    {
    if (strcmp(producttagtype, "IN")==0)
    {
    if(strcmp(producttagtaype, "ENEW")==0 || strcmp(producttagtype, "SMAG")==0 || strcmp(producttagtype, "TNEW")==0 || strcmp(producttagtype, "LTTB"))
    strcpy(temppf, "<PER");
    else
    sprintf(temppf, "<%s", in);
    while(!strstr(inputline, temppf))
    subprocessfile();
    }
    else if (strcmp(producttagtype, "RN"==0)
    veryfyfile
    ... {{{more else ifs here}}}
    break;
    }

    my problem is that the break is causing the code to move on to the next function which dumps the rest of the sgm file and then adds my tags, i need to add the tags and then go to the next function while still getting out of the while loop . . .

    Should I use a return; to get out of the loop and continue the program without losing the appropriate placement of the tags??

    thanks.

    vicky b.

  12. #12
    Registered User
    Join Date
    Sep 2001
    Posts
    8

    Unhappy While loop(strlen(prodtag)) problem... still not working right 9/24/01

    I have tried many different approaches and still have had no luck getting my while loop to run properly. It runs best with the break, but I think I am puttin the break in the wrong place, because it is not outputting my variable to insert in the right place. Can anyone see what I have done to get the output backwards??

    Thanks.

    Vicky B. I am trying as best I can to preserve the original code as it was written, which is why I have left in the while with a strlen in it, [against advice] my charge is to fix and implement and try not to make too many modifications to original code . . .

    have a great day!

  13. #13
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    I think you're on the right track (output mostly right), but you need to step back and work it through on paper.

    Small random changes just to see if it will fix it are very counter productive (unless you get lucky early).
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  14. #14
    Registered User
    Join Date
    Sep 2001
    Posts
    8

    Smile while(strlen) loop

    Thanks for the reply. I found that the global variable is empty when looking to start the while loop, I am trying to track down how that fits in the big picture. maybe a quick draw up of the process flow from the original calling module will help me pinpoint the problem.

    Thank you. Have a great day!

    Vicky B.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. issues
    By OrAnGeWorX in forum C Programming
    Replies: 35
    Last Post: 11-18-2008, 11:18 PM
  2. Solution to culling issues?
    By VirtualAce in forum Game Programming
    Replies: 4
    Last Post: 03-14-2006, 05:59 PM
  3. Multi-platform support issues.
    By OOPboredom in forum C Programming
    Replies: 2
    Last Post: 04-27-2004, 06:41 PM
  4. Memory Issues
    By Padawan in forum C Programming
    Replies: 12
    Last Post: 04-03-2004, 02:10 AM
  5. hexdump issues
    By daluu in forum C Programming
    Replies: 2
    Last Post: 03-04-2003, 08:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21