Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
379 views
in Technique[技术] by (71.8m points)

c++ - Error: cannot convert 'char*' to 'char**' for argument '1' to 'int upper(char**)'

I have an assignment to calculate the number of vowels,capital letters, consonants etc. in an array. But I keep getting the error:

Error cannot convert 'char*' to 'char** ' for argument '1' to 'int upper(char**)'

I have no idea why I get this error.

My main program:

#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 << "	A) Count the number of vowels in the string
";
      cout << "	B) Count the number of consonants in the string
";
      cout << "	C) Count the number of uppercase alphabets in the string
";
      cout << "	D) Count the number of lowercase alphabets in the string
";
      cout << "	E) Count the number of alphabets in the string
";
      cout << "	F) Count the number of digits in the string
";
      cout << "	G) Find the average number character in each word of the string
";
      cout << "	H) Display the string with first letter of words capitalized
";
      cout << "	I) Enter a new string string
";
      cout << "	Q) Quit this program

";

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

      while ((toupper(choice) < 'A' || toupper(choice) > 'I') && toupper(choice)!='Q')
      {
         cout << "	Enter 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!
";
                     exit(EXIT_SUCCESS);
      }
   } while (toupper(choice) != 'Q');

    return 0;}

My header file:

    #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

My implementation file:

#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 ;   
}
//=====================================================================
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Change function declaration

int upper(char *[]);

to

int upper(char *);

Or even to

int upper( const char * );

Take into account that the function definition is also wrong

Instead of

 size = sizeof(words);

you have to use

 size = strlen(words);

The same is valid for other function definitions.

The function could be defined the following way

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

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

    return uppercase;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...