Assertion Error on string class assignment

This is a discussion on Assertion Error on string class assignment within the C++ Programming forums, part of the General Programming Boards category; Sheesh...my problems continue now, but with a different flavor. I'm reading data in from a file, and the data is ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    80

    Assertion Error on string class assignment

    Sheesh...my problems continue now, but with a different flavor. I'm reading data in from a file, and the data is mostly jibberish. My goal is to extract only words, which are defined as sequences of letters. I have a little loop that I figured would do that for me. I don't want to do char by char input, as the file is huge, and that would be slow. Thus, I decided to read the file line by line into mem, and extract words from there. It appears to get the first word into temp2 correctly, but when I try to "reset" temp2 to be empty, I get an assertion error. Any ideas?

    Code:
    while(in)
            {
                getline(in, temp);
                for (j = 0; j < temp.size(); j++)
                {
                    if (isalpha(temp[j]))
                        temp2 = temp2 + temp[j];
                    else
                    {
                        Files files;
                        files.setWord(temp2.c_str());
                        wordStruct.addWord(files, i);
                        temp2 = "";                    
                    }
                }
                
            }
    Last edited by BigDaddyDrew; 03-24-2003 at 03:27 PM.

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    How about posting all your code? We really can't help you if we don't know what data types your variables are and what is happening around the code you posted. Also, what's Files? Thats not a standard C++ class. You might want to include the header file of that when you post the other code.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    80
    The Files class doesn't have anything to do with my problem...the difficulties lie with the temp2 variable which is a standard string object. I think the problem has to do with the fact that I used temp2.c_str()....but, I'm not positive. If this was the problem, then I don't know how to rememdy it.

    Edit: I did a little bit of switching, etc, and I do not believe the problem is related to the temporary c_str() conversion....

    I'll post the entire getData function, along with the Files.h header.

    Code:
    void getData(vector<char*>& directories, Word& wordStruct)
    {
        ifstream in;
        string temp, temp2;
        int i = 0, j = 0;
        for (i = 0; i < 3; i++)
        {
            if (i == 0)
                in.open("f1.txt");
            if (i == 1)
                in.open("f2.txt");
            if (i == 2)
                in.open("f3.txt");
            while(in)
            {
                getline(in, temp);
                for (j = 0; j < temp.size(); j++)
                {
                    if (isalpha(temp[j]))
                        temp2 = temp2 + temp[j];
                    else
                    {
                        Files files;
                        files.setWord(temp2.c_str());
                        wordStruct.addWord(files, i);
                        temp2 = "";                    
                    }
                }
                
            }
            in.close();
        }
    }
    
    
    class Files
    {
    private:
        vector<int> fileList;
        char* word;
    
    public:
        Files()
        { word = NULL; }
        ~Files()
        { if (word != NULL) delete word; word = NULL; }
        void addFile(int);
        char* getWord()
        { return word; }
        void setWord(const char*);
        void printFile();
    };
    Last edited by BigDaddyDrew; 03-24-2003 at 03:53 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 06:57 AM
  2. how get string vector from class in file?
    By tord in forum C++ Programming
    Replies: 3
    Last Post: 06-17-2005, 09:58 AM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. "Operator must be a member function..." (Error)
    By Magos in forum C++ Programming
    Replies: 16
    Last Post: 10-28-2002, 01:54 PM
  5. Replies: 3
    Last Post: 12-03-2001, 12:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21