我创建了一个程序,可以计算提供的字符串中的元音数量。它会正确计数元音,并在用户提供“ y”或“ Y”时重复进行。但是,当它重复时,它会自动为我尝试使用的C字符串分配“”。
int main()
{
//Creating repeating decision
char answer = 'y';
while ((answer == 'y') || (answer == 'Y'))
{
//declaring our C-String
char ourString[81] = "Default";
//Prompting user for a string, storing it as a C-String
std::cout << "Please enter a string!(Less than 80 characters please.)\n";
std::cin.getline(ourString, 81);
//Using a loop to count the amount of vowels
int ourNum = 0;
int vowels = 0;
while (ourString[ourNum] != '\0')
{
switch (ourString[ourNum]) {
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
case 'y':
case 'Y':
vowels++;
ourNum++;
break;
default:
ourNum++;
break;
}
}
std::cout << "The numbers of vowels in: \"" << ourString << "\" is " << vowels << "!\n";
std::cout << "Do again? Please enter \"Y\" to repeat, or any other character to escape.";
std::cin >> answer;
}
}
任何方向将不胜感激。谢谢!
答案 0 :(得分:6)
在写入“ y”并按下回车按钮之后,“ Y”和fig, ax = plt.subplots(nrows=2, ncols=4, figsize=(20,10))
fig.delaxes(ax[1,3]) #7 days in a week, remove 8th panel
ax = ax.flatten() #Far easier to work with a flattened array
lsize=8
plt.subplots_adjust(wspace=0.05, hspace=0.15) #Remove some whitespace between subplots
for idx, gp in df.groupby(df.Date.dt.dayofweek):
ax[idx].set_title(gp.Date.dt.day_name().iloc[0]) #Set title to the weekday
(gp.groupby(gp.Date.dt.hour).size().rename_axis([None]).to_frame()
.reindex(np.arange(0,24,1)).fillna(0)
.plot(kind='bar', ax=ax[idx], rot=0, ec='k', legend=False))
# Titles on correct panels
if idx%4 == 0:
_ = ax[idx].set_ylabel('Counts', fontsize=11)
if (idx//4 == 1) | (idx%4 == 3):
_ = ax[idx].set_xlabel('Tweet Hour', fontsize=11)
# Ticks on correct panels
_ = ax[idx].tick_params(axis='both', which='major', labelsize=lsize,
labelbottom=(idx//4 == 1) | (idx%4 == 3),
bottom=(idx//4 == 1) | (idx%4 == 3),
labelleft=(idx%4 == 0),
left=(idx%4 == 0))
# Consistent bounds between subplots.
lb, ub = list(zip(*[axis.get_ylim() for axis in ax]))
for axis in ax:
axis.set_ylim(min(lb), max(ub))
plt.show()
都存储在输入缓冲区中,因此“ y”进入答案字符,并且认为“ / n”下一条getline的输入。
有一些解决方案。您可以在"/n"
之后向cin.ignore()
添加呼叫。或者,您可以将yes设为字符串,然后在其中使用cin >> yes
而不是getline
。