如果cin>>ch,那字串后面的空格怎么自動去掉啊?
#include <iostream>
#include <string>
#include <cctype>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
const int SIZE = 10;
int main(int argc, char** argv) {
cout << "Enter words (q to quit):"<<endl;
string ch;
int vow = 0;//元音單詞個數
int cons = 0;//輔音單詞個數
int others = 0;
cin >> ch;
while(ch!="q")
{
ch = tolower(ch[0]);
if(isalpha(ch[0]))
{
if(ch[0]=='a'||ch[0]=='e'||ch[0]=='o'||ch[0]=='i'||ch[0]=='u')
++vow;
else
++cons;
}
else
++others;
//cin.get();
cin >> ch;
}
cout << vow << " words beginning with vowels." <<endl;
cout << cons << " words beginning with consonants."<<endl;
cout << others << " others."<<endl;
return 0;
}
uj5u.com熱心網友回復:
cin會忽略分隔符(空格 換行 制表符)uj5u.com熱心網友回復:
不是cin哦,是流運算子
uj5u.com熱心網友回復:
那忽略的話不久一段話都到string中了,不是只有一個單詞被賦進去嘛
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/98893.html
標籤:C++ 語言
上一篇:迷宮問題
下一篇:每一步都是什么意思呀
