Removing Comments

hi, this code below lets me insert a .cpp file and removes any comments start with /* and end with */ so i edited it so that it removes comments start with // .
but there is a problem in it : when it find // it starts removing and can't stop .
i want to tell the code when you find a new line or whatever, stop removing. how can i do this ?


code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <fstream>
using namespace std;

int main (){
ifstream infile;
string filename;
ofstream outfile;
char c1, c2;
bool isInsideComment = false;

cout << "Please input file name (to remove comments from): ";
cin >> filename;

infile.open (filename.c_str());
if (infile.fail()) {
cout << "\nInvaild file name.\n";
return 1;
}
outfile.open(("out_"+filename).c_str());

infile.get (c1);
while (!infile.eof()) { 
if ((c1 == '/') && (!isInsideComment)) {
infile.get (c2);
if (c2 == '*') 
isInsideComment = true;
else if ((c1 == '/') && (c2 == '/'))
isInsideComment = true;
else {
outfile.put (c1);
outfile.put (c2);
}
}

else if ( (c1 == '*') && isInsideComment) {
infile.get (c2);
if (c2 == '/')
isInsideComment = false;
else if ((c1 == '\n') && isInsideComment)
isInsideComment = false;
}
else if (!isInsideComment)
outfile.put (c1);
infile.get (c1);
}
infile.close();
outfile.close();
}
Last edited on
This code is very familiar. Do you happen to be from Roblox?

Anyway, I'll look at your code.
no i'm not from roblox :)
closed account (zb0S216C)
Why do you want to remove comments? The pre-processor does this for you before compiling the program. Besides, comments are an invaluable documentation feature.

Wazzak
no i want to create a c++ program to insert a cpp file in it to remove the comments from it
This looks like a challenge from "reddit.com/r/dailyprogrammer".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <fstream>
#include <string>

int main(void) {
    std::string source;
    std::ifstream readFile("file.txt");
    getline(readFile, source, '\0');
    while(source.find("/*") != std::string::npos) {
        size_t Beg = source.find("/*");
        source.erase(Beg, (source.find("*/", Beg) - Beg)+2);
    }
    while(source.find("//") != std::string::npos) {
        size_t Beg = source.find("//");
        source.erase(Beg, source.find("\n", Beg) - Beg);
    }
    std::cout << source;
    return 0;
}

I don't know why you are using boolean to decide whether or not the program is "inside of a comment". I've posted an example I just coded up to give you an idea on how you could solve this challenge. If you want me to fix your code, then you need to format your code and put the code between code tags.
ok i put the code between code tags and yes i wanna fix it please
I've tested the code provided and it works flawlessly. I've tested the code below with your program and it removes all of them. Is that not what you want? There are many ways you could improve your code.

1
2
3
4
#include /* string */ <string>

int main() { } // remove me
// now remove me 
ok, but try something like :


1
2
3
4
5
#include /* string */ <string>

int main() { } // remove me
// now remove me 
cout << "Hello";


you will find that the last line is removed because the program removes everything comes after //
I've tested the code provided and it works flawlessly.


1
2
3
4
5
6
7
8
#include <iostream>

const char* c = "/*" ;

int main( )
{
    std::cout << "Will I still be here?" ;
}


Really? =)
Really? =)

Well, that's a problem. :P It's an easy fix though.
So what will be the fix for my problem ???
Another gotcha:

lines that end with a backslash continue on the next line:

1
2
3
4
// this is a single line comment, but it ends with an escape char:  \
  this line is also considered part of the comment \
  as is this one (even if the forum syntax highlighting doesn't show it)
this_is_not_a_comment(); 
Topic archived. No new replies allowed.