My program asks the user to enter a line of text ended with a period. It removes any spaces at the very beginning of the sentence. It then changes all the characters to lowercase except the very first letter in the sentence, which it capitalizes. The next part of my program is supposed to erase multiple consecutive whitespace characters leaving just one space between words.
This is the only usage of single and double quotes that doesn't give me any red squiggly underline warnings in my function remove_extra_blanks:
The function looks at each character in the string one by one to see if it is a space character, a new line character, or neither. Based on that it decides which characters to copy into the temp string.
I get red squiggly underline warnings under != and == when I use single quotations when comparing string characters for equality:
But if I use double quotations when assigning a space to an element of my temp string I get a red squiggly underline warning under the = in
temp.at(i) = ' '; :
Find/replace and regular expressions will work and they are easy to code, but find/replace is slow when the strings are big, and regular expressions will bloat your code by pulling in a large library.
The problem that you're having comes from confusing double-quotes and single-quotes. Single quotes contain chars. Double quotes contain c-strings. So at lines 60-63 you're trying to compare a string to a character.
Since next is always a single character, I'd code it that way by changing lines 49 and 53 to char last = 'z'; and char next = s[i];
Better yet, since next goes through all the characters in the string, you can use a range-based for loop by replacing lines 51-53 with for (char next : s) {
All the comparisons with next should use single quotes. E.g.: if (((next != ' ') && (next != '\n')) && ((last == ' ') || (last == '\n')))
temp.at(i) = ' '; won't work. at[] must refer to an existing character in the string. You want to add a new character. No worries, just use += instead: temp += ' ';
You could simplify the code somewhat by recognizing that you're dividing each character into two kinds: "spaces" and "not spaces." If you record the next character's kind and the last character's kind, the code gets easier.