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.