Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,672 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,163 people online right now. Registration is fast and FREE... Join Now!




Quick Refresher help PLEASE HELP

 
Reply to this topicStart new topic

Quick Refresher help PLEASE HELP, Getting Off this constant loop

HermosaBeach
post 4 Sep, 2007 - 08:33 PM
Post #1


New D.I.C Head

*
Joined: 30 Aug, 2007
Posts: 11


My Contributions


Im trying to do a refresher on tokenizing a string without actually using the string class. I ran into a possible logic problem , maybe someone can help me.


everything works just fine, I am just having a problem with my next() function in my tokenizer class.If someone wants to take a look at it that would be great .. I divided the whole source into modules.


Here is the driver:
CODE

#include "stringTokenizer.h"
#include <iostream>
using namespace std;


int main(){
    // get input from the user
    int size = 256;
    char sentence[size];
    cout << "Enter a sentence to be Tokenized:" << endl;
    char delimiter = ' ';
    cin.getline(sentence, size);
    
    //test if user input is inside variables
    cout << sentence << endl;

    char *ptr;
    
    ptr = sentence;
    
    //create object
    stringTokenizer firstObject(ptr,delimiter);

    char *tokens;
    
    tokens = firstObject.next();
    while (tokens != NULL)
    {
        cout << tokens << endl;
        tokens = firstObject.next();
        
    }
    system("PAUSE");
    return 0;
}



Here is the header:
CODE

#include <iostream>
using namespace std;


class stringTokenizer{
    
    
public:
    
    stringTokenizer(char sentence[],char delimiter);
    char *next();
private:
    char *originalString;
    char token;
    
};




Here is the definitions:
CODE

#include "stringTokenizer.h"
#include <iostream>

stringTokenizer::stringTokenizer(char sentence[],char delimiter){
    
    originalString = sentence;
    token = delimiter;
    
    cout << "In Private data type original string is :" << originalString << endl;
    cout << "In private data Type token is : " << token;
}


//here is my problem!
char* stringTokenizer::next(){

    for(int x = 0; x < sizeof(originalString); x++)
    {
          if(originalString[x] == ' ')
          {
            originalString[x] = token;
            
          }
        
    }

    return originalString;
}






If you run and compile , you will get an infinite loop. I am close to finishing this, but I am having a problem separating the sentence with delimiters.

This post has been edited by HermosaBeach: 4 Sep, 2007 - 08:37 PM
User is offlineProfile CardPM

Go to the top of the page

Bench
post 5 Sep, 2007 - 02:28 AM
Post #2


D.I.C Addict

Group Icon
Joined: 20 Aug, 2007
Posts: 602



Thanked 10 times

Dream Kudos: 150

Expert In: C/C++

My Contributions


I assume you've deliberately not allocated any space for a char array in your stringTokenizer class, and are using stringTokenizer as if it were a Functor class?


your next() method never seems to modify the originalString pointer, so whenever its called, all it does is return a pointer to the beginning of the string.
CODE

char* stringTokenizer::next(){

    for(int x = 0; x < sizeof(originalString); x++)
    {
          if(originalString[x] == ' ')
          {
            originalString[x] = token;
            
          }
        
    }

    return originalString;
}

You also have a problem here
for(int x = 0; x < sizeof(originalString); x++)
sizeof(originalString) is the size of a pointer in bytes. if you want the length of the string, use strlen() from the <cstring> libary.



In any case - the condition for this while loop will never change
CODE
    tokens = firstObject.next();
    while (tokens != NULL)
    {
        cout << tokens << endl;
        tokens = firstObject.next();
        
    }



Presumably, you want something like this..
CODE
char* stringTokenizer::next()
{
    //boolean - Has the delimiter been found?
    bool next_token = false;

    //Run while not at the null-terminator
    while( originalString[0] != '\0' )
    {
        if( originalString[0] == token )
            //delimiter found!
            next_token = true;

        //Move the string pointer forward 1 place
        ++originalString;

        if( next_token == true )
            return originalString;
    }
    //No delimiter found - reached end of the string
    return NULL;
}



Also, this line shouldn't compile - size needs to be const unless you're dynamically allocating your array
CODE
    int size = 256;
    char sentence[size];


either const int size = 256; or enum { size = 256 }; should fix it.

This post has been edited by Bench: 5 Sep, 2007 - 02:30 AM
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 06:10AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month