-
Code formatter help
I decided to make a program that keeps the same formatting of the c++ code but inserts the color tags for this forum.
I got that all to work right, but now I am trying to make it not colorize the keywords that are in between the quotation.
I was running into some trouble so I decided before implementing it with quotes I would try and make it not highlight anything between %. But it is not working right
Here is my code, and here it is also ran through my code formatter so you can not only see the code but the problem (at the start in the commented section with the %)
Code:
#include <iostream>
#include <fstream>
#include <string>
// % double int char bool % char bool
using namespace std;
int main()
{
string item;
bool hold = false;
ifstream in_file;
ofstream out_file;
in_file.open("raw.txt");
if ( in_file.fail() )
{
cout << "Error opening input file";
cin.get();
return 1;
}
out_file.open("formated.txt");
if ( out_file.fail() )
{
cout << "Error opening input file";
cin.get();
return 1;
}
while ( !in_file.eof() )
{
if ( in_file.peek() == '\n')
{
out_file << '\n';
in_file.get();
}
else if ( in_file.peek() == ' ')
{
out_file << " ";
in_file.get();
}
else if ( in_file.peek() == '\t')
{
out_file << '\t';
in_file.get();
}
else
{
in_file >> item;
char temp1 = item[0] ;
int i = item.length();
if (temp1 == '%' || item[i-1] == '%' || item == "%")
{
if (hold == false ) hold = true;
else if (hold == true ) hold = false;
}
if ( item == "int" || item == "double" || item == "char"
|| item == "using" || item == "namespace" || item == "#include"
|| item == "return" || item == "void" || item == "class"
|| item == "delete" || item == "#ifndef" || item == "#define"
|| item == "#endif" || item == "public" || item == "protected"
|| item == "const" || item == "private" || item == "string"
|| item == "bool" || item == "true" || item == "true;" || item == "false" || item == "false;"
|| item == "if" || item == "if(" || item == "else" || item == "do" || item == "while" || item == "while("
|| item == "friend" || item == "operator"
&& hold == false )
{
item.insert(0,""); //Ignore these 2 lines of code
item.insert(item.length(),""); //The board is detecting the insert color string and it is causing trouble
}
out_file << item;
}//End of else
}//End of while
in_file.close();
out_file.close();
system("formated.txt");
return 0;
}
-
First of all:-
Anything with EOF is generally bad news. Get rid of it. Use this instead
Code:
while(infile>>variable)
.
Next, think about what has priority.
For example, if you encounter a quote you should turn off the rest of your code formatting to ignore highlighting key words, such as while, for etc.
And likewise, if your comment has quotes in them, you would ignore the formatting for the quotations, since comments have priority over quotes.
Once you have your rules of precedence in place the rest should be easy.
Oh btw this has been done before:
But don't bother looking at this link if you want to do this as a exercise or just for fun.
;)
http://cboard.cprogramming.com/showt...ht=code+format
-
Yeah, I was just out walking my dog and thinking about the problem and I was luke "duh".
And yes I know someone had already made one, I was just doing it for fun. I will still be to lazy to use it when I post though :)