vendredi 12 février 2021

C++ count white spaces inside the switch statement

I'm doing this C++ Primer Fifth Edition Exercise 5.11. Even though I'm reading input through std::noskipws the blank spaces are not being counted. The tabs and newlines are OK. What am I missing? Thanks!

#include <iostream>
#include "Sales_item.h"
#include "Sales_data.h"
#include <string>
#include <cstdio>
#include <vector>
using namespace std;

    int main() {
    
      unsigned aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0;
      unsigned space = 0, tabs = 0, newline = 0;
      char ch;
      while(cin>>std::noskipws>>ch) switch (ch) {
              
                     case 'a':
                     case 'A':
                          ++aCnt;
                          break;
                     case 'e':
                     case 'E':
                          ++eCnt;
                          break;
                     case 'i':
                     case 'I':
                          ++iCnt;
                          break;
                     case 'o':
                     case 'O':
                          ++oCnt;
                          break;
                     case 'u':
                     case 'U':
                          ++uCnt;
                          break;
                     case '\n':
                          ++newline;
                          break;
                     case '\t':
                          ++tabs;
                          break;
                     case ' ':
                          ++space;
                          break;
 
                     }
                
      cout << "Number of vowel a: \t" << aCnt << '\n';
      cout << "Number of vowel e: \t" << eCnt << '\n';
      cout << "Number of vowel i: \t" << iCnt << '\n';
      cout << "Number of vowel o: \t" << oCnt << '\n';
      cout << "Number of vowel u: \t" << uCnt << '\n';
      cout << "Number of blank spaces: \t" << space << '\n';
      cout << "Number of tabs : \t" << tabs << '\n';
      cout << "Number of newlines : \t" << newline << '\n';
 

system("pause"); return 0;}

Aucun commentaire:

Enregistrer un commentaire