【问题标题】:Error: cannot convert 'char*' to 'char**' for argument '1' to 'int upper(char**)'错误:无法将参数 '1' 的 'char*' 转换为 'char**' 到 'int upper(char**)'
【发布时间】:2015-08-05 08:41:48
【问题描述】:

我有一个任务来计算数组中元音、大写字母、辅音等的数量。但我不断得到 错误:

错误不能将参数 '1' 的 'char*' 转换为 'char**' 'int 上(char**)'

我不知道为什么会出现这个错误。

我的主程序:

#include "stringCount.h"
using namespace std;

int main()
{
    const int SIZE = 1024;  // The maximum size of the C-string
    char cstring[SIZE];     
    char choice;        

    cout << "Enter a C-string of at most 1024 characters: ";
    cin.getline(cstring, SIZE); //Get a c-string

    // Display the Menu

   do
   {
      cout << "\tA) Count the number of vowels in the string\n";
      cout << "\tB) Count the number of consonants in the string\n";
      cout << "\tC) Count the number of uppercase alphabets in the string\n";
      cout << "\tD) Count the number of lowercase alphabets in the string\n";
      cout << "\tE) Count the number of alphabets in the string\n";
      cout << "\tF) Count the number of digits in the string\n";
      cout << "\tG) Find the average number character in each word of the string\n";
      cout << "\tH) Display the string with first letter of words capitalized\n";
      cout << "\tI) Enter a new string string\n";
      cout << "\tQ) Quit this program\n\n";

      cout << "\tEnter your choice (A - I) or Q: ";
      cin >> choice;

      while ((toupper(choice) < 'A' || toupper(choice) > 'I') && toupper(choice)!='Q')
      {
         cout << "\tEnter ONLY (A - I) or Q: ";
         cin >> choice;
      }

        // Process User's choice
      switch (toupper(choice))
      {
         case 'A':   cout << "The string has " << vowel(cstring) << " vowels." << endl;
                     break;
         case 'B':   cout << "The string has " << consonant(cstring) << " consonants." << endl;
                     break;
        case 'C':   cout << "There are " << upper(cstring) << " uppercase alphabets in the string." << endl;
                     break;
        case 'D':   cout << "There are " << lower(cstring) << " lowercase alphabets in the string." << endl;
                     break;
        case 'E':   cout << "There are " << alphabet(cstring) << " alphabets in the string." << endl;
                     break;
        case 'F':   cout << "There are " << digit(cstring) << " digits in the string." << endl;
                     break;
            case 'G':   cout << "There average number of letters per word in the string is " << wordCount(cstring) << "." << endl;
                     break;
            case 'H':   cout << "The capitalized string is: "  << endl;
                            capital(cstring);
                            cout << cstring << endl;
                     break;                     
         case 'I':   cin.get();
                     cout << "Enter a C-string of at most 1024 characters: ";
                     cin.getline(cstring, SIZE);
                     break; 
            case 'Q':       cout << "Goodbye!\n";
                     exit(EXIT_SUCCESS);
      }
   } while (toupper(choice) != 'Q');

    return 0;}

我的头文件:

    #ifndef STRING_COUNT
    #define STRING_COUNT

    #include <iostream>
    #include <cstdlib>

    double wordCount(char *[]);
    void capital(char *[]);
    int vowel(char *[]);
    int consonant(char *[]);
    int upper(char *[]);
    int lower(char *[]);
    int alphabet(char *[]);
    int digit(char *[]);

    #endif

我的实现文件:

#include <iostream>
#include <cstdlib>
#include <cstring>
#include "stringCount.h"

double wordCount(char*words)
{
    int a, size, word=0 ;

    size = sizeof (words);

    for (a=0 ; a < size ; a++)
    {
         if (isspace(words[a]))
         {
            word++;
         }
    }
    return word+1;
}
//======================================================================
void capital(char * words)
{
    int i,  size ;

    size = sizeof (words);


    for (i=0 ; i < size ; i++)
    {
        if (isspace(words[i])&& isalpha(words[i+1]) )
        {
            words[i+1] = toupper(words[i+1]);
            i++;
        }
    }
}
//=====================================================================
int vowel(char * words)
{
    int a =0;
    int size, vowels=0 ;
    size =  sizeof(words);   

    for (a=0; a< size;  a++)

    {
        if( words[a]== 'a'|| words[a] == 'e' || words[a]== 'i' || words[a] == 'o' || words[a] == 'u' ||words[a]== 'A'|| words[a] == 'E' || words[a]== 'I' || words[a] == 'O' || words[a] == 'U')
        {
            vowels++;
        } 
    }
    return vowels;
}
//=====================================================================
int consonant(char * words)
{
    int i,  size,  cons =0;  
    size = sizeof(words);

      for (i = 0; i< size ; i++)
      {
        if (isalpha(words[i])&!( words[i]== 'a'|| words[i] == 'e' || words[i]== 'i' || words[i] == 'o' || words[i] == 'u' ||words[i]== 'A'|| words[i] == 'E' || words[i]== 'I' || words[i] == 'O' || words[i] == 'U'))
        {
            cons++ ;
        }

      } ;


     return cons; 
}
//====================================================================
int upper(char * words)
{

     int i,  size,  uppercase =0 ;

     size = sizeof(words);
     for (i = 0 ; i< size ; i++)
     {
        if (isupper(words[i]))
        {
            uppercase++;
        }
     }

     return uppercase;

}
//===============================================================
int lower(char * words)
{

     int i,  size,  lowercase =0 ;

     size = sizeof(words);
     for (i = 0 ; i< size ; i++)
     {
        if (islower(words[i]))
        {
            lowercase++;
        }
     }

     return lowercase;

}
//================================================================
int alphabet(char * words)
{
    int alphab =0;

    int i,  size;    
    size = sizeof(words);

      for (i = 0; i< size ; i++)
      {
        if (isalpha(words[i]))
        {
            alphab++ ;
        }

      }
    return alphab;
}
//=================================================================
int digit(char * words)
{
    int a,  size,  digi =0;
    size = sizeof(words);

    for (a=0 ; a < size ; a++)
    {
        if(isdigit(words[a]))
        {
            digi ++;
        }
    }
    return digi ;   
}
//=====================================================================

【问题讨论】:

  • 请将代码量减少到能够重现您的问题的最少示例。在这种情况下,不应超过 ~10 行。另外请解释一下您从错误消息中到底不明白的地方。
  • double wordCount(char*words) 与声明的签名不同:double wordCount(char *[]);
  • 这里的人喜欢提供帮助,是的,但你不能只是把你的整个程序放在这里然后说“请告诉我为什么它不起作用”。您能否至少给出导致错误的行号?并且还尝试使用那一行构建一个最小的示例。你现在所做的只是对社区的不尊重,对不起。

标签: c++ pointers char


【解决方案1】:

更改函数声明

int upper(char *[]);

int upper(char *);

甚至是

int upper( const char * );

考虑到函数定义也是错误的

代替

 size = sizeof(words);

你必须使用

 size = strlen(words);

同样适用于其他函数定义。

函数可以这样定义

int upper( const char * words )
{
    int uppercase = 0 ;

    for ( const char *p = words; *p != '\0'; ++p )
    {
        if ( isupper( ( unsigned char )*p ) ) ++uppercase;
    }

    return uppercase;
}

【讨论】:

  • 如果更改为int upper(const char * words) {,还需要更新原型以包含const
  • 谢谢你们,你们不知道这对我来说意味着什么,在过去的一周里,出于沮丧,我一直在拔头发。
猜你喜欢
  • 1970-01-01
  • 2014-03-02
  • 2017-08-02
  • 1970-01-01
  • 2018-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多