在用户输入数组中查找元音 //我是C++的新手,我不理解所有的语法规则,以及指针之类的东西是如何运行的。我想知道如何制作一个程序来查找输入数组中的元音,请不要提任何建议。我知道我可能很难做到这一点,但我想知道为什么和如何做的事情更好 #include<iostream> using namespace std; int main() { int sum = 0; int n; char vowels[6] = {'a', 'e', 'i', 'o', 'u', 'y'}; char *word = NULL; cout << "Enter word" << endl; cin >> n; for(int i=0; i < n; i++) { for(int j=0; j < 6; j++) if(word[i] == vowels[j]) { sum++; } cout << sum; } delete [] word; word = NULL; return 0; } #包括 使用名称空间std; int main() { 整数和=0; int n; 字符元音[6]={'a','e','i','o','u','y'}; char*word=NULL; cout n; 对于(int i=0;iword不接受单词作为输入,它只接受一个数字。这是因为word属于int类型
您需要将输入值设置到word中,并在每个字母后打印总和,因为您是在通过word的循环中打印的。代码中有两个错误: 首先,在用户输入数组中查找元音 //我是C++的新手,我不理解所有的语法规则,以及指针之类的东西是如何运行的。我想知道如何制作一个程序来查找输入数组中的元音,请不要提任何建议。我知道我可能很难做到这一点,但我想知道为什么和如何做的事情更好 #include<iostream> using namespace std; int main() { int sum = 0; int n; char vowels[6] = {'a', 'e', 'i', 'o', 'u', 'y'}; char *word = NULL; cout << "Enter word" << endl; cin >> n; for(int i=0; i < n; i++) { for(int j=0; j < 6; j++) if(word[i] == vowels[j]) { sum++; } cout << sum; } delete [] word; word = NULL; return 0; } #包括 使用名称空间std; int main() { 整数和=0; int n; 字符元音[6]={'a','e','i','o','u','y'}; char*word=NULL; cout n; 对于(int i=0;iword不接受单词作为输入,它只接受一个数字。这是因为word属于int类型,c++,arrays,C++,Arrays,您需要将输入值设置到word中,并在每个字母后打印总和,因为您是在通过word的循环中打印的。代码中有两个错误: 首先,cin>>word不接受单词作为输入,它只接受一个数字。这是因为word属于int类型 此外,您需要在for循环之外打印sum,这样它就不会连续打印。您需要注意程序段中的一些事情 您需要创建一个字符数组(例如word[20])来读取字符串 您需要将行读入char数组,而不是int(cin>>n;) 使用“cin”无法读取字符串 您需要在循环外打印总和(元音数) #include
cin>>word
不接受单词作为输入,它只接受一个数字。这是因为word
属于int
类型
此外,您需要在for循环之外打印
sum
,这样它就不会连续打印。您需要注意程序段中的一些事情
cin>>n;
)#include<iostream>
#include<string.h>
using namespace std;
int main()
{
int sum = 0;
int n;
char vowels[6] = {'a', 'e', 'i', 'o', 'u', 'y'};
char word[20] = NULL;
cout << "Enter word" << endl;
while (getline(cin, word)) //read line of text including white space until enter key is pressed
{
}
n=strlen(word); //get the length of the input string
for(int i=0; i < n; i++)
{
for(int j=0; j < 6; j++)
if(word[i] == vowels[j])
{
sum++;
}
}
cout << sum; //Print total number of vowles
delete [] word;
word = NULL;
return 0;
}
#包括
#包括
使用名称空间std;
int main()
{
整数和=0;
int n;
字符元音[6]={'a','e','i','o','u','y'};
字符字[20]=空;
难道你忘了输入单词(cin>>n
不能输入)。你想要一个总计数还是每个元音的计数?你能给出一个示例输入和示例输出吗?