输入的时候出现问题

C++语言 码拜 9年前 (2015-05-11) 881次浏览 0个评论
 

题目::

统计每个元音字母在字符串中出现的次数。
 

Input
输入数据首先包括一个整数n,表示测试实例的个数,然后是n行长度不超过100的字符串。
 

Output
对于每个测试实例输出5行,格式如下:
a:num1
e:num2
i:num3
o:num4
u:num5
多个测试实例之间由一个空行隔开。

请特别注意:最后一块输出后面没有空行:)
 

Sample Input
2
aeiou
my name is ignatius
 

Sample Output
a:1
e:1
i:1
o:1
u:1

a:2
e:1
i:3
o:0
u:1

我的代码:

#include <iostream>
#include <cstring>

using namespace std;

int main()
{
char Sen[105];
int count[5];
int n;
int i, j;
int length;
cin >> n;
for (i = 0; i < n; i++)
{
cin.getline(Sen, 100, “”\n””);
length = strlen(Sen);
for (j = 0; j < 5; j++)
{
count[j] = 0;
}
for (j = 0; j < length; j++)
{
if (Sen[j] == “”a”” || Sen[j] == “”A””) count[0]++;
if (Sen[j] == “”e”” || Sen[j] == “”E””) count[1]++;
if (Sen[j] == “”i”” || Sen[j] == “”I””) count[2]++;
if (Sen[j] == “”o”” || Sen[j] == “”O””) count[3]++;
if (Sen[j] == “”u”” || Sen[j] == “”U””) count[4]++;
}
cout << “a:” << count[0] << endl;
cout << “e:” << count[1] << endl;
cout << “i:” << count[2] << endl;
cout << “o:” << count[3] << endl;
cout << “u:” << count[4] << endl;
if (i != (n – 1)) cout << endl << endl;
}
return 0;
}

为什么会跳过第一次的输入??

6分
加一句:

cin >> n;
fflush(stdin);
这是什么意思,是要刷新流吗??
8分
输入n的值时 换行符被cin.getline(Sen, 100, “”\n””);获取    在 cin >> n; 后加一句 cin.get();    (个人意见,有错的地方请原谅)
6分
cin.clear();
cin.sync();

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明输入的时候出现问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!