The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
[C, Code::Blocks, W7] having problem stopping a loop
Discuss [C, Code::Blocks, W7] having problem stopping a loop in the C Programming forum on Dev Shed. [C, Code::Blocks, W7] having problem stopping a loop C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 15th, 2012, 02:21 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 3
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;
}
|

November 15th, 2012, 02:32 AM
|
|
Contributing User
|
|
Join Date: Oct 2012
Posts: 30
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.
|

November 15th, 2012, 02:40 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 3
Time spent in forums: 48 m 39 sec
Reputation Power: 0
|
|
|
it's on pastebin, the link is just below with (), remove them
|

November 15th, 2012, 05:19 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 11
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
|

November 15th, 2012, 05:20 AM
|
 |
Contributed User
|
|
|
|
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;
}
|

November 15th, 2012, 08:01 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 3
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! 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|