-5

Im having trouble storing my string from user input into fileName. I need to save fileName into GetfileName().

Here is a snippet of my code:

 class Frame {
        char* fileName;
        Frame* pNext;
    public:
        Frame();
        ~Frame();
        char*& GetfileName() { return fileName; }
        Frame*& GetpNext() { return pNext; };
    };


    void Animation::InsertFrame() {
        Frame* frame = new Frame; //used to hold the frames
        char* firstName = new char[40];

        cout << "Please enter the Frame filename :";

        cin.getline(firstName, 40); //enter a filename
        strcpy(&frame->GetfileName, firstName); //error, need to copy the inputed name into the function getFileName that returns a char* filename


}
Pavan Chandaka
  • 11,671
  • 5
  • 26
  • 34
bob smith
  • 63
  • 8

1 Answers1

1

I've made small changes in your source code in order to test it and fix it. I've created a method called SetfileName in Frame class and also changed the char *fileName to char fileName[40], so that Frame class holds the value of fileName instead of the pointer.

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

 using namespace std;

 class Frame {
        char fileName[40];
        Frame *pNext;

    public:
        Frame() {}
        ~Frame() {}
        const char *GetfileName () { return fileName; }
        const Frame *GetpNext () { return pNext; };

        void SetfileName(const char *name) { strncpy(fileName, name, sizeof(fileName)); }

        void printFileName() { cout << fileName << endl;  }
};


void InsertFrame() {
        Frame* frame = new Frame; //used to hold the frames
        char* firstName = new char[40];

        cout << "Please enter the Frame filename :";

        cin.getline(firstName, 40); //enter a filename
        frame->SetfileName(firstName);
        frame->printFileName();
}

int main() {

    InsertFrame();

    return 0;
}
Claudio Borges
  • 132
  • 2
  • 6