More about classes.

This is a discussion on More about classes. within the C++ Programming forums, part of the General Programming Boards category; Ok I checked google and got no results and I even searched the board. I found the answer on the ...

  1. #1
    1479
    Join Date
    Aug 2003
    Posts
    253

    More about classes.

    Ok I checked google and got no results and I even searched the board. I found the answer on the board but I don't understand it.

    So how do you pass a function a characater array, how do you declare it and how do I get it to work?
    Knowledge is power and I want it all

    -0RealityFusion0-

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Code:
    void Func(char *arr)
         {
         //Do something with the str
         }
    
    
    int main(void)
         {
         char str[256];
         Func(str);
         return 0;
         }
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    A character array meaning a string?
    Code:
    void func(char* c);
    
    int main(void)
    {
    func("string");
    
    
    }
    
    void func(char* c)
    {
    for (int i=0;c[i]!='\0';i++)
      cout<<c[i];
    
    }
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #4
    1479
    Join Date
    Aug 2003
    Posts
    253
    I made some changes:

    Code:
    #inlcude <iostream>
    using namespace std;
    
    class Cat
        {
        public:
            GetName(char* name)
        }
             
    void Cat::GetName(char* name)
        {
    
        cout <<"Enter a name." <<endl;
        cin.getline(name,20);
    
        cout <<"The name you have given is: " <<endl;
        cout <<name;
        }
    int main()
        {
        char name[20];
        
         Cat::GetName(name);
            
    system("pause");
        }
    I changed what is in red and now I get 11 errors?
    Last edited by RealityFusion; 08-15-2003 at 01:15 PM.
    Knowledge is power and I want it all

    -0RealityFusion0-

  5. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Code:
    #inlcude <iostream>
    
    Read carefully
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  6. #6
    1479
    Join Date
    Aug 2003
    Posts
    253
    hahaha!
    Knowledge is power and I want it all

    -0RealityFusion0-

  7. #7
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    a good way to approach your errors is to handle the first error that shows up first. I'm sure that little typo was the first.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  8. #8
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    BTW, prefer std::string to character arrays (and std::vector to arrays of other types) especially when passing to a function. Unlike java, an array is passed only as the pointer to the first array element -- there is no length information passed. Buffer overruns, etc. are tricky things. Further, a multidimentional array is MUCH better passed as a std::vector of std::vectors (for the 2D case); it's easier to declare, won't leak memory, and is just as easy to use.

  9. #9
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    I get the idea you like Stl, Cat.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  10. #10
    1479
    Join Date
    Aug 2003
    Posts
    253
    I made some changes:

    Code:
    #inlcude <iostream>
    using namespace std;
    
    class Cat
        {
        public:
            GetName(char* name)
        }
             
    void Cat::GetName(char* name)
        {
    
        cout <<"Enter a name." <<endl;
        cin.getline(name,20);
    
        cout <<"The name you have given is: " <<endl;
        cout <<name;
        }
    int main()
        {
        char name[20];
        
         Cat::GetName(name);
            
    system("pause");
        }
    I changed what is in red and now I get 11 errors?
    Knowledge is power and I want it all

    -0RealityFusion0-

  11. #11
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    You need to call the function GetName on an instance of a Cat. Cat is an abstract idea, and clearly you cannot get the name of Cat, but you can get the name of a Cat. The syntax you are using only works for static funtions (which don't require an associated instance of the class).

    Code:
    int main()
    {
      char name[20];
      Cat felix;
      felix.GetName(name);
      return 0;
    }
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  12. #12
    1479
    Join Date
    Aug 2003
    Posts
    253
    Changed it and it is still giving me the same errors.

    11 C:\Documents and Settings\Owner\My Documents\FucntionsCallingStrings.cpp
    semicolon

    11 C:\Documents and Settings\Owner\My Documents\FucntionsCallingStrings.cpp
    ISO

    11 C:\Documents and Settings\Owner\My Documents\FucntionsCallingStrings.cpp
    syntax

    7 C:\Documents and Settings\Owner\My Documents\FucntionsCallingStrings.cpp
    candidate

    is C:\Documents and Settings\Owner\My Documents\ is
    void Cat::GetName(char*)
    Knowledge is power and I want it all

    -0RealityFusion0-

  13. #13
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,705
    You're missing a semi-colon after the function declaration of GetName() and after the structure definition.
    Code:
    #include <cmath>
    #include <complex>
    bool flip(bool value)
    {
           return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0)*(1 << (value + 2)))
        ).real() < 0;
    }

  14. #14
    Registered User
    Join Date
    Mar 2002
    Posts
    249
    So what you're really trying to say is....
    Code:
    #include <iostream>
    using namespace std;
    
    class Cat
    {
        public:
            void GetName(char* name);
    };
             
    void Cat::GetName(char* name)
    {
        cout <<"Enter a name." <<endl;
        cin.getline(name,20);
    
        cout <<"The name you have given is: " <<endl;
        cout <<name;
    }
    int main()
    {
        char name[20];
        Cat felix;
        felix.GetName(name);
    
        return 0;
    }
    Last edited by funkydude9; 08-15-2003 at 05:18 PM.
    Well, there are a few things wrong with your code:

    1) It does not work.
    2) It does not work.
    3) It does not work.

    Hope this helps.

  15. #15
    1479
    Join Date
    Aug 2003
    Posts
    253
    Thank you all that helped me!
    Knowledge is power and I want it all

    -0RealityFusion0-

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 08:04 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 04:38 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM

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