Thread: Code that looks for Comments

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    4

    Code that looks for Comments

    Help--C++ very weak--I have to write and test code that recognizes one form of the comments of the C++ language (those that begin with /* and end with */). Thanks.

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Code:
    if(code.find("//*"){
       cout<<"I can find comments";
    }
    Problem solved.
    Woop?

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Problem solved.
    Better would be:
    Code:
    if(code.find("/*"){
       cout<<"I can find comments\n";
    }

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by prog-bman View Post
    Code:
    if(code.find("//*"){
       cout<<"I can find comments";
    }
    Problem solved.
    Code:
    std::cout << "/* I don't think that solves the problem. */" << std::endl;

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    4
    Sorry--here's more info-"C" version of it.

    Code:
    int getComment() {
      getChar();
      if (charClass != AST)
         return SLASH_CODE;
      else {
      do{
          getChar();
             while (charClass != AST)
                 getChar();
         } while (charClass != SLASH);
         return COMMENT;
    }
    This was straight from the book--Thanks for helping

  6. #6
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    My bad so I can't type. Maybe I should make sure I can type better when I am being a jerk . And to be super technical neither of the answers work it needs to be:
    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
    	std::string code = "int main(){ /*Comment*/ int val = 10; return 0;} ";
    
    	if(code.find("/*") != std::string::npos){
    		std::cout<<"I can find comments";
    	}//if
    
    	//Wait for user input
    	std::cin.get();
    
    	return 0;	
    }
    Last edited by prog-bman; 05-29-2007 at 04:09 PM.
    Woop?

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    4
    Thanks prog-bman

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM