Passing a string to a class

This is a discussion on Passing a string to a class within the C++ Programming forums, part of the General Programming Boards category; Hello all, OK, here's the deal. I have this assignment where I am trying to implement a class method. In ...

  1. #1
    Registered User
    Join Date
    May 2004

    Passing a string to a class

    Hello all,

    OK, here's the deal. I have this assignment where I am trying to implement a class method. In it I am trying to calculate the total of uppercase letters in a string. My problem is that I can't grasp how to pass the string to the accessor function. At the moment my program will not even compile giving me this error;

    71 C:\C++Course\A1-1.cpp invalid conversion from `char*' to `int'
    Here is my code as it stands now;

    #include <iostream>
    #include <cctype>    // Not sure if this is really needed here 
    #include <string>
    using namespace std;
    class Word           // Declaring the class object
        public:          // public section
        int GetNumberOfUppercase();                       // accessor function
        void SetNumberOfUppercase(int UppercTotal);       // accessor function
        private:                                          // begin private section  
        int UppercaseTotal;                               // member variable
              // GetNumberOfUppercase, public accessor function
              // return number of uppercase letters
        int Word::GetNumberOfUppercase()
            return UppercaseTotal;
        // Definition of SetNumberOfUppercase, public
        // accessor function
        // return sets UppercaseTotal member
        void Word::SetNumberOfUppercase(int UppercTotal)
              char WordArray[25];  
              int i,UpperTotal;
          int size = strlen(WordArray);
          for(int i=0; i<size; i++)
            { if (isupper(WordArray[i]))
         UppercaseTotal = UpperTotal;  
    // begin main processing
    int main()
        char line[25];
      // Want to pass this value to SetNumberOfUppercase  ?????   
     // -----------------------------------------------------------------------
        cout<<"Enter a string 25 Char. long Max : ";
        cin >> line;
        Word Input;   
    // Compile error here 
        cout<< "The word you entered " << line << " has " << endl; 
        cout<< Input.GetNumberOfUppercase() << " Uppercase Letters" << endl;
      // Pause to look at the results
         return 0;
    } // end main
    Now I've been reading a lot but somehow can't find exactly what I'm looking for. My instructor suggested that I declare a char* input. Initialize it with say input= new char[25]; but I am not sure exactly how to go about it and my instructor won't be available for several more days and I would really like to go forward with this. Can someone point me to a link or explain how to proceed with a simple example?

    Thanks in advance,


  2. #2
    Registered User
    Join Date
    Jan 2005
    SetNumberOfUppercase should take a char*, not an int (or the class should take a char* in its constructor if you knew how to do that). You are passing line, which is a C style string to the SetNumberOfUppercase function, so the SetNumberOfUppercase function parameter should accept a char* to hold the string. How familiar are you with function parameters and types?

    Incidentally, C style strings don't make much sense in C++, the C++ string class would usually be better.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM
  2. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2003, 11:03 PM
  3. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21