Thread: Problems stripping comments

  1. #1
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949

    Problems stripping comments

    I'm trying to strip the code of all comments. However, as you can see, it doesn't work right. Anyone know the problem?

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    int main() {
    	string str = "", main = "";
    	ifstream in("file.txt");
    	int pos;
    	while(!in.eof()) {
    		in.getline((char *) str.c_str(), 255);
    		pos = str.find("//");
    		if(pos != string.npos) {
    			main += str.erase(pos, str.length());
    		} else {
    			main += str;
    		}
    		main += '\n';
    	}
    	cout << main << endl;
    	in.close();
    	cin.get();
    	return 0;
    }
    I'm running it with this as file.txt:
    Code:
      one; // comment 1
    		two;
    
    
    
    
    
    r;
    four;
    Do not make direct eye contact with me.

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    try changing this:
    main += str.erase(pos, str.length());

    to this:
    str.erase(pos, str.end());
    main += str;

    As far as I can tell string::erase() takes either a single iterator or a range of iterators. even if it took a starting position and the number of char to erase (which is what I think you are trying to do), you probably don't want to erase the entire length of str as you will go out of bounds unless pos == str.begin().

  3. #3
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    How do I get an iterator to the posth position?
    Do not make direct eye contact with me.

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Quote Originally Posted by Lurker
    Code:
    in.getline((char *) str.c_str(), 255);


    It should probably be
    Code:
    getline(in, str);
    also:
    Quote Originally Posted by Lurker
    Code:
    pos = str.find("//");
    if(pos != string.npos) {
    	main += str.erase(pos, str.length());
    } else {
    	main += str;
    }
    should be
    Code:
    main += str.substr(0, str.find("//"));

  5. #5
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Great! Thanks for all the help!
    Do not make direct eye contact with me.

  6. #6
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    what about the /**/ comments
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. removing comments of type '//' and '/*'
    By rohit83.ken in forum C++ Programming
    Replies: 3
    Last Post: 10-20-2007, 02:24 AM
  3. The Art of Writing Comments :: Software Engineering
    By kuphryn in forum C++ Programming
    Replies: 15
    Last Post: 11-23-2002, 05:18 PM
  4. tile colliision detection problems
    By werdy666 in forum Game Programming
    Replies: 1
    Last Post: 10-23-2002, 09:26 PM
  5. Coding Problems
    By RpiMatty in forum C++ Programming
    Replies: 12
    Last Post: 01-06-2002, 02:47 AM