My function returns an address instead of an integer

Can somebody tell me why I keep getting an address when I run the length function? I don't have any of the other functions or even the list implemented but I thought that would mean it returns a 0 instead of an address.

class Vlist{

private:

class Node
{
public:
Node(Video *video, Node *next): videos(video), m_next(next)
{}

Video *videos;
Node *m_next;
};

Node *m_head;

public:

Vlist();
~Vlist();
void insert(Video *video);
void insert_sorted();
void print();
bool find();
bool remove();
int length();

};


Part of vlist.cpp

int Vlist:: length()
{
int size = 0;
Node *ptr = m_head;
while(ptr != NULL)
{
size++;
ptr = ptr -> m_next;
}
return size;
}


int main()
{
Vlist vlist;
string title, url, comment,command;
double length;
int rating;

while (getline(cin,command))
{
if (command != "length" && (command != "insert") && (command != "print") && (command != "lookup") && (command != "remove"))
{
cerr<< "<" << command << "> is not a legal command, giving up..." << endl;
return 1;
}

else if (command == "length")
{
vlist.length();
cout << length << endl;
}

else if (command == "insert")
{
}

else if (command == "print")
{
vlist.print();
}

else if (command == "lookup")
{
}

else if (command == "remove")
{
}
}

return 0;
}

I have an entire vlist.cpp and vlist.h file if needed. I'm not really sure where to locate the problem.
Last edited on
Are you referring to this part:
 
double length;


1
2
3
4
5
else if (command == "length")
{
    vlist.length();
    cout << length << endl;
}


First, the variable length is declared, but not initialised - which means it contains a garbage value.

Then the function vlist.length(); is called but nothing is done with the resulting value. Last, the uninitialised variable is output.

Something like this could work:
1
2
3
4
else if (command == "length")
{
    cout << vlist.length() << endl;
}


or maybe
1
2
3
4
5
else if (command == "length")
{
    int length = vlist.length();
    cout << length << endl;
}




Ah okay thank you so much!
Topic archived. No new replies allowed.