View Poll Results: Idea

Voters
2. You may not vote on this poll
  • You're an idiot!

    1 50.00%
  • Too old, it's been done.

    0 0%
  • Great idea!

    0 0%
  • I think you have a start!

    1 50.00%

Thread: I'll need some coffee...

  1. #1
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937

    I'll need some coffee...

    Ok. I'm creating an AI conversation program. The first thing I'm doing is creating a library that dissects sentences. The breakdownintowords() function is working very well, except for a small bug (note that the function isn't complete). Whenever I input "Do you know my grnadmother?", it displays:
    (seperating words)
    do
    you
    know
    my
    |\|||&^(ect....)|\my

    It looks like char word5[21] is not being written to. Below is the main code, then the class code, and then specifically where I think the problem is:

    Code:
    #include <iostream.h>
    #include "input.h"
    
    int main()
    {
    	sentence user(150);
    
    	cin.getline(user,150,'\n');
    
    	cout << user << endl;
    	
    	user.BreakDownIntoWords(10);
    
    	return 0;
    }
    The class code (with function.....):

    Code:
    class sentence
    {
    public:
    	int length;
    	char* value;
    
    	sentence(int temp_length)
    	{
    		value = new char[temp_length];
    		length = temp_length;
    	}
    	~sentence()
    	{
    		delete value;
    	}
    
    	char& operator[](int ind)
    	{
    		return value[ind];
    	}
    	
    	operator char*()
    	{
    		return value;
    	}
    
    	int BreakDownIntoWords(int words_max);
    
    };
    
    sentence::BreakDownIntoWords(int words_max)
    {
    	int wordstart = 0;
    	int numberofwords = 0;
    	int wordcount = 0;
    	char* word;
    	char word1[21];
    	char word2[21];
    	char word3[21];
    	char word4[21];
    	char word5[21];
    	char word6[21];
    	char word7[21];
    	char word8[21];
    	char word9[21];
    	char word10[21];
    	char word11[21];
    	char word12[21];
    	char word13[21];
    	char word14[21];
    	char word15[21];
    	char word16[21];
    	char word17[21];
    	char word18[21];
    	char word19[21];
    	char word20[21];
    	for(int x = -1; x < length; x++)
    	{
    		switch(value[x])
    		{
    		case '\0':
    			{
    				goto END;
    			}break;
    		case ' ':
    			{
    				wordcount++;
    				switch(wordcount)
    				{
    				case 1:
    					{
    						word = word1;
    					}break;
    				case 2:
    					{
    						word = word2;
    					}break;
    				case 3:
    					{
    						word = word3;
    					}break;
    				case 4:
    					{
    						word = word4;
    					}break;
    				case 5:
    					{
    						word = word5;
    					}break;
    				case 6:
    					{
    						word = word6;
    					}break;
    				case 7:
    					{
    						word = word7;
    					}break;
    				case 8:
    					{
    						word = word8;
    					}break;
    				case 9:
    					{
    						word = word9;
    					}break;
    				case 10:
    					{
    						word = word10;
    					}break;
    				case 11:
    					{
    						word = word11;
    					}break;
    				case 12:
    					{
    						word = word12;
    					}break;
    				case 13:
    					{
    						word = word13;
    					}break;
    				case 14:
    					{
    						word = word14;
    					}break;
    				case 15:
    					{
    						word = word15;
    					}break;
    				case 16:
    					{
    						word = word16;
    					}break;
    				case 17:
    					{
    						word = word17;
    					}break;
    				case 18:
    					{
    						word = word18;
    					}break;
    				case 19:
    					{
    						word = word19;
    					}break;
    				case 20:
    					{
    						word = word20;
    					}break;
    				default:
    					{
    						cout << "ERROR: Critical array overbounds!!!" << endl;
    						return 0;
    					}break;
    				}
    				int z = 0;
    				for(int y = wordstart; y < x; y++)
    				{
    					word[z] = value[y];
    					z++;
    				}
    				word[z] = '\0';
    				wordstart = x + 1;
    					
    					
    			}break;
    		default:
    			{
    			}break;
    		}
    	}
    END:
    	cout << word1 << endl;
    	cout << word2 << endl;
    	cout << word3 << endl;
    	cout << word4 << endl;
    	cout << word5 << endl;
    	return numberofwords;
    }
    Where I think the problem is.... (in word seperating function):

    Code:
    int z = 0;
    for(int y = wordstart; y < x; y++)
    {
      word[z] = value[y];
       z++;
    }
    word[z] = '\0';
    wordstart = x + 1;
    By the way, if you have any critiques (and hell you sure will) about my coding style/technique please voice them. Thanks for the help.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    i don't suggest ever using variables which are organized with numbers in them: (ie: object1, array2, etc) unless they're there for temporary use only, and used very infrequently at that.
    use an array of char*'s instead.

    i think your problem is in splitting up the words. you may want to add a space to the end of the sentence to simplify things.

  3. #3
    Registered User fletch's Avatar
    Join Date
    Jul 2002
    Posts
    176
    Code:
    ~sentence()
    	{
    		delete value;
    	}
    Should be 'delete [] value' to delete the entire array. 'delete value' only deletes the first character.

    Code:
    for(int x = -1; x < length; x++)
    	{
    		switch(value[x])
    Why does x start out at -1? The first cell in the value array should be value[0], not value[-1].

    Your program separates a word each time it encounters a space. "Do you know my grandmother?" Since there is no space after the word grandmother, it's never separated from the rest of the string. The switch encounters '\0' and exits. Try entering 'Do you know my grandmother ' with a space after grandmother. It'll work fine.

    Code:
    case 1:
           {
                  word = word1;
           }break;
    You don't need the braces here. Try this
    Code:
    case 1:
           word = word1;
           break;
    Or, even better in terms of readability
    Code:
    case 1: word = word1; break;
    case 2: word = word2; break;
    //etc etc etc
    "Logic is the art of going wrong with confidence."
    Morris Kline

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 03-26-2009, 02:47 PM
  2. How many cups of coffee per day
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 35
    Last Post: 03-03-2003, 12:19 PM
  3. QUITTING coffee tips
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 02-15-2003, 01:05 PM
  4. Is coffee going to kill me?
    By Shadow12345 in forum A Brief History of Cprogramming.com
    Replies: 25
    Last Post: 11-27-2002, 07:39 AM
  5. functions ??? help
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-17-2001, 02:33 AM