C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 15th, 2012, 02:21 AM
Natanhel Natanhel is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 3 Natanhel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 48 m 39 sec
Reputation 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;
}

Reply With Quote
  #2  
Old November 15th, 2012, 02:32 AM
shilpac shilpac is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 30 shilpac User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 3 m 34 sec
Reputation Power: 1
Quote:
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.

Reply With Quote
  #3  
Old November 15th, 2012, 02:40 AM
Natanhel Natanhel is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 3 Natanhel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 48 m 39 sec
Reputation Power: 0
it's on pastebin, the link is just below with (), remove them

Reply With Quote
  #4  
Old November 15th, 2012, 05:19 AM
tomynolan tomynolan is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 11 tomynolan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 38 m 12 sec
Reputation 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

Reply With Quote
  #5  
Old November 15th, 2012, 05:20 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,838 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 17 h 9 m 51 sec
Reputation Power: 1774
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

Reply With Quote
  #6  
Old November 15th, 2012, 08:01 AM
Natanhel Natanhel is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 3 Natanhel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 48 m 39 sec
Reputation Power: 0
Quote:
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!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > [C, Code::Blocks, W7] having problem stopping a loop

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap