C++ 使用指针传递C字符串参数
我整天都在忙这个节目。我终于觉得我真的很接近了。我必须找出字符串中元音和字符的数量。然后在最后输出它们。然而,当我编译我的程序时崩溃了。我一整天都在检查语法和我的书。如果有人能帮忙,我会非常感激!因为我还要编写5个类似的函数来处理c字符串。谢谢C++ 使用指针传递C字符串参数,c++,function,pointers,arguments,c-strings,C++,Function,Pointers,Arguments,C Strings,我整天都在忙这个节目。我终于觉得我真的很接近了。我必须找出字符串中元音和字符的数量。然后在最后输出它们。然而,当我编译我的程序时崩溃了。我一整天都在检查语法和我的书。如果有人能帮忙,我会非常感激!因为我还要编写5个类似的函数来处理c字符串。谢谢 #include <iostream> #include <string> using namespace std; int specialCounter(char *, int &); int main() {
#include <iostream>
#include <string>
using namespace std;
int specialCounter(char *, int &);
int main()
{
const int SIZE = 51; //Array size
char userString[SIZE]; // To hold the string
char letter;
int numCons;
// Get the user's input string
cout << "First, Please enter a string (up to 50 characters): " << endl;
cin.getline(userString, SIZE);
// Display output
cout << "The number of vowels found is " << specialCounter(userString, numCons) << "." << endl;
cout << "The number of consonants found is " << numCons << "." << endl;
}
int specialCounter(char *strPtr, int &cons)
{
int vowels = 0;
cons = 0;
while (*strPtr != '/0')
{
if (*strPtr == 'a' || 'A' || 'e' || 'E' || 'i' || 'I' || 'o' || 'O' || 'u' || 'U')
{
vowels++; // if vowel is found, increment vowel counter
// go to the next character in the string
}
else
{
cons++; // if consonant is found, increment consonant counter
// go to the next character in the string
}
strPtr++;
}
return vowels;
}
#包括
#包括
使用名称空间std;
int特殊计数器(字符*,int&);
int main()
{
常量int SIZE=51;//数组大小
char userString[SIZE];//保存字符串
字符字母;
国际货币基金组织;
//获取用户的输入字符串
cout我将假设您仅限于不使用std::string
或std::getline
,并且您必须假设用户输入的字符少于51个
您的崩溃源于:
while (*strPtr != '/0')
空字符是转义码。“/0”
是具有实现定义值的多字符文字。这意味着它可能始终为真。将其更改为:
while (*strPtr != '\0') //or while (strPtr)
除此之外,您的元音检查有一个逻辑错误。您必须对照每个元音进行检查,如下所示:
if (*strPtr == 'a' || *strPtr == 'e') //etc.
如果与每个字符的touper
或tolower
版本进行比较,以将比较次数减少2倍,您会发现比较更容易
while (*strPtr != '/0')
应该是:
while (*strPtr != 0)
或
你的编译器没有给你警告吗?如果是,不要忽略警告。如果不是,找一个更好的编译器
另请参阅其他比较中有关错误的注释。其他答案应该可以解决您的问题,我是否可以建议您编写单独的函数,而不是一个万能函数
bool IsSpecialChar(char c)
{
switch(c)
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
return true;
}
return false;
}
int specialCounter(char *strPtr, int &cons)
{
int vowels = 0;
cons = 0;
while (*strPtr != '\0')
{
IsSpecialChar(*strPtr) ? vowels++ : cons++;
strPtr++;
}
return vowels;
}
*strprtr='a'| |'a'|'e'|'e'|'i'|'i'|'o'|'o'|'u'|'u'
将始终为真,因为'a'
不是0。我不确定我是否理解,因为在我检查空终止符'/0'的时候。你是否在寻找任何特定的表达式*strprtr='A'| |*strprtr='e'…
。输入完所有内容后,扔掉它,将所有测试字符放在一个字符串常量中(常量字符元音[]=“aaeeiiouu”
),并将表达式更改为if(strchr(元音,*strprtr))
Ohhh。好吧,我不应该使用or | |,因为它搜索所有元音?就像为每个字符使用一个switch语句一样?@user2063325,我个人会使用std::count_,如果
,但它可能是不允许的。请参阅我的答案,以获得针对多个条件进行比较的正确方法。是的,它没有给我一个警告。:/我不相信我把刀子划错了方向。非常感谢!
bool IsSpecialChar(char c)
{
switch(c)
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
return true;
}
return false;
}
int specialCounter(char *strPtr, int &cons)
{
int vowels = 0;
cons = 0;
while (*strPtr != '\0')
{
IsSpecialChar(*strPtr) ? vowels++ : cons++;
strPtr++;
}
return vowels;
}