I’m trying to see if the end of the string being read is a ". If it isn’t, I want it to print out something.
if(!line.find_last_of("\"")) {
cout << "Extra parameter is typed.";
continue;
I was trying to use find_last_of but when I run it extra parameter is printed no matter if it the command has extra parameters. Example:
lc "file.txt" -suppose to true so it's suppose to continue program but returns false
lc "file.txt" lk - suppose to return false and it does but should only return false for this type of case.
Although I think @Jonathon Seng’s answer is good (and have up-voted it), I think there’s another possibility that might be worth mentioning. Instead of
mystring.at(mystring.length()-1), you could use*mystring.rbegin():Of course, you still have to check that the string isn’t empty as well. For this, I’d normally prefer
!line.empty()overline.length() > 0, so the final version becomes:Edit: note that the test for
!line.empty()must be first.&&evaluates its left operand, and then if (and only if) that evaluates totrue, evaluates its right operand. We need to verify that the line isn’t empty first, then check the character only if the string isn’t non-empty.