利用getline函数的两种用法cin.getline()输入流在外面getline()输入流作为参数保存在里面第一种方法:利用c++primer里面提供的,使用基于范围for语句。#include<iostream>#include<string>using namespace std;int main() {string s; int a = 0, b = 0, cd = 0, d = 0, e = 0;getline(cin, s);for (auto c:s) {switch (c) {case '\0':break;case 'a':++a; break;case 'e':++b; break;case 'i':++cd; break;case 'u':++d; break;case 'o':++e; break;}}cout << a << " " << b << " " << cd << " " << e << " " << d << endl;return 0;}
第二种利用数组,简单的for循环
#include<iostream>
#include<string>
using namespace std;
int main(){
char s[80]; int a=0,b=0,c=0,d=0,e=0;
cin.getline(s,80);
for(int i=0;i<80;++i){
switch(s[i]){
case '\0':
break;
case 'a' :
++a;break;
case 'e' :
++b;break;
case 'i' :
++c;break;
case 'u':
++d;break;
case 'o':
++e;break;
}
}
cout << a << " "<<b <<" "<<c<<" " <<e<<" "<<d<<endl;
return 0;
}