Passing strings, please help!

This is a discussion on Passing strings, please help! within the C++ Programming forums, part of the General Programming Boards category; Hi thanks for looking... Below is the main method, followed by the student class. I want to pass the string ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    3

    Unhappy Passing strings, please help!

    Hi thanks for looking...

    Below is the main method, followed by the student class. I want to pass the string (called name) onto the student class. What am I doing wrong?

    #include <iostream.h>
    #include <string.h>
    #include "student.h"

    void main(void)
    {
    char name[10];
    cin.getline(name, 10, '\n');
    Person joe(name);

    cout<< name;
    cout<< joe.display();


    int i;
    cin >> i;
    }

    //below is the "student.h" class
    class Person
    {
    char name[10];
    public:
    Person(char nam[10])
    {
    name=nam;
    }

    char display()
    {
    return name;
    }
    };

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    In the Constructor, recieve "nam" as a reference. Also use Strcpy() to store it in "name", dont use '='.

    You might also want to pass the display as a pointer

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Array of Strings to a function ??
    By Taper in forum C Programming
    Replies: 20
    Last Post: 12-07-2008, 01:58 PM
  2. Passing strings to functions and modifying them
    By diddy02 in forum C Programming
    Replies: 6
    Last Post: 08-11-2008, 01:07 AM
  3. passing strings from functions as the "return" part
    By fraktal in forum C Programming
    Replies: 8
    Last Post: 12-13-2005, 12:38 AM
  4. Passing an Array of Strings from VB to a C DLL
    By mr_nice! in forum Windows Programming
    Replies: 9
    Last Post: 03-08-2005, 05:16 AM
  5. Passing Strings by Reference
    By xshapirox in forum C++ Programming
    Replies: 3
    Last Post: 10-11-2004, 09:35 AM

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