Thread: While loop not looping

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    9

    While loop not looping

    The display line isnt looping and I cant figure out why...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    #include <io.h>
    #include <fcntl.h>
    
    
    int main()
    {
        // String Storage
        char mystring[100];
        size_t mystring_reply;
    
    
        // Format into words
        int character_scanner = 0;
        int begin_word = 0;
    
    
        FILE *outfile;        //  DHH declare a pointer to a file control block.
        FILE *infile;
    
    
        int file_size = 0;
        char *buffer;
    
    
        // Create, open and write to a file**********************
        outfile = fopen("Hello_World.txt", "w+");
    
    
        if (!outfile)
        {
            printf("File failed to open");
        }
    
    
        fprintf(outfile, "%s", "Hello World. This would happen to be the\n"
                                "string being tested for whitespace!\n");
        fseek(outfile, 0, SEEK_SET);
        // ***************************************************************************
    #if 0
        ( DHH long string, multiple adjacent strings are concatenated by the compiler
        into one long string.  This works as long as there are no % characters in it
        because it occupies the position of the format string in the fprintf function call)
        A safer implementation would be:
    
    
        fprintf(outfile, "%s", "Hello World. This would happen to be the string "
                "being tested for whitespace!\n");
    
    
                    -or-
    
    
        fprintf(outfile, "%s\n", "Hello World. This would happen to be the string "
                "being tested for whitespace!\n");
    #endif    
        /*
         *     DHH  This version of fgets makes it unnecessary to search for every use of mystring
         *    to make sure you don't declare it with multiple lengths.  I practice, the
         *    only constants I allow in my code are 1 and 0.  Anything else is declared
         *    as a #define or a variable.  It prevents a lot of unnecessary debuging later.
         */
    
    
        while (fgets(mystring, sizeof mystring, outfile))
        {
    
    
    #if 0
            OK, we got a line of input for parsing.
           initialize the character scanner to start of line.
    #endif
    
    
        character_scanner = 0;
    
    
        // loop processing the line.**********************
        while (mystring[character_scanner] != NULL)    //  DHH
            {
            begin_word = character_scanner;    
    
    
            if (!isspace(mystring[character_scanner]))
            {
            while (!isspace(mystring[++character_scanner]));
            
            mystring[character_scanner++] = '\0';
            }
            else
            {
            begin_word = ++character_scanner;
            }
            }
        }
        fclose(outfile);
        // ****************************************************
    
    
        // Loop the display line ******************************
        character_scanner = 0;
        do
        {
            printf("%c", mystring[character_scanner]);
            character_scanner++;
        } while (mystring[character_scanner] != NULL);
    
    
        // ****************************************************
    }

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    since its a 'do' loop, it will always print at least one character. but if the character is not printable, you might not see anything. i'm guessing you get one or more unprintable characters then it hits '0' in mystring[character_scanner]

    change your printf("%c" to printf("%02x" to get the hex output to see what the first character is or use a debugger

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    while (mystring[character_scanner] != NULL);
    I would do this instead
    Code:
    while (mystring[character_scanner] != '\0');
    NULL is not the same as ASCII nul!
    It can be different; it is sometimes the same when both are zero.
    But, NULL might not be zero.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by stahta01
    NULL is not the same as ASCII nul!
    It can be different; it is sometimes the same when both are zero.
    But, NULL might not be zero.
    NULL has to be equal to zero though, so the comparison is correct. However, the comparison is semantically wrong because the intent is to compare mystring[character_scanner] with a null character, not a null pointer constant.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jul 2012
    Posts
    9
    Although this is quite embarrassing, I thought id let you know I found the problem. The compilers auto-stop feature was placed before the end of the loop. took me two days to figure that out. there's one for the newbie book...

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by laserlight View Post
    NULL has to be equal to zero though, so the comparison is correct. However, the comparison is semantically wrong because the intent is to compare mystring[character_scanner] with a null character, not a null pointer constant.
    Please post a link to the C standard that says that.
    NOTE: I do NOT mean a like to the C++ standard.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    whats a compiler auto-stop feature?

  8. #8
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by stahta01 View Post
    Please post a link to the C standard that says that. NOTE: I do NOT mean a like to the C++ standard.
    A "null pointer" is not necessarily equal to zero, but the constant NULL is absolutely equal to zero (often cast to void*). That's the way the language works: assigning zero to a pointer tells the compiler to assign a proper null pointer value (whatever that is) to the pointer.

    I'm not going to look through the standard for you, but here's a C-FAQ link: Question 5.4
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by oogabooga View Post
    A "null pointer" is not necessarily equal to zero, but the constant NULL is absolutely equal to zero (often cast to void*). That's the way the language works: assigning zero to a pointer tells the compiler to assign a proper null pointer value (whatever that is) to the pointer.

    I'm not going to look through the standard for you, but here's a C-FAQ link: Question 5.4
    That is NOT what I learned; and, I do not believe it is true that the standard says that.
    Note: It is true for all the C Compilers I have used; but, I never ever read that the standard says it must be true.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  10. #10
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    [QUOTE=stahta01;1116410]That is NOT what I learned; and, I do not believe it is true that the standard says that.QUOTE]
    Why don't you look it up yourself (since you apparently have a copy) and post back?

    But if NULL is not zero, then why is it perfectly fine in C to assign 0 to a pointer instead of using NULL?
    Code:
    char *p = 0;  // no difference from p = NULL
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  11. #11
    Lurker
    Join Date
    Dec 2004
    Posts
    296
    Quote Originally Posted by stahta01 View Post
    That is NOT what I learned; and, I do not believe it is true that the standard says that.
    Note: It is true for all the C Compilers I have used; but, I never ever read that the standard says it must be true.

    Tim S.
    There have been compilers for machines where NULL != 0, if I remember correctly most, if not all, of those where before C was standardized.

    I have been in several discussions about this issue before, I have tried reading the standard and I am still not sure what is right and wrong regarding this. I have even spoken to a few self professed "language lawyers" regarding this and then didn't agree with each other, so I really hope that somebody in this thread can shed some light on this issue! :-)

  12. #12
    Lurker
    Join Date
    Dec 2004
    Posts
    296
    Quote Originally Posted by oogabooga View Post
    Why don't you look it up yourself (since you apparently have a copy) and post back?

    But if NULL is not zero, then why is it perfectly fine in C to assign 0 to a pointer instead of using NULL?
    Code:
    char *p = 0;  // no difference from p = NULL
    Because that is how your compiler implemented it, it doesn't mean that it is the only way to do it. It might be the only correct way to do it though, but I don't know the answer to that.
    Last edited by Jimmy; 07-20-2012 at 06:23 PM. Reason: Broken quote

  13. #13
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by Jimmy
    There have been compilers for machines where NULL != 0
    Name one (with a link if possible). Understand an important difference here, though. I'm not talking about the actual bit pattern that is assigned to a pointer to represent a "null" pointer (which can indeed be non-zero). I'm talking about the definition of NULL and the fact that assigning 0 to a pointer is interpreted by the compiler as assigning the proper "null" value, whatever that is.

    Quote Originally Posted by Jimmy View Post
    Because that is how your compiler implemented it, it doesn't mean that it is the only way to do it.
    It has nothing to do with a particular implementation. I was pointing out the FACT that assigning 0 to a pointer is identical to assigning NULL. Therefore it's hard to understand how NULL can be non-zero.

    But I'm definitely not a language lawyer (I can hardly read that thing), so perhaps there's some solution here where NULL can be non-zero.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  14. #14
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    I don't have a proper copy of the C standard to hand but from The C89 Draft.

    An integral constant expression with the value 0, or such an expression cast to type void * , is called a null pointer constant. If a null pointer constant is assigned to or compared for equality to a pointer, the constant is converted to a pointer of that type. Such a pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function.
    later on:
    A.6.3 Implementation-defined behavior

    Each implementation shall document its behavior in each of the areas listed in this section. The following are implementation-defined:
    ...
    * The null pointer constant to which the macro NULL expands (4.1.5).

    As I understand it NULL is just a preprocessor define to 0 -- so after preprocessing, these two assignments are exactly the same:
    Code:
    char *p1 = 0;  
    char *p2 = NULL;  // preprocessed to char *p2 = 0;
    I think the confusion comes from the fact that the machine representation of the null pointer isn't guaranteed to have value 0, though it does on all the architectures I know of.
    Code:
    char *p = 0;   // NULL pointer
    int a = (int)p; // a might not be 0
    The C faq names a couple of architectures with non-0 null pointer representations (Question 5.17) but NULL is still 0, as oogabooga concisely explained.

  15. #15
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    @smokeyangel: Your quote does NOT agree with your conclusion.

    A.6.3 Implementation-defined behavior
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Do/While loop not looping
    By pdenman in forum C++ Programming
    Replies: 6
    Last Post: 03-18-2011, 01:10 AM
  2. looping a variable within a loop.
    By vespine in forum C Programming
    Replies: 3
    Last Post: 06-27-2010, 10:30 PM
  3. do while loop keeps looping
    By wankel in forum C Programming
    Replies: 23
    Last Post: 06-15-2009, 04:53 PM
  4. 'For loop' looping one too many times.
    By thealmightyone in forum C Programming
    Replies: 7
    Last Post: 02-20-2009, 06:46 AM
  5. looping prob for loop
    By bazzano in forum C Programming
    Replies: 2
    Last Post: 03-18-2006, 11:29 AM