1 Replies - 657 Views - Last Post: 03 May 2011 - 04:15 PM Rate Topic: -----

#1 mrjohnv01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 03-October 09


Posted 03 May 2011 - 04:08 PM

I have an assignment for my C++ and I am very lost on how to do this homework. I just want to know where and how should I start off this assignment.

Write a program named h33.cpp that removes C++ comments from input, sending the uncommented portion of the program to output. Your program should work with both single line (//) comments, and multi-line (/* */) comments.
 A "/*" sequence is ended by the first "*/" that is encountered.
 Comment characters inside string literals "don't count"
 Strings quotes may be escaped using an escape character.
 Single-line comments // may be included inside multi-line comments
Test your program against several different C++ files with different types of comments.
You'll find an example test file (test.cpp) that you can process. Use redirection to open the file and write to standard output. (That is, use cin and cout, not the filestream classes.) Check the output to see that only comments have been removed.

I kind of get what my professor is asking me. I think I would need to remove those comment line from a sentence.

// Single line comment should be deleted
/* Multi-line comment should be deleted
   // Not really a comment
   "Not really a string"
#include <iostream>  // Single line comment should be deleted
using namespace std;
char * s = "Some text /* with an embedded */ // comment ";

int main(int argc, /* argument count */  char *argv[] /* strings */)
    cout << s << endl;
    cout << "/* this is ok " << endl;
    cout << "as is this */" << endl;

This is the code to test, so the output would be /* this is ok, but I would need to remove that comment so it should come out to be "this is ok." Any help would be much appreciate. I just want someone to guide me.

Is This A Good Question/Topic? 0
  • +

Replies To: stream

#2 ishkabible  Icon User is offline

  • spelling expret
  • member icon

Reputation: 1747
  • View blog
  • Posts: 5,898
  • Joined: 03-August 09

Re: stream

Posted 03 May 2011 - 04:15 PM

i would load the C++ file into a std::string, then use the 'find' method to find and instances of "//". once found i find the "\n" that came after that. then i would remove everything from each instance. i would then do this for the whole string. i would do the same thing but with "/*" and "*/".
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1