如何防止字符在 for 循环中打印两次?C++
How do I prevent character from being printed twice inside for loop? c++
这个问题看起来与其他问题相似(我已经检查过了。但是,提出的任何问题都没有我正在寻找的答案。
我正在使用C++编写一个程序来检查字符串中的字符是元音还是辅音,并使用计数器循环迭代字符串以产生结果。 isalpha() 位于一个名为 'isVowel' 的布尔函数中,它只做 2 件事。 1 它检查检查的值是否确实是合法的字母字符。 2 它检查字符是否是元音, 如果为 true,则返回 true 到 main。我需要指出为什么我的 cout 'is 元音' 在 for 循环"if (answer == true)"语句中的原始字符串下方打印两次的方向。
#include <iomanip>
#include <iostream>
#include <string>
#include <ctype.h>
using namespace std;
bool isVowel(char word){
if(isalpha(word)){
if(word == 'a' || word == 'A')
{
return true;
}
}
return false;
}
int main ()
{
//Variables
string choice;
bool answer;
//program starts here
cout << setw(50) << "Vowels and Consonants" << endl;
cout << "Please enter a word: ";
cin >> choice;
//while loop gathering each letter of the word to test for vowels/consonants
/*while((unsigned)index < choice.length())*/
for (int index = 0;(unsigned) index < choice.length();){
char letter = choice[index];
answer = isVowel(choice[index]);
cout << letter << endl;
if(answer != false){
cout << letter << " is a vowel" << endl;
}
index++;
}
return 0;
}
您应该使用如下表达式更改第 36-40 行: cout << letter << ((answer != false) ? " is a voweln" : "n");
相关文章:
- g++的分段错误(在NaN上使用to_string两次时)
- 蛇在C++不会连续转两次
- 检查一个数组是否包含在另一个数组中,以相反的顺序,至少两次
- 从具有按值捕获的 lambda 移动构造 std::函数时,移动构造函数调用两次
- 我应该如何去缓解两次出现的cin?
- Realloc 两次无法在 Visual Studio 上运行
- 使用 getline(cin, var) 两次在进行字符串比较时会产生错误 (==)
- 为什么映射插入和 map.find() 的单次迭代比插入和 map.find() 的两次单独迭代慢得多
- C++析构函数调用两次,堆栈分配的复合对象
- 为什么参数在构造 std::thread 时移动两次
- Qt插槽调用了两次
- 做 std::用相同的unique_ptr移动两次
- C++两次定义相同的函数会导致错误
- 为什么具有静态存储持续时间的同一内联变量在包含在 VS2017 编译的两个翻译单元中时会构造和销毁两次
- 对于优化级别为 0 的 std::vector,析构函数被调用两次
- 使用柯南打包时如何避免列出两次依赖?
- 为什么要执行两次位移((x >> 4)<< 4)?
- 对结构向量进行两次排序
- 如果我使用同一个密钥推送用户数据两次,会发生什么
- 谷歌测试发现在macOSX上添加并运行了我的所有测试两次