Thread: Sentence to a list , need help

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    2

    Sentence to a list , need help

    The idea is to create a " list " given a user inputed sentence.
    For example:" Hello I am here " . WOuld output the list .
    Hello
    I
    am
    here
    ----------------------
    I have tried to compile and I get a error , but it tells me nothing .I think it is right and should work.
    ----Here is the code if someone can help I would greatly appreciate it.


    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    void main()
    {
    	string sentence;
    
    	char string1[]="";
    	char space[]=" ";
    	char period[]=".";
    	char word1[]=" ";
    	char word2[]=" ";
    
    	cout << "This program turns a given sentence into a list of words." << endl;
    	cout << "Input a sentence: ";
    	getline(cin, sentence);
    	cout << endl;
    	
    	int len = sentence.length();
    	strcpy(string1, sentence.c_str());
    
    	int i;
    	int t;
    	
    	for(i=0; i<len; i++)
    	{
    		if(string1[i]==space[0] || string1[i]==period[0])
    		{
    			for(t=0; t<i; t++)
    			{
    				word1[t]=string1[t];
    			}
    			break;
    		}
    	}
    
    	i=i+2;
    	t=t+2;
    
    
    	for(i; i<len; i++)
    	{
    		if(string1[i]==space[0] || string1[i]==period[0])
    		{
    			for(t; t<i; t++)
    			{
    				int b=0;
    				word2[b]=string1[t];
    				b=b+1;
    			}
    			break;
    		}
    	}
    
    	cout << endl;
    	cout << word1 << endl;
    	cout << word2 << endl;
    
    
    	int endProg=0;
    
    	while(endProg==0)
    	{
    		cout << "To end the program type 1: ";
    		cin >> endProg;
    		cout << endl;
    	}
    
    }

  2. #2
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Code:
    char string1[]="";
    I believe string1 only has 1 element and it's '\0'. And since it's not a dynamic array and it's size is way too small, thus the errors.
    Code:
    strcpy(string1, sentence.c_str());
    "All that we see or seem
    Is but a dream within a dream." - Poe

  3. #3
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    Why not use strtok with delimiters? Seems you could make the whitespace a delimiter and be done with it quick.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Once you have your sentence...

    1. Initialize a stringstream object with the sentence.
    2. Parse the stringstream using >> to extract the individual words in a loop.
    3. Store the individual words into a container of some sort std::list, std::vector, etc...
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    printf("Hello Cboard\n"); codeprada's Avatar
    Join Date
    Jan 2011
    Location
    In a little room at the back of your brain
    Posts
    68
    Quote Originally Posted by ~Kyo~ View Post
    Why not use strtok with delimiters? Seems you could make the whitespace a delimiter and be done with it quick.
    ^ this
    Code:
    #include <iostream>
    #include <string.h> //Codeblocks needs this
    
    using namespace std;
    
    int main(int argc, char * argv[])
    {
        string sentence = "Hello Cboard I am typing absolutely nothing.";
        size_t sz = strlen(sentence.c_str());
        char * sp = new char[sz];
        strcpy(sp, sentence.c_str());
        sp[sz] = '\0';
        char * tok = strtok(sp, " ");
        while (tok != NULL) {
            cout << tok << endl;
            tok = strtok(NULL, " ");
        }
        delete sp;
        delete tok;
        return 0;
    }
    We shouldn't be quick to criticize unless we can do better.
    Code:
    public function __clone() { die ( "I'm one of a kind" ); }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c program that accepts and executes commands?
    By Cimposter in forum C Programming
    Replies: 3
    Last Post: 09-30-2009, 02:58 PM
  2. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  3. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 09:01 AM
  4. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 05:20 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM