read from file, runtime error

This is a discussion on read from file, runtime error within the C++ Programming forums, part of the General Programming Boards category; I am trying to read in from a file, remove numbers, and hyphens so that the output of this... 1. ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    45

    read from file, runtime error

    I am trying to read in from a file, remove numbers, and hyphens so that the output of this...

    1. HellO does- This Work
    would be...

    hello does this work
    I believe this code works the way I expect it too...however, i copied a large amount of text from the internet and pasted it into a text document. The program gets a runtime error...

    Debug Assertion Failed!
    ...
    Expression: (unsigned)(c+1)<=256
    ...
    To troubleshoot I took a very small part of the text and ran it and retyped any non alpha-numeric characters. This worked, however there is way too much text to go through line by line and replace...actually that would defete the purpose of having this program. I am guessing that the text contains non-standard types of characters? Any ideas on how to go about this? Thanks.

    Code:
    void formatText(string filename)	
    {
        ifstream fin(filename.c_str());
        ofstream formatout("format.txt");
        
        string word;
        while (fin >> word)
        {
            int x;
            int length = word.length();
            for (x = 0; x < length; x++)
            {
    	    char c = word[x];
    	    if (isalpha(c))				//checks for letter
    	    {
      	        c = tolower(c);			     //makes lowercase
    	        formatout << c;
    	    }
    	    else if (c == '-')			       //removes hyphens
    	        formatout << " ";
    	}
            formatout << " ";
        }
    }

  2. #2
    Banned
    Join Date
    Jun 2005
    Posts
    594
    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <algorithm> 
    #include <cctype>
    
    using namespace std;
    
    int main()
    {
    	string filename = ("file.txt");
    	ifstream fin(filename.c_str());
    	ofstream formatout("format.txt");
    	 
    	string word;
    	
    	while(getline(fin, word, '\n'))
    	{
    		transform(word.begin(), word.end(), word.begin(), (int(*)(int))tolower);
    		for(int i = 0; i != word.size()-1; i++)
    		{
    			if(word[i] == '-')
    			{
    				word[i] = ' ';
    			}
    		}
    		formatout << word << "\n";
    	}
    	fin.close();
    	formatout.close();
    }
    }

    that my post on how id do it, if youd prefer to use your way, i
    need you to post your text file, or at least a good portion of it
    so i have something to test. but your error is comming
    from isalpha().
    Last edited by ILoveVectors; 08-03-2005 at 10:38 AM.

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    45
    Thanks for your help. This compiles fine without runtime errors, however, it simply ignores the characters giving trouble to isalpha(), namely the hyphen.

    I am going to attach a portion of the text and you can see what i mean. If you delete the hyphens and replace them with hyphens...it works fine(don't ask)

    Also, do you have any idea how to replace any apostrophe's with blank spaces?
    Attached Files Attached Files

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    There aren't any non-standard characters in the text you posted, and your program works fine on it for me.
    Last edited by 7stud; 08-03-2005 at 12:08 PM.

  5. #5
    Registered User
    Join Date
    Aug 2004
    Posts
    45
    make sure to look closely though...it does not remove the hyphens as it should

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    make sure to look closely though...it does not remove the hyphens as it should
    I did, and it looks like it works to me. Here is an example:
    Romans 1

    1Paul, a servant of Christ Jesus, called to be an apostle and set apart for the gospel of God— 2the gospel
    romans paul a servant of christ jesus called to be an apostle and set apart for the gospel of god the gospel

  7. #7
    Registered User
    Join Date
    Aug 2004
    Posts
    45
    Oh i see... you used my original code and it worked?

    As far as syntax, I was able to get that to work the way i wanted it to with text i typed in. The problem came up with this particular text that i copied...I will try it again.

    Thanks

  8. #8
    Registered User
    Join Date
    Aug 2004
    Posts
    45
    i have successfully accomplished what i wanted with a combination of my original code and also ILoveVectors example. The program removes punctuation and lowers everything to lowercase. This is the issue i have come up against though, and i am not sure if this is a programming problem or simply just a logical problem, but you all seem like very smart people, so i will give it a go.

    How can i distinguish between a word with a hyphen such as...

    self-esteem
    and...

    for the gospel of God— 2the gospel
    and...

    For since the creation of the world God's invisible qualities—his eternal power and divine nature—have been clearly seen, being understood from what has been made, so that men are without excuse.
    The former being necessary and the latters being unnecessary. Any ideas? thanks
    Last edited by quizkiwi; 08-05-2005 at 09:53 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 02:17 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM

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