How to pass a string to a function

This is a discussion on How to pass a string to a function within the C++ Programming forums, part of the General Programming Boards category; Can anybody help me with this please? How can I pass the name from the user input to the constructor ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,336
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    2,144
    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.
    Code:
    namespace life
    {
        const bool change = true;
    }

  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
    2,144
    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.
    Code:
    namespace life
    {
        const bool change = true;
    }

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,170
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

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-21-2007, 11:58 PM
  3. C function - Pass by value? Pass By Ref?
    By stevong in forum C Programming
    Replies: 4
    Last Post: 11-18-2005, 07:02 AM
  4. Pass a string to a function
    By colinuk in forum C Programming
    Replies: 10
    Last Post: 01-31-2005, 07:05 PM
  5. Replies: 3
    Last Post: 04-02-2002, 12:39 PM

Tags for this Thread


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