Checking if a char is a newline
c++
Solution
By default, formatted input from streams skips leading whitespace. You need to either disable skipping of leading whitespaces or use one of the functions which won't skip spaces:
std::cin >> std::noskipws; // disables skipping of leading whitespace
char c;
while (std::cin.get(c)) { // doesn't skip whitespace anyway
...
}
Problem
I'm doing an exercise in C++ Primer and basically I am using a switch statement to count the number of vowels in a text that I input. I input the text using a while loop. ``` while(cin >> ch) ``` and proceed with the cases, a, e, i, o, u, incrementing an integer variable for the respective cases. Now the next part of the question says also count the spaces, tabs and newlines. I tried doing ``` case ' ': ``` and so forth using '\t' and '\n'. But it seems like it doesn't compute these cases. I also tried just using a default and using an if else statement ``` default: if(ch == ' ') ++space; ``` etc. But this doesn't proceed either. I also tried putting in the integer values of ' ', '\t', '\n'. What am I doing wrong here? Also, I know that if I use isspace() I can count the combined total but I need to compute each one individually. I'm not sure why the equality test won't do the job.