When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
8 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
Here is my program: ```
#include <iostream>
#include <string>
using std::string;
#include <fstream>
using namespace std;
enum State{
CODE,
IN_CHAR,
STRONG_CHAR,
IN_STRING,
STRONG_STRING,
POTENTIAL_COMMENT,
SINGLE_LINE_COMMENT,
STRONG_SINGLE_LINE_COMMENT,
MULTI_LINE_COMMENT,
MAYBE_END_OF_COMMENT
};
void UNCOMMENT(const string& inputFileName, const string& outputFileName){
ifstream inputFile(inputFileName);
ofstream outputFile(outputFileName);
State s = CODE;
char c;
while(inputFile.get(c)){
switch(s){
case CODE:
if(c == '''){
s = IN_CHAR;
outputFile << c;
}else if ( c == '/'){
s = POTENTIAL_COMMENT;
}else if (c == '"'){
s = IN_STRING;
outputFile << c;
}else {
outputFile << c;
}
break;
case IN_CHAR:
outputFile << c;
if (c == '\'){
s = STRONG_CHAR;
}else if (c == '''){
s = CODE;
}
break;
case STRONG_CHAR:
s = IN_CHAR;
break;
case IN_STRING:
outputFile << c;
if(c == '"'){
s = CODE;
}else if (c == '\'){
s = STRONG_STRING;
}
break;
case STRONG_STRING:
outputFile << c;
s = IN_STRING;
break;
case POTENTIAL_COMMENT:
if (c == '/'){
s = SINGLE_LINE_COMMENT;
}else if (c == ''){
s = MULTI_LINE_COMMENT;
}
break;
case SINGLE_LINE_COMMENT:
if (c == '\n'){
outputFile << c;
s = CODE;
}else if (c == '\'){
s = STRONG_SINGLE_LINE_COMMENT;
}
break;
case MULTI_LINE_COMMENT:
if(c == ''){
s = MAYBE_END_OF_COMMENT;
}
break;
case MAYBE_END_OF_COMMENT:
if (c == '/'){
s = CODE;
}else if (c != ''){
s = MULTI_LINE_COMMENT;
}
break;
}
}
inputFile.close();
outputFile.close();
}
int main()
{
string inputFileName;
cout << "PLease input a .cpp file to remove comments!" << endl;
getline(cin, inputFileName);
string outputFileName = "output.cpp";
UNCOMMENT(inputFileName, outputFileName);
cout << "Comments have been removed succefully!";
return 0;
}
!close
Thank you and let us know if you have any more questions!
This thread is now set to auto-hide after an hour of inactivity
@upbeat topaz
Please don't delete forum posts. They can be helpful to refer to later and other members can learn from them. In the future you can use !solved to close a post and mark a post as solved.
!solved