Thread: Weird special character text after output

  1. #1
    System-7
    Join Date
    Nov 2005
    Posts
    65

    loop problems

    Scroll to my latest post



    ---------------------------------
    Alright so I made this program for class that will separate a string into words. The program performs correctly except for after the output another line is displayed with lots of odd characters and such. The only reason I can think of for why this is happening is that it is reading outside of the character array but I do not see how it could do this.

    Code:
    #include <iostream>
    using namespace std;
    
    #define LINELENGTH 100
    
    void spaceWords(char phrase[]);
    void tWords(char phrase[]);
    void rWords(char phrase[]);
    void nWords(char phrase[]);
    
    int main(void)
    {
    	while (!cin.eof())
    	{
    		
    		char phrase[LINELENGTH] = {' '};
    		
    		cout << "Please enter a line of text: ";
    		cin.getline (phrase, LINELENGTH);
    
    		spaceWords(phrase);
    	}
    	return 0;
    }
    void spaceWords(char phrase[])
    {
    	int count = 0;
    	int total = 0;
    	int base = 0;
    	int count2 = 1;
    
    	//loop until count is equal to line length
    	while (count < LINELENGTH)
    	{
    		//while not a blank space, add to the counter
    		while (phrase[count] != ' ')
    		{
    			count ++;
    		}
    		//print out word number	
    		cout << "Word " << count2 << ": ";
    		//
    		for (base; (base < count); base ++)
    		{
    			cout << phrase[base];
    		}
    		cout << '\n';
    		base = count + 1;
    		count++;
    		count2++;
    		
    	}
    }
    Also I have to account for \r, \n, \t...but when i make the while loop look for it...I get the error "illegal escape sequence" I tried this to look for it:
    Code:
     while ((phrase[count] = '\') && (phrase[count+1] = 'n'))
    Also we are not allowed to use strtok. Only using like comparing (==) in an if or while statement or a switch statement for one char.

    Thanks for the help,

    ...Dan
    Last edited by Dan17; 12-07-2005 at 01:10 PM.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Code:
    while (count < LINELENGTH)
    My guess is the above is causing problems because the string you enter from the keyboard is probably not that long. You need to do something like this.
    Code:
    int len = strlen(phrase);
    while( count < len )
    {

  3. #3
    System-7
    Join Date
    Nov 2005
    Posts
    65
    Thanks for the help! It's not displaying that text anymore except for it is missing one word now...but that I can fix.

    Also does anyone have any idea on how to account for \r, \n, \t...but when i make the while loop look for it...I get the error "illegal escape sequence"?

    Thanks,

    ...Dan

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    '\n' is a single character, so just check for '\n' instead of '\' and 'n' separately. Also, remember to use == instead of = when checking for equality. You probably don't have to worry about '\r', it is converted automatically to '\n' by the stream code.

  5. #5
    System-7
    Join Date
    Nov 2005
    Posts
    65
    If it is a single character...when it is typed in with a string...lets say: hello\nworld

    wouldn't the strlen count 12 characters...\n as 2?

    ...Dan

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    No, that's 11 characters. The \ is just an escape character to indicate that you are using a special character. There is no newline character on your keyboard, so you use an escape sequence to indicate that you want the newline to appear there.

  7. #7
    System-7
    Join Date
    Nov 2005
    Posts
    65
    Right now my code is looking like this:
    Code:
    #include <iostream>
    using namespace std;
    
    #define LINELENGTH 100
    
    void spaceWords(char phrase[]);
    
    int main(void)
    {
    	while (!cin.eof())
    	{
    		
    		char phrase[LINELENGTH] = {' '};
    		
    		cout << "Please enter a line of text: ";
    		cin.getline (phrase, LINELENGTH);
    
    		spaceWords(phrase);
    	}
    	return 0;
    }
    void spaceWords(char phrase[])
    {
    	int count = 0;
    	int total = 0;
    	int base = 0;
    	int count2 = 1;
    	int length = strlen(phrase);
    
    	//loop until count is equal to line length
    	while (count <= length)
    	{
    		//while not a blank space, add to the counter
    		while (phrase[count] != ' ')
    		{
    			count ++;
    		}
    
    		//print out word number	
    		cout << "Word " << count2 << ": ";
    
    		for (base; (base <= count) && (count <= length); base ++)
    		{
    			cout << phrase[base];
    		}
    		cout << '\n';
    		count++;
    		base = count;
    		count2 = count2 + 1;		
    	}
    }
    The problem is that since I got rid of the extra characters added in using the line strlen...now my last word is not being displayed...I have tried many things to get it to display but it wont. The really weird thing is if I type in Hello World...the count before the hello will reach 5 (which is correct...5 characters) but after it loops, before World will reach 192 (which is obviously not correct). If i use more than two words, it is always the last world that has an unusually high count number

    Any help would be great,

    ...Dan

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You need to check for count less than length in the loop looking for the space. Remember that there is no space at the end of "Hello World". There aren't any characters. The second time through your loop looking for the space will go on forever until it finds a space in the unknown memory that you access past the bounds of phrase. Simply break that loop if you reach the end of phrase and it should work better.

  9. #9
    System-7
    Join Date
    Nov 2005
    Posts
    65
    Thanks Daved! The program is working great now.

    Thanks you guys for the help,

    ...Dan

  10. #10
    System-7
    Join Date
    Nov 2005
    Posts
    65
    I have one last problem and then it should be good. I am having trouble with finding \n in the input and then separating the words. I came up with this line but it did not work:
    Code:
    while ((phrase[count] != ' ') && (count < length) && (phrase[count] != '\n'))
    Thanks again for the help,

    ...Dan

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    There won't be any newline in the input. You use getline to get the data. The getline function reads up until the first newline, but doesn't include that newline in the text it saves in the string (it just ignores it). So phrase will never have a newline character in it as long as you use getline to read in the line.

    When count is no longer less than length, then you know you've reached the end of the line.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Or you just check for the null character. Or just check for anything that falls in under isspace, and save yourself the time and effort.


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    System-7
    Join Date
    Nov 2005
    Posts
    65
    Well this is what the teacher asked us to do:
    Write a complete C++ program that will read a line of text at a time from the keyboard as a string. Your program will separate it into words for any of these 4 whitespace characters -- blank ' '; tab '\t'; return '\r'; and newline '\n' – and print each word. Upon receiving end of file, your program will terminate.
    Use cin.getline() to read a whole line of input at a time (see both table 6-3 on page 218 and table 6-4 on page 219) as in this sample code fragment:

    #define LINELEN 100
    ...
    char buffer[LINELEN];
    ...
    cin.getline(buffer, LINELEN);

    No string functions are required, but you may use them if you wish. However, you may not use the function strtok() if you should happen across it. It is deprecated since it alters its string argument without warning. You can compare characters with == in an if or while statement, or use a switch statement for one char.
    Your screen will resemble:

    Enter string: test one line now
    Word 1: test
    Word 2: one
    Word 3: line
    Word 4: now
    Enter string: ^Z
    I think where my program is at now it will account for what the teacher wants to input. If I like type in Hello <TAB KEY> WORLD, is displays both words correctly. Does it appear that I am missing anything?

  14. #14
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Yes you do miss something. You are not checking for the tab-character.
    If you input multiple whitspace characters you count them as a word.
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input a Hex number and output hex number to a text field
    By zoobaby in forum C++ Programming
    Replies: 4
    Last Post: 05-12-2009, 11:26 AM
  2. Reading Character at a time from a text file
    By Giania in forum C Programming
    Replies: 8
    Last Post: 02-25-2006, 03:17 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. C++ text file output...specifics
    By paranoidandroid in forum C++ Programming
    Replies: 3
    Last Post: 12-31-2001, 10:57 AM
  5. Validating the contents of a char buffer
    By mattz in forum C Programming
    Replies: 3
    Last Post: 12-09-2001, 06:21 PM