这个问题看起来类似于其他问题(我已经检查过。)但是没有问题提出了我正在寻找的答案。
我正在使用C ++编写程序来检查字符串中的字符是元音还是辅音,并使用计数器循环迭代字符串以产生结果。 isalpha()在一个名为'isVowel'的bool函数中,它只做两件事。 1它检查检查的值是否确实是合法的字母字符。 2它检查字符是否为元音,如果为true,则返回true为main。我需要指出为什么我的cout'是元音'在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;
}
答案 0 :(得分:0)
您应该使用如下表达式更改第36-40行:cout << letter << ((answer != false) ? " is a vowel\n" : "\n");