1

I'm working on a homework assignment to evaluate an expression from a file, but for some reason when I use getline(filename, string variable), the string has an extra enter. I know because when I outputted the string it came with a newline. And when I check the length() of the string, it's always one more than the actual string size, but I can't get rid of the extra space at the end. The last line in the input file works just fine though which I'm assuming is because it doesn't have an enter at the end of it. The evaluate function just evaluates the string. The place with the problem is in my error checking function where detecting errors depends on the length of the string. Thus, it prints out "error" because it doesn't work with the incorrect string length which is one more than the actual length.

ifstream input("input31.txt");
    string line;
    if (input.eof()) {
        cout << "is empty";
        input.close();
    }
    else {


        string line;
        while (!input.eof()) {
            getline(input, line);
            cout << line.length();
            }
        }
        input.close();
    }

I have tried detecting if there is a '\n' in the string, but it doesn't detect that for some reason. Forgive me if I have made a dumb mistake with this as I usually do. I am still new to coding, and it helps me to have extra eyes on it.

edit: Here is the input I'm using.

0*00000000000000000+0000000000000001
(1+2)*(1000+2000)
(+1+2)*(1000+2000)
((1+2)*(1000+2000))*(1+10000)
(1000000000000000-1)
99999999999999999/99999999999999999
(-100000+10000)*8
((-1))-(-1)

edit: I'm also using xcode on Mac if that makes any difference.

  • 1
    In case the input file was written on a different OS than the program is compiled and run, make sure that you change the line endings in the file to the format used by the OS running the program. See [this question](https://stackoverflow.com/questions/6089231/getting-std-ifstream-to-handle-lf-cr-and-crlf). – walnut Oct 23 '19 at 17:08
  • Just so I'm clear, all of the rest of the code inside `if(line != "")` is therefore unnecessary to reproduce your problem ? And fyi, [this is wrong: `while (!input.eof()) `](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – WhozCraig Oct 23 '19 at 17:11
  • getline does not append the new line to the string. You could use a debugger and take a closer look at the string. Maybe some other invisible character at the end of the string? – Lukas-T Oct 23 '19 at 17:14
  • 2
    @churill It will append `\r` if the file uses windows line endings and the program runs on a unix-like system, because the `\r` is not part of the newline there. This also explains why OP doesn't find it by searching for `\n`. – walnut Oct 23 '19 at 17:15
  • oh that would make sense since our instructor requires our program to be compatible with linux :/ –  Oct 23 '19 at 17:18
  • the '\r' worked tysm! –  Oct 23 '19 at 17:24
  • @uneven_mark -- `getline` does **not** append `\r` in the circumstances you describe. That character is in the source file, and `getline` does not remove it, just as it does not remove any `A` characters in the file. – Pete Becker Oct 23 '19 at 17:41
  • @PeteBecker Yes, I should have said "*leaves in*" instead of "*append*". I hope that makes the explanation better? – walnut Oct 23 '19 at 17:43
  • @uneven_mark -- yes, with that change, it's a good explanation. – Pete Becker Oct 23 '19 at 17:45

0 Answers0