Hi,
I'm working on classes with pointers. I do have a main function that calls this.
This program compiles. But the input from the user isn't getting passed to the other functions.
The user_input function should take the response and pass it to the create_posting then it is outputted to the display function.
I think the problem is in the user_input function but I've tried tweaking things and it still isn't working.
Any tips are appreciated.

class Posting
{
        public:

        Posting(); //Constructor
        void user_input(char * input,char* &response);
        void create_posting();  



        private:

        char *type;
        char *info;
        char *place;
        char *email;


};
Posting::Posting()
{
        type=NULL;
        info=NULL;
        place=NULL;
        email=NULL;
}
void Posting::user_input(char * input,char* &response)

{
        char temp[500];

        cout << input;
        cin.get(temp,500,'\n');
        cin.ignore(100,'\n');
        response=new char[strlen(temp)+1];
        response[0]=toupper(response[0]);
}
void Posting::create_posting()
{
        user_input( "What type of posting is this? ", type);
        user_input( "Give a short description of your posting: ",info);
        user_input("What is your location? ", place);
        user_input("What is your e-mail? ", email);
}

void Posting::display_posting()

{
  cout<< "This is your posting"<<'\t'<<type<<'\t'<<info<<'\t'<<place<<'t'<<email   <<endl;

Some problems seen:

Don't forget to delete those pointers using delete[]. Can be done in the destructor or before you decide to reuse/reallocate the pointer.
Posting class does not have a member function called display_posting();
Deprecated conversion warnings from string to char* (Can be ignored really or change first parameter to const char* for user_input function)

Finally, you didn't copy anything to your response. Your response contains NULL values..
You need to do: strcpy(response, temp); //Copy the temp data to the response..

#include <iostream>
#include <cstring>

using namespace std;

class Posting
{
    public:
        Posting(); //Constructor
        ~Posting(); //Destructor
        void user_input(const char* input, char* &response); //const char* for first parameter to get rid of the warnings..
        void create_posting();
        void display_posting();    //Your class was missing this data-member.

    private:
        char *type;
        char *info;
        char *place;
        char *email;
};

Posting::~Posting()  //Deletes all allocated memory when the class gets destroyed or goes out of scope.
{
    delete[] type;
    delete[] info;
    delete[] place;
    delete[] email;
}

Posting::Posting()
{
    type=NULL;
    info=NULL;
    place=NULL;
    email=NULL;
}

void Posting::user_input(const char* input, char* &response)  //const char* first parameter.
{
    char temp[500];

    cout << input;
    cin.get(temp,500,'\n');
    cin.ignore(100,'\n');
    response = new char[strlen(temp) + 1];
    memset(response, 0, strlen(temp) + 1);  //Set all values to 0.. Makes the string null terminated for safety. (Just an extra precaution)
    strcpy(response, temp);                 //Copy the input data to the response.
    response[0] = toupper(response[0]);     //After the copy, you may now uppercase the first character.
}

void Posting::create_posting()
{
    user_input( "What type of posting is this? ", type);
    user_input( "Give a short description of your posting: ",info);
    user_input("What is your location? ", place);
    user_input("What is your e-mail? ", email);
}

void Posting::display_posting()
{
    cout<< "This is your posting"<<'\t'<<type<<'\t'<<info<<'\t'<<place<<'\t'<<email   <<endl;
}

int main()
{
    Posting P;
    P.create_posting();
    P.display_posting();
}

That worked. Thank you for the helpful advice!

commented: The first parameter of user_input should const char*. After that, you'll be perfect! :) +6

Why is this threat not marked as solved yet?

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.