0

Here is a comment stripper program from which takes input from Cin and removes the comments.

#include <iostream>
#include <fstream>

char Getchar();
char var;
int main()
{
	char ch;
	do
	{
		Getchar();
		switch(var)
		{
			case '/':
			switch(Getchar())
			{
				case '*':
					Getchar();
					while(true)
					{
						if(var=='*'&&Getchar()=='/')
						{
							break;
						}
						else
						{
							Getchar();
						}
					}
					break;

				case '/':
					while(Getchar()!='\n');
					std::cout<<'\n';
					break;

				default:
					break;
	
			}
			break;
			case '"':
			do
			{
				std::cout<<var;	
			}
			while (Getchar()!='"');
			std::cout<<var;
			break;

			default:
			std::cout<<var;
		}
			
	}
	while(var!='x');
	
}

char Getchar()
{
	 std::cin.get(var);
	 return var;
}

So any improvements ?
Secondly I am looking for another way by which i can close input any suggestions on that.
I mean this program exits when "x" is inputted. Is there any other way of Exiting?

6
Contributors
22
Replies
25
Views
8 Years
Discussion Span
Last Post by mvmalderen
Featured Replies
  • 1

    [QUOTE=Sky Diploma;865973]What does CTRL+C do?[/QUOTE] [ICODE]CTRL+C[/ICODE] just breaks/stops the execution of a console program :) By the way: I had also expected your program was stripping comments from C/C++ source files, but you can easily adapt it to remove comments from files :) Read More

  • 2
    WaltP 2,905   8 Years Ago

    [QUOTE=Sky Diploma;865962]So any improvements ?[/QUOTE] Yes. Stop using TABs to indent. Set your editor to use 4 SPACEs when the TAB key is pressed. As for exiting, you can exit when ENTER is pressed all by itself. And about Ctrl+C, look it up. Read More

  • 1
    ArkM 1,090   8 Years Ago

    Other failed tests: [code=cplusplus] f("\""); // Strip me! [/code] [code=cplusplus] ch = '\"'; // Strip me! [/code] Probably, this is too much!(?) : [code=cplusplus] #if 0 /* Code wrecks... #endif cout << "Hello, Sky Diploma!\n"; [/code] Read More

0

Oh, so all it does is strip comments from keyboard input and not from a *.c or *.cpp data file??? Why would you want to write such a program?


>>I mean this program exits when "x" is inputted. Is there any other way of Exiting?

Just press Ctrl+C.

0

Oh, so all it does is strip comments from keyboard input and not from a *.c or *.cpp data file??? Why would you want to write such a program?


>>I mean this program exits when "x" is inputted. Is there any other way of Exiting?

Just press Ctrl+C.

Wouldnt I be Able To Replace cin and cout with filestreams and remove comments from .c or .cpp files

This is an exercise in my c++ book so I just wrote it down. As I am teaching myself C++ i post down the code and ask you guys for improvements :)

What does CTRL+C do?

0

Improvements? Try to process this (correct) line in C++ and see what happens:

ifstream file("//please/dont/strip.me");

;)

0

Improvements? Try to process this (correct) line in C++ and see what happens:

ifstream file("//please/dont/strip.me");

;)

It returns

ifstream("//please/dont/strip.me");

1

What does CTRL+C do?

CTRL+C just breaks/stops the execution of a console program :)

By the way: I had also expected your program was stripping comments from C/C++ source files, but you can easily adapt it to remove comments from files :)

Votes + Comments
Thanks :)
2

So any improvements ?

Yes. Stop using TABs to indent. Set your editor to use 4 SPACEs when the TAB key is pressed.

As for exiting, you can exit when ENTER is pressed all by itself.

And about Ctrl+C, look it up.

Votes + Comments
Done !! Thanks :)
I didn't know that about the TABs, I'll surely try it :)
0

Wouldnt I be Able To Replace cin and cout with filestreams and remove comments from .c or .cpp files

Actuall you don't have to change a thing to make it get input from a data file instead of the keyboard -- just direct stdin from data file, like this: c:>myprogram.exe <test.cpp <Return> When the above is typed from a command prompt window your program will get input from the file test.cpp instead of the keyboard.

0

Actuall you don't have to change a thing to make it get input from a data file instead of the keyboard -- just direct stdin from data file, like this: c:>myprogram.exe <test.cpp <Return> When the above is typed from a command prompt window your program will get input from the file test.cpp instead of the keyboard.

But it won't strip the comments out of the file ...

0

If you want the comments stripped from the file then just redirect stdout to another file c:>myprogram.exe <test.cpp >newfile.cpp <Return>

0

Thats really Nice to Know :)

Will that work on all platforms

Is this just like using Pipes to pass information

for example
WIndows.

C:> dir >>output.txt

Would put all the output of the Dir command In Output.txt

0

Will that work on all platforms

I know that it works on Windows, Linux and Unix :P

for example
WIndows.

C:> dir >>output.txt

Would put all the output of the Dir command In Output.txt

Yes, but remember that >> is different from > : >> appends to the file (if it exists, otherwise it's just creating the file and writing the output of the dir command to it)

For further info on this: http://commandwindows.com/command1.htm

1

Other failed tests:

f("\""); // Strip me!
ch = '\"'; // Strip me!

Probably, this is too much!(?) :

#if 0
/* Code wrecks...
#endif
cout << "Hello, Sky Diploma!\n";
0

To echo ArkM there are going to be a lot of exceptions.

The best/easiest way would be to use regular expressions via the boost library.

0

To echo ArkM there are going to be a lot of exceptions.
The best/easiest way would be to use regular expressions via the boost library.

In no circumstances.
There are few syntax contexts where comments occured, so reduced scanner+parser tandem which is capable to strip comments is a very simple and fast code w/o (relatively) cumbersome and slow regex.
However it's not so easy to cope with macros...

0

I agree that I dint consider the /" .

And I

ch = '\"'; // Strip me!

And thought that that was invalid.
So i guess i will work on that as well :)

0

Some additions:
>...to be a lot of exceptions.
Nope.
Essentially, Sky Diploma's code does not recognized correctly three very simple lexical-level constructs:
1. string literals
2. character literals
3. line continuation
Besides that it has a very strange termination condition (letter x - why?! ) and can't react to eof condition...

0
#include <iostream>
#include <fstream>

char Getchar();
char var;
int main()
{
	do{
		Getchar();
		switch(var)
		{
			case '/':
			switch(Getchar())
			{
				case '*':
					Getchar();
					while(true)
					{
						if(var=='*'&&Getchar()=='/')
						{
							break;
						}
						else
						{
							Getchar();
						}
					}
					break;

				case '/':
					while(Getchar()!='\n');
					std::cout<<'\n';
					break;

				default:
					break;
	
			}
			break;
			case '\'':
			std::cout<<var;
			Getchar();
			do
			{
			if(var=='\\'&&Getchar()=='\'')
				{
				std::cout<<'\\';

				}
			std::cout<<var;
			}
			while (Getchar()!='\'');
			std::cout<<var;
			break;
			case '"':
			do
			{
				if(var=='\\'&&Getchar()=='"')
				{
				std::cout<<'\\';

				}
				std::cout<<var;
				Getchar();	
			}	while (var!='"');
			std::cout<<var;
			break;
			default:
			std::cout<<var;
		}
			
	}
	while(std::cin);
	
}

char Getchar()
{
	 std::cin.get(var);
	 return var;
}

This is like the improvised function.
However i still am not able to crack "\//" AND "/*................." Till end of file though.
Please Help me out with that .

Though I dint test it through a lot of Code. I Think that this would be enough. But would it?

0

You could also use the following code to strip all the C++ style comments:

char expr[80]; // user inputted expression (with/without comments)
/* Strip down the '//'-comments */
char *p = strstr(expr, "//");
if(p) *p='\0';

expr now contains the expression without all the C++ comments :)

The C-style comments are a bit harder to do, but it's do-able :P

0

You could also use the following code to strip all the C++ style comments:

char expr[80]; // user inputted expression (with/without comments)
/* Strip down the '//'-comments */
char *p = strstr(expr, "//");
if(p) *p='\0';

expr now contains the expression without all the C++ comments :)

The C-style comments are a bit harder to do, but it's do-able :P

I dont think that this will work out.

string s="foldername//file";

The above function would remove the file"; part.

0

I dont think that this will work out.

string s="foldername//file";

The above function would remove the file"; part.

True, but keep in mind that this way doesn't work directly with C++ strings :) (however with a few modifications you can make it also work for C++ strings :))

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.