Reading a word from a line of text

Joined
Sep 23, 2004
Messages
15
Code:
inword = FALSE;
	while((c = getchar())!= EOF)
	{
		if(c == '\n')
		{
			line++;
		}
		if(!inword && !isDelimit(c))
		{
			inword = TRUE;
		}
		else if(inword && isDelimit(c))
		{
			inword = FALSE;
			
		}
		if(inword)
		{
			word[i] = c;
			i++;
		}

		
		
	}
I wrote this piece of code to read a word at a time from a line of text using spaces as delimiter. For example
"This is an apple". This codes suppose to read the word "This" to an array word. I have to do something with the array before move on to the next word on the line. But I traced this code and it copy the entire line without spaces. I print the array out and it looks like this Thisisanapple. Which is not the point. Can anyone help?
 
Try this... you need to process the word at line++; then you need to reset i to 0.... the reason it's putting them together with no spaces is that i just keeps being incremented, and so it just keeps adding it onto word.

Oh wait, you'll need to do more than reset i to 0... you need to clear the entire word variable. I'm not sure whether you're using a C++ string, or a char array. So if word was a string, you'd just have to do word.clear(); If it was a char array, I suppose you'd have to loop through the size of the array to clear it.

Code:
inword = FALSE;
	while((c = getchar())!= EOF)
	{
		if(c == '\n')
		{
			line++;
                        processWord(word);  //for example purposes, try cout << word << endl;
                        i = 0;
		}
		if(!inword && !isDelimit(c))
		{
			inword = TRUE;
		}
		else if(inword && isDelimit(c))
		{
			inword = FALSE;
			
		}
		if(inword)
		{
			word[i] = c;
			i++;
		}

		
		
	}
 
photonchaser said:
Excuse me for the confusion

No need to apologize. At least you seem to have avoided the staring at lines of code for >30 minutes and then realize it was a missing semicolon. (I was about to kill my compiler for not telling me ...) :p

I always have to force myself to sit down with a pencil and paper and figure out the logic of a program first. It avoids problems like this. But I hate doing it... I'd much rather just fire up computer and start coding...
 
Code:
inword = FALSE;
	while((c = getchar())!= EOF)
	{
		if(c == '\n')
		{
			line++;
		}
		else if(isDelimit(c))
		{
			processWord(word);
			clearString(word);
			i = 0;
		}

		if(!inword && !isDelimit(c))
		{
			inword = TRUE;
		}
		else if(inword && isDelimit(c))
		{
			inword = FALSE;
			
		}
		if(inword)
		{
			word[i] = c;
			i++;
		}

		word[i] = 0;

		
		
	}
I like to process the current word stores in the array right after the character is a space instead of waiting till the newline character. So is this sound ok? BTW, I'm using redirection to input my paragraph into the program.
 
I'm pretty much done with codes below. It reads words from a paragraph, one word at a time untill EOF is reached. It copy the word into an array and the line number that the word currently on. The array and the line number are member of a WORD structure. However, the line number is read incorrectly. See a sample run below

The cat in a hat
^Z
Word: a, Line: 1
Word: cat, Line: 1
Word: hat, Line: 2
Word: in, Line: 1
Word: the, Line: 1
Press any key to continue

The word 'hat' is on the first line
So if anyone see where I made mistake(s), please let me know
Here is the actual code to read the word and its line number
Code:
int readWord(WORD *string[])
{
	
	char wordbuffer[WRDLEN];
	int c, i, wordCount,inword, line;
	
	wordCount = 0;
	i = 0;
	line = 1;
	inword = FALSE;
	
	while((c = getchar())!= EOF)
	{
		if(!inword && !isDelimit(c))
		{
			inword = TRUE;
		}
		else if(inword && isDelimit(c))
		{
			if(c == '\n')
			{
				line++;
			}
			inword = FALSE;
			if(!isDuplicate(wordbuffer, string, wordCount))
			{
				copyWord(wordbuffer, string, line, wordCount);
				wordCount++;
			}
			clearWord(wordbuffer);
			i = 0;
			
		}
		if(inword)
		{
			if(isUpper(c))
			{
				c = tolower(c);
			}
			wordbuffer[i] = c;
			i++;
		}


		
		
	}

	return wordCount;

Thank You Very Much
 
put if(c == '\n') line++; after if(!isDuplicate(wordbuffer, string, wordCount))
 
starhawk said:
first thing that popped into my head: you would LOVE qbasic.

Hahahah. I did.

photonchaser, not that it matters, but if you want a little cleaner code, you could take advantage of a few of C++'s <string> functions. Example ...

Code:
string s;
char c = 'a';
s.clear()  //clears the string
s.push_back(c);  //pushes the character onto the string.

You could use the .push_back(char) function to read in the text, rather than creating a char array and then copying that into a string... but like I said, it doesn't really seem to matter for your program.
 
I would love to take advantage of many useful functions in C++. But my instructor only allowed C standard functions.

Cheer
 
Back
Top