Unable to cout object

This is a discussion on Unable to cout object within the C++ Programming forums, part of the General Programming Boards category; hi guys, i cant' seem to cout the object. the code goes below: ---------------- #include <iostream.h> #include <fstream.h> class vl ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    2

    Unable to cout object

    hi guys, i cant' seem to cout the object. the code goes below:
    ----------------

    #include <iostream.h>
    #include <fstream.h>

    class vl {
    private:
    char* ptr;
    public:
    vl() {
    ptr = new char[8];
    strcpy(ptr,"00000000");
    }
    void read(char* linefeed) {
    delete [] ptr;
    ptr = new char[strlen(linefeed)];
    strcpy(ptr,linefeed);
    }
    void display() {
    cout << ptr << endl;
    }
    ~vl() {
    delete [] ptr;
    }
    };

    void main() {
    fstream infile;
    char temp[120];
    infile.open("hp.txt",ios::in);
    while (infile) {
    vl integer;
    infile.get(temp,120,'\n'); infile.get();
    if ((!isdigit(temp[0])) || (!isalpha(temp[0]))) { continue; }
    integer.read(temp);
    integer.display();
    cout << endl;
    }
    cout << "END OF FILE" << endl;
    infile.close();
    }

    ---------------

    and e.g of a hp.txt file would be,
    ---------------
    1234
    -1234
    ---------------

    right now the priority would be echo out the object... but i cant seem to do it, anyone can help?

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    317
    right now it appears that you are just displaying the mem address, the pointer:
    Code:
    cout << ptr << endl; 
    Try dereferencing the pointer to obtain the value at the mem address

    Code:
    cout<<*ptr<<endl;

  3. #3
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    another problem:
    Code:
    vl() { 
      ptr = new char[8]; 
      strcpy(ptr,"00000000"); 
    }
    here you're assigning 9 bytes, (8 bytes + null terminator) to an eight byte space.

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    2

    Thanks guys

    Heh guys, thanks for your reply... but kinda ironed out the bugs already.

    one reason is because of the control if, thats the reason why its not echoed out, i think i got the conditions mixed up somewhere, but it works fine now...

    ptr works after i got the conditions right.. thanks

    modified the new char[9] to accomodate the \0, thanks for pointing that out too...

    *bows*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 11-14-2006, 11:52 AM
  2. GDI object lifetime and C++ object lifetime
    By VirtualAce in forum Windows Programming
    Replies: 4
    Last Post: 06-16-2006, 06:26 AM
  3. ERRPR: Object reference not set to an instance of an object
    By blackhack in forum C++ Programming
    Replies: 1
    Last Post: 07-13-2005, 06:27 PM
  4. Set Classes
    By Nicknameguy in forum C++ Programming
    Replies: 13
    Last Post: 10-31-2002, 02:56 PM
  5. Set Classes
    By Nicknameguy in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2002, 08:40 PM

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