与基本面作斗争.特别是字符[],字符*和从数组中读取.也许我应该使用类型字符串
Struggling with the fundamentals. Especially char[], char* and reading from arrays. Perhaps I should use type string
我需要一些指导。
我想提高我的技能,所以我正在练习较小的项目。
当前的挑战是创建一个可以计算用户输入单词中的音节的函数。
我的想法是声明一个元音数组:a,e,i,o,u。
然后遍历用户输入的单词,检查单词中的任何字母是否与元音数组匹配,如果是,则检查下一个字母是否不匹配。(我假设音节是由元音和辅音的存在定义的。
这将需要嵌套的循环,一个循环遍历单词,然后另一个循环使用当前索引"word"遍历元音数组。
我什至还没有弄清楚我将如何对单词[i+1]做同样的事情。
但是我很挣扎,因为由于基本错误,我无法编译我的程序。我可能应该使用字符串类,但我不知道。
这是我所拥有的(它不会编译!
#include <iostream>
char vowels[] = {'a', 'e', 'i', 'o', 'u'};
int numberOfSyllables(char *word)
{
int numberOfVowelsFound = 0;
for ( &element : word )
{
bool vowelMatch = 0;
for ( &vowel : vowels)
{
if (element == vowel)
{
vowelMatch = 1;
break;
}
}
if ((vowelMatch == 1) numberOfVowelsFound++;
}
return numberOfVowelsFound;
}
int main()
{
char *userInput[50];
std::cout << "Enter a word: ";
std::cin >> *userInput;
std::cout << numberOfSyllables(userInput) << " syllables found";
return 0;
}
这不是一个代码审查网站,但无论如何我都会尽力而为:
- 您的
for
循环没有类型:
for (TYPE &element : word )
在这种情况下,要循环的类型是char
。
如果您希望编译器为您找出类型:
for (auto &element : word)
您正在使用"foreach"样式循环遍历
word
,但char *
不能以这种方式循环。准确地说,没有为char *
定义std::begin
和std::end
函数,所以"foreach"样式循环不知道您希望字符串在哪里开始/结束。要么使用不同样式的循环,要么使用支持"foreach"样式循环的类型(例如std::string
或 C++17 的std::string_view
)。您在
if
语句中添加了一个额外的括号(
:
// |
// v
if ((vowelMatch == 1) numberOfVowelsFound++;
您将
userInput
变量声明为"指向字符的 40 个指针数组",但您可能希望为其写入"字符",而不是"指向字符的指针"。将其类型更改为"40 个字符的数组"。类似地,您取消引用
userInput
变量(可能是为了避免警告),由于userInput
是一个"40 的数组(指向 char 的指针)",它将返回该数组中第一个未初始化的"指向 char 的指针"(在这种情况下*var
与var[0]
相同)。只需删除取消引用运算符*
并按照上述建议更改数组的类型,std::cin
就会弄清楚该怎么做。由于您(错误地)取消引用userInput
以避免警告,因此std::cin
认为您要写入未初始化指针指向的位置。此时,您也无法控制程序将写入的位置;它可能只是崩溃。最后,您再次将错误的类型传递到
numberOfSyllables(userInput)
中,如前所述,userInput
是一个"指向字符的 40 个指针数组",而您的函数需要"字符指针"。将userInput
类型更改为"字符数组",然后编译器可以将其转换为"字符指针"。
最终代码:
// compile with -std=c++17 for std::string_view, or use another loop style
#include <string_view>
#include <iostream>
char vowels[] = {'a', 'e', 'i', 'o', 'u'};
int numberOfSyllables(char *word)
{
int numberOfVowelsFound = 0;
// `std::string_view` can be used in "foreach" style loops
// we need to use `const char`, since `std::string_view` is a "fake string" and not writable
for (const char &element : std::string_view(word))
// Another loop style (This even works in C):
// for (int i=0; word[i] != ' '; i++) // While the current element is not NUL
// {
// const char element = word[i]; // Remember the current element
{
bool vowelMatch = 0;
for (const char &vowel : vowels) // Use const here too just for good measure
{
if (element == vowel)
{
vowelMatch = 1;
break;
}
}
if (vowelMatch == 1) numberOfVowelsFound++; // Removed a parenthesis here
}
return numberOfVowelsFound;
}
int main()
{
char userInput[50]; // Changed type of this variable
std::cout << "Enter a word: ";
std::cin >> userInput; // Removed a dereference here
std::cout << numberOfSyllables(userInput) << " syllables found";
return 0;
}
- C++字符*缓冲区的大小
- HEX值到wchar_t字符(UTF-8)的转换
- 为什么 Serial.println(<char[]>);返回随机字符?
- 我的字符计数代码计算错误.为什么
- 字符串-C++后显示的随机字符
- 将Integer转换为4字节的unsined字符矢量(按大端字节顺序)
- 如何在C++中从字符串中分割字符
- 为什么msgrcv()将垃圾字符馈送到缓冲区
- 指向指向字符数组的指针数组的指针
- 如何用转义符替换字符串中的所有特殊字符
- 为什么 sscanf 无法从一个字符串中读取uint64_t和字符?
- 比较字符数组
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 从矢量<无符号字符>转换为字符* 包括垃圾数据
- 如何使用Crypto++并为RSA返回可打印的字节/字符数组
- 如何在C++中确定文本文件中的元素是字符还是数字
- 如何将一个ostringstream十六进制字符串字符对转换为单个unit8t等价的二进制值
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- C++中无符号字符溢出
- 与基本面作斗争.特别是字符[],字符*和从数组中读取.也许我应该使用类型字符串