After I enter first name then program fail.

Error: RUN FAILED (exit value 1, total time: 2s). I use Netbeans

#include <iostream>

using namespace std;

struct Student {
    char * firstName;
    char * lastName;
    char * telephone;
};

void printStudent(Student s) {
    cout << "Student info: " << endl;
    cout << "Name: " << s.firstName << " " << s.lastName << ". Telephone: " << s.telephone << endl;
}

int main() {
    Student s;
    cout << "First name?" << endl;
    cin.getline(s.firstName, 100);
    cout << "Last name?" << endl;
    cin.getline(s.lastName, 100);
    cout << "Telephone?" << endl;
    cin.getline(s.telephone, 100);

}

The problem is that there is no memory allocated to the members of that structure -- they are just pointers whose address is unknown. cin does not allocate memory -- that is your responsibility. Try this structure

struct Student {
    char firstName[100];
    char lastName[100];
    char telephone[20];
};

line 22: why do you want a 100-digit telephone number?? I don't think there are any phone numbers in the whole world that has that many digits. Reduce that to something more reasonable, such as 10 or 15. Even that is probably more than enough.

Thanks for your help. Now my code is OK. 100-digit telephone number because I just want test program. It'll be fixed after.

You might want to use sizeof there instead of hardcoding a number. That way when you change the size of the fields in the structure you won't have to bother making similar changes throughout the rest of the program. Hardcoding numbers like you did leads to lots of bugs that are often difficult to find.

Since your are using C++, why not rather use C++ strings?

// structStudent.cpp //  // 2014-02-25 //

#include <iostream>
#include <string>

using namespace std;


struct Student
{
private:
    string firstName;
    string lastName;
    string telephone;

public:
    void set_firstName( const string& fn ) { firstName = fn; }
    void set_lastName( const string& ln )  { lastName = ln;  }
    void set_telephone( const string& ph ) { telephone = ph; }

    string get_firstName() const { return firstName; }
    string get_lastName()  const { return lastName; }
    string get_telephone() const { return telephone; }

    void takeIn()
    {
        cout << "Enter first name :  " << flush;
        getline( cin, firstName );

        cout << "Enter last name  :  " << flush;
        getline( cin, lastName );

        cout << "Enter telephone  :  " << flush;
        getline( cin, telephone );
    }
    void print() const
    {
        cout << "Student info: " << endl;
        cout << "Name: " << firstName << " " << lastName
             << ", Telephone: " << telephone << endl;
    }
} ;


// vs ...
void takeIn( Student& s )
{
    string tmp;
    cout << "Enter first name :  " << flush;
    getline( cin, tmp );
    s.set_firstName( tmp );

    cout << "Enter last name  :  " << flush;
    getline( cin, tmp );
    s.set_lastName( tmp );

    cout << "Enter telephone  :  " << flush;
    getline( cin, tmp );
    s.set_telephone( tmp );
}
void print( const Student& s)
{
    cout << "Student info: " << endl;
    cout << "Name: " << s.get_firstName() << " " << s.get_lastName()
         << ", Telephone: " << s.get_telephone() << endl;
}



int main()
{
    Student s;

    // compare ...

    takeIn( s );
    print( s );

    // vs ...

    s.takeIn();
    s.print();
}

@Ancient Dragon: Thanks for your useful tip.
@David W: Thank you for spending time to rewrite my program in a better way. I learnt many from your code.

Hey ... always so nice to hear back from a 'happy coder'.

Enjoy your code!

You may be spending SO MUCH time coding ... or trying to figure out some code ... and that time spent could be a BIG problem ... especially, if you don't enjoy what you are doing :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.