0

I've opened a pre-existing file and am sending the data into a new file while trying to format it.

I'm trying to

  1. Make the first letter after the number uppercase
  2. create a new line when each number appears
  3. remove any extra spaces other than 1 right after the numbers.

If I don't include the break; I can't get out of the if-else statements so I know my logic is off somewhere. I'm not entirely sure where i'm getting hung up.

//input and output files have successfully opened
char next;
dirty_file.get(next);
clean_file << static_cast<char>(tolower(next));

while(!dirty_file.eof())
{
    if(isdigit(next))
    {
        clean_file << "\n" <<  " ";
        clean_file.put(toupper(next));
    }
    else
    {
        if(isalpha(next))
        {
            clean_file.put(toupper(next));
        }
        else if(isspace(next))
        {
            dirty_file.putback(next);
        }
        else
        {
            clean_file.put(next);
        }
        break;
    }

    dirty_file.get(next);
}
return;
Jawa
  • 2,336
  • 6
  • 34
  • 39
ColinO
  • 59
  • 1
  • 6
  • 1st of all: [`while(!dirty_file.eof())`](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong)! – πάντα ῥεῖ Apr 04 '15 at 15:59
  • Generally, don't do e.g. `while (!dirty_file.eof())`, reason being that the `eofbit` flag isn't set until you read beyond the end of the file, as well as it won't detect other errors. Instead in your case do `while (dirty_file.get(next))`. – Some programmer dude Apr 04 '15 at 16:01
  • although in this case it works as OP is reading at the end of the while loop. still, bad practice imo – vsoftco Apr 04 '15 at 16:01
  • Also you need to think again about the logic of the code, especially when you have a whitespace in the input file. If you put the space back into the buffer, what do you think the next character to be read is? The exact same space you just put back, leading to an infinite loop where you read a space, put it back, read the space again and so on. I suggest you read e.g. [this `putback` reference](http://en.cppreference.com/w/cpp/io/basic_istream/putback). – Some programmer dude Apr 04 '15 at 16:03
  • yea, thats what i'm running into after making the dirty_file.get(next) change – ColinO Apr 04 '15 at 16:04
  • If you're not writing the spaces to the destination file, just ignore them, and simply do nothing. – Some programmer dude Apr 04 '15 at 16:49

0 Answers0