0

I have a piece of code that I need to adjust so that it removes all lines with // while copying. I tried to do:

f (prevkar == '/' &&kar!= '/' )
            uitput.put ('/');

but it didn't seem to work.

output.open ("output.txt",ios::out);
kar = input.get ( );
while ( ! input.eof ( ) )
{
    //this part needs to be adjusted
    output.put (kar);
    kar = input.get ( );
}
Amit Yadav
  • 4,422
  • 5
  • 34
  • 79
demi
  • 3
  • 5
  • 1
    When creating a [mcve] to show us, please do it of your actual attempt. Also please read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Oct 13 '19 at 10:17
  • On an unrelated note, please read [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons). – Some programmer dude Oct 13 '19 at 10:17

1 Answers1

0

You may change it to the following: (supposed your input is null-terminated)

bool linebreak=false, putknel=false;
while (kar!='\0')
{

    if(linebreak)
    {
        if(kar=='\n')linebreak=false;
    }
    if(kar!='/')
    {
        output.put (kar);
        if(putknel)
        {
            putknel=false;
            output.put('/');
        }
    }
    else 
    {
        if(!putknel)putknel=true;
        else 
        {
            putknel=false;
            linebreak=true;
        }
    }
    kar = input.get ( );
}

But this may be somewhat tedious.

If you use the regular expression it will be much easier.

Just replace all that match //[^\r\n]*\r\n to empty strings.