C 循环不重新分配字符串值
C++ While loop not re-assigning string value
我创建了一个程序,该程序计算提供的字符串中的元音数量。它正确计数元音并在用户提供" 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] != ' ')
{
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;
}
}
任何方向都将不胜感激。谢谢!
写作" y y"之后并按下" y y"的输入按钮。"/n"
存储在输入缓冲区中,因此" y y"转到答案char,并"/n n&quot"被认为是下一个Getline的输入。有一些解决方案。cin >> yes
之后,您可以将呼叫添加到cin.ignore()
。或者,您可以制作一个字符串,然后使用getline
而不是在那里的operator>>
。
相关文章:
- 用字符串的向量分配字符串向量
- 在C++中动态分配字符串数组
- 分配字符串uint8_t
- 如何分配字符串来提升野兽multi_buffer
- 分配字符串时EXCEPTION_ACCESS_VIOLATION
- 使用带有动态分配字符串的strcpy_s()和strcat_s()
- 如何分配字符串文本
- C 循环不重新分配字符串值
- 如何在C 中分配字符串数组
- 动态内存分配 C++ 字符串
- 如何存储可以分配字符串的数值
- 通过引用传递结构并分配字符串
- 动态分配字符串文字
- 分配字符串数组时出现分段错误
- 如何从 Arduino 的 C++ 类中的构造函数中分配字符串字段
- 动态分配 C 字符串内存,无需先询问用户输入时间
- 分配字符串时出现 char* 新建和删除 [] 错误
- C++正在记住指向已分配字符串的指针
- 在结构中分配字符串时出现 Seg 错误
- 指向字符串的指针,可用于动态分配字符串数组