Code that looks for Comments

This is a discussion on Code that looks for Comments within the C++ Programming forums, part of the General Programming Boards category; Help--C++ very weak--I have to write and test code that recognizes one form of the comments of the C++ language ...

  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,812
    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,270
    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,812
    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 05: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, 09:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 08: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, 06:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM

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