breakin a for loop

Discussion in 'C' started by jefcoatv, Nov 17, 2010.

  1. jefcoatv

    jefcoatv New Member

    Joined:
    Oct 1, 2010
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    I am allocating memory for a sting of text and then picking out a word at a time. I am getting stuck on the for loop.

    I am trying to break a for loop and am having a hard time. I am new to programming and the c language. I need some advice on this one. I have attached the full code.

    I have tried this break and the program shuts down.
    if((pList->Word)='\0')
    break;



    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<ctype.h>
    
    #define MAXWORDS 1000
    
    typedef struct
    {
    	char Word[88];
    }UniqueWords;
    
    char *GetWord(char *Source, char *Dest);
    
    
    
    int main(int c, char **v)
    
    {
    	char *LocationGetty = "C:\\Users\\svjefcoat\\Documents\\Visual Studio 2010\\Projects\\assignment 10\\Getty.txt";
    	UniqueWords Word[MAXWORDS];
    	UniqueWords *pList;
    
    	char line[99];
    	char line2[99];
    	char Dest[39];
    	char Dest2[39];
    	char *Read;
    	FILE *pStorage;
    	char *pDArray;
    	int size;
    
    	pStorage = fopen(LocationGetty, "r"); //Location Getty
    	if(!pStorage)
    		return 0;
    
    	size = (sizeof(pStorage));
    
    	while(1)
    	{
    		if(feof(pStorage))
    			break;
    		*line=NULL;
    
    
    		fgets(line,sizeof(line), pStorage);
    
    		pDArray = malloc(sizeof(line));
    
    		strcpy(pDArray, line); 
    
    		pDArray[(sizeof(line))+1] = '\0';
    
    
    
    		printf("%-s", line);
    
    		Read = pDArray;
    
    		pList = Word;
    
    		for(;;)
    		{
    			Read = GetWord(Read, pList->Word);
    
    			pList++;
    		}
    
    		free(pDArray);
    
    
    		getchar();
    
    		return 0;
    
    	}
    
    	//pList++;
    
    
    
    	fclose(pStorage);
    
    
    	getchar();
    
    	return 0;
    }
    
    char *GetWord(char *Source, char *Dest)
    {
    	// skip all leading space characters
    	while(*Source)
    	{
    		if(isspace(*Source))
    		{
    			Source++;
    			continue;
    		}
    		else
    			break;
    	}
    
    	while(*Source)
    	{
    		if(isspace(*Source)) 
    		{
    			*Dest = '\0';
    			return Source;
    		}
    		*Dest++ = *Source++;
    	}
    	*Dest = '\0';
    	return Source;
    }
    
    
     
  2. go4expert

    go4expert Moderator

    Joined:
    Aug 3, 2004
    Messages:
    306
    Likes Received:
    9
    Trophy Points:
    0
    If you are trying to break this for loop

    Code:
    for(;;)
    {
      Read = GetWord(Read, pList->Word);
      pList++;
    }
    
    then the program should exit because there is nothing much to be executed after this. Unconditional return 0 inside a while loop means the loop will be executed only once.
     
  3. techgeek.in

    techgeek.in New Member

    Joined:
    Dec 20, 2009
    Messages:
    572
    Likes Received:
    19
    Trophy Points:
    0
    Occupation:
    EOC (exploitation of computers)..i m a Terminator.
    Location:
    Not an alien!! for sure
    Home Page:
    http://www.techgeek.in
    if you need another way to break the look then use some condition corresponding to a “break” and “exit(1)”…
     
  4. raji.vunnam

    raji.vunnam New Member

    Joined:
    Jan 6, 2011
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    I have tried this break and the program shuts down.
    if((pList->Word)='\0')
    break;


    here is nothing a small corection

    for assigning 5 to 'a' then we single '=' i.e like " a=5 "

    if we want campare 'a' with 5 then we use '==' i.e like

    if(a==5)

    pf("pass");

    in above if((pList->word)=='\0')
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice