Extra line in output file

Hello,

I need help with my output file. It gives me an extra line whenever I run my program. I have provided my while loop for assistance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

    while(  !in_file.eof() )
    {
        getline( in_file, line );

        for( int i = 0; i < line.length(); i++ )
        {
          current_char = line[i];

          current_char = toupper( current_char );

            if( current_char >= 'A' && current_char <= 'Z' )
            {
                out_file << caesarShift( current_char, shift_value );
            }
            else
            {
                out_file << current_char;
            }
        }

        out_file << "\n";
¿don't you realize that you are checking the condition too late?
If you reach the end of file in line 4, then you process that faulty input too

while( getline(in_file, line) )
Change this:
1
2
3
   while(  !in_file.eof() )
    {
        getline( in_file, line );

to this:
1
2
    while ( getline(in_file, line) )
    {


In your current code, the getline() fails on the final iteration, but the rest of the loop is executed anyway. In the new version, the body of the loop is entered only when the geline() succeeds.
Thanks for your input. It worked and Thank you very much!
Last edited on
Topic archived. No new replies allowed.