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

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    [C, Code::Blocks, W7] having problem stopping a loop


    Hey, I'm having a problem getting this while loop to end.
    the assignment needs to stop when it get EOF while the loop works until it get EOF it should stop when it reaches EOF.

    Well, I tried to stop it with break if it gets to EOF with no success and even return it.
    doesn't work :\

    I'd appreciate if you guys could tell me what am I doing wrong:
    Code:
    /*-------------------------------------------------------------------------
      Include files:
    --------------------------------------------------------------------------*/
    
    #include <stdio.h>
    
    
    
    /*=========================================================================
      Constants and definitions:
    ==========================================================================*/
    
    /* put your #defines and typedefs here*/
    
    /*-------------------------------------------------------------------------
      The main program. (describe what your program does here)
     -------------------------------------------------------------------------*/
    int main()
    {
        int c, x=0;
        c=getchar();
    
      while (c != EOF)
            {
                if (c==' ')/*identify special number*/
                {
                    putchar(' ');
                    c=getchar();
                    if (c=='5')
                    {
                        putchar(c);
                        c=getchar();
                        if (c==' ')
                        {
                            x=x+1;
                        }
                    }
                }
                if (x%2==0)
                {
                while (c!=EOF)
            {
            if (c==' ')
                {
                    putchar(c);
                    c=getchar();
                    if (c<='z'&&c>='a')
                    {
                        putchar((c-'a')+'A');
                    }
                    else if (c<='Z'&&c>='A')
                    {
                        putchar(c);
                    }
    /*this part turns the 1st letter after a ' ' char into a major one
    /////////////////////// no matter if it's a little one or a major one.*/
    
                    while (c!=' ')/*this part reads a word and turn all letters to small ones*/
                    {
                        c=getchar();
                        if (c<='Z'&&c>='A')
                        {
                            putchar((c-'A')+'a');
                        }
                        else if (c<='z'&&c>='a')
                        {
                            putchar(c);
                        }
                    }
                }
                else /*turns the 1st word to a capital letter word*/
                {
                    if (c<='z'&&c>='a')
                    {
                        putchar((c-'a')+'A');
                    }
                    else if (c<='Z'&&c>='A')
                    {
                        putchar(c);
                    }
                    while (c!=' ')/*this part reads a word and turn all letters to small ones*/
                    {
                        c=getchar();
                        if (c<='Z'&&c>='A')
                        {
                            putchar((c-'A')+'a');
                        }
                        else if (c<='z'&&c>='a')
                        {
                            putchar(c);
                        }
                    }
    
                }
    
                    break;
            }
                }
                else
                {
                    putchar(c);
                    c=getchar();
                }
    
            }
        return 0;
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    30
    Rep Power
    2
    Originally Posted by Natanhel
    Hey, I'm having a problem getting this while loop to end.
    the assignment needs to stop when it get EOF while the loop works until it get EOF it should stop when it reaches EOF.

    Well, I tried to stop it with break if it gets to EOF with no success and even return it.
    doesn't work :\

    I'd appreciate if you guys could tell me what am I doing wrong:
    pastebin(.)com/iaytzFUS
    Paste your code.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0
    it's on pastebin, the link is just below with (), remove them
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    11
    Rep Power
    0

    at a glance


    I think your variable 'c' isnt being changed...

    instead of
    Code:
    c = getChar() ;
    while (c != EOF) {
    //all your code
    c = getChar();
    }
    try this

    Code:
    while ((c=getChar) != EOF) {
    //all your code
    }
    This will automaticly update 'c' every time it check's the argument because the '=' operator retruns the value that is assigns

    hope this helps could be wrong I only glanced at it
  8. #5
  9. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,396
    Rep Power
    1871
    Well the first problem is your main is nearly 100 lines long (certainly more than can be fitted on screen at once). The readability of code goes downhill rapidly if you can't see a whole function at once.

    The other problem is you're calling getchar() in too many places. Each once for example needs to have an EOF check associated with it, and this is not done in many cases.

    Consider organising the code along the lines of a Finite State Machine
    Notice that there is only one point where a character is read, and that is checked for EOF.
    Also notice that each part of the code depends only on 'state' and 'ch'. At the moment, state is a single enum, but it could be expanded to be a struct containing say your 'x' variable, and maybe even a char array for the current 'word'.

    Code:
    #include <stdio.h>
    #include <ctype.h>
    enum states {
        OUT_WORD,
        IN_WORD
    };
    int main(void) {
        int ch;
        enum states state = OUT_WORD;
        while ( (ch=getchar()) != EOF ) {
            if ( state == OUT_WORD && !isspace(ch) ) {
                state = IN_WORD;
            } else
            if ( state == IN_WORD && isspace(ch) ) {
                printf("\n");
                state = OUT_WORD;
            }
    
            if ( state == IN_WORD ) {
                putchar(ch);
            }
        }
        return 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
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0
    Originally Posted by salem
    Well the first problem is your main is nearly 100 lines long (certainly more than can be fitted on screen at once). The readability of code goes downhill rapidly if you can't see a whole function at once.

    The other problem is you're calling getchar() in too many places. Each once for example needs to have an EOF check associated with it, and this is not done in many cases.

    Consider organising the code along the lines of a Finite State Machine
    Notice that there is only one point where a character is read, and that is checked for EOF.
    Also notice that each part of the code depends only on 'state' and 'ch'. At the moment, state is a single enum, but it could be expanded to be a struct containing say your 'x' variable, and maybe even a char array for the current 'word'.

    Code:
    #include <stdio.h>
    #include <ctype.h>
    enum states {
        OUT_WORD,
        IN_WORD
    };
    int main(void) {
        int ch;
        enum states state = OUT_WORD;
        while ( (ch=getchar()) != EOF ) {
            if ( state == OUT_WORD && !isspace(ch) ) {
                state = IN_WORD;
            } else
            if ( state == IN_WORD && isspace(ch) ) {
                printf("\n");
                state = OUT_WORD;
            }
    
            if ( state == IN_WORD ) {
                putchar(ch);
            }
        }
        return 0;        
    }
    thanks for the explanation but i can't use arrays and the ctype.h library for this code..

    tomynolan, it might be it, that's what a colleague of mine suggested because c is a char and EOF is not within the ASCII codes, so it doesn't recognize it and it get stuck on the loop.
    the thing is, it messes up all the code.. might have to rewrite it with the function (c=getchar()) in the while condition.

    thanks for the help! :)

IMN logo majestic logo threadwatch logo seochat tools logo