Thread: How to pass a string to a function

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    7

    How to pass a string to a function

    Can anybody help me with this please?
    How can I pass the name from the user input to the constructor ?
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    
    class myClass
    {
    private:
    
    public:
        myClass() 
        {
        }
    
        myClass(int a)
        {
            cout << a << endl;
        }
    
        myClass(string name) 
        {
            cout << name << endl;
        }
    
        void myfunction()
        {
            cout << "something" << endl;
        }
    };
    
    
    int main()
    {
        string name;
    
        myClass object; 
        object.myClass(); 
        myClass(4); 
    
        cout << "your name?" << endl;
        getline(cin, thename);
        myClass(thename);
    
    
        system("pause");
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Try something like
    myClass myShinyNewObject(thename);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    7
    Can you be more explicit? I do not understand what do you mean.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    at this point your code doesn't even compile. check your variable names in main.

    also, how much more explicit do you need it? salem literally gave you the line of code you need.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    62
    what Salem mean is that on line 43 you have
    Code:
    myClass(thename);
    myClass is not an object. If you want to use a class constructor, you should actually "construct something".

    having that said, that is far from being the only error in your code... its just the one you asked about.
    Last edited by killme; 09-13-2013 at 08:18 AM.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    actually, the code would compile and run as written, if "thename" was actually defined. the effect would be to create a new temporary "myClass" object, and immediately destroy it.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by valentin View Post
    Can you be more explicit? I do not understand what do you mean.
    To create an object, you type
    <Type of class> <Name>(<Parameter list>);
    If the parameter list is empty, you must type
    <Type of class> <Name>;
    The <Parameter list> is the same as if you would call a function, so to pass a variable TheName to the object, you would do

    <Type of class> <Name>(TheName);

    For example

    MyClass MyName(TheName);

    The parameter will be passed to the constructor.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pass an string of array in a function
    By kantida in forum C Programming
    Replies: 1
    Last Post: 04-29-2011, 04:50 AM
  2. Replies: 3
    Last Post: 11-22-2007, 12:58 AM
  3. C function - Pass by value? Pass By Ref?
    By stevong in forum C Programming
    Replies: 4
    Last Post: 11-18-2005, 08:02 AM
  4. Pass a string to a function
    By colinuk in forum C Programming
    Replies: 10
    Last Post: 01-31-2005, 08:05 PM
  5. Replies: 3
    Last Post: 04-02-2002, 01:39 PM

Tags for this Thread