未读取空格

Whitespaces are not being read

本文关键字:空格 读取      更新时间:2023-10-16
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
    int vovles_count[5] = { 0,0,0,0,0 };
    char vovwls[5] = { 'a','e','i','o','u' };
    char vov;
    int nonVov = 0 , punt = 0 , douseq = 0;
    //std::cin >> std::noskipws; //tells not to skip white spaces
    while (cin >> vov)
    {
        switch (vov)
        {
            case 'a':
                ++vovles_count[0];
                break;
            case 'e':
                ++vovles_count[1];
                break;
            case 'i':
                ++vovles_count[2];
                break;
            case 'o':
                ++vovles_count[3];
                break;
            case 'u':
                ++vovles_count[4];
                break;
            case ' ':
                ++punt;
                break;
            case 'n':
                ++punt;
                break;
            case 'f':
                cin >> vov;
                if (vov == 'l' || vov == 'f')
                {
                    ++douseq;
                    ++nonVov; //previous f
                    ++nonVov; //current f
                } 
                else if (vov == 'i')
                {
                    ++vovles_count[2];
                    ++douseq;
                    ++nonVov;   //previous f
                }
                else if (vov == ' ' || vov == 'n')
                    ++punt;
                break;
            default:
                ++nonVov;
                break;
        }
    }
    for (int i = 0; i < 5; ++i)
    {
        cout << vovwls[i] << " occured for " << vovles_count[i] << "times" << 'n';
    }
    cout << 'n' << nonVov << " are the occurence of non vovels " << 'n' ;
    cout << 'n' << punt << " are the occurence of whitespaces " << 'n';
    cout << 'n' << douseq << " are the occurence of double sequence " << 'n';

    system("pause");
    return 0;
  }

我认为我在代码中没有看到任何问题,但我不知道为什么没有读取空格?

使用 CIN 默认情况下会跳过空格。 您可以尝试其他方法:

cin.get((;

正是这个函数的代码重载将帮助您:

istream& get (char* s, streamsize n, char delim);

将 '' 放入 delim 以结束输入行,按回车键