#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    13
    Rep Power
    0

    return NULL problem


    Hey..I have the following function which returns a const char*

    However...there are times that the function wont return anyting..but just cout a message. But the function still needs to retun a const char*..

    so i tried to do return NULL; It copiles..but when it runs it quits!

    then i tried declaring a char* a; without assigning a char for it to point to..
    and to to do return a; but same problem

    the code is bellow..any ideas?? THANKSS!!!

    const char* HashTable::get(int k) const
    {
    int y = k % TABLE_SIZE;

    if(hash[y].isIn(k))
    {
    return hash[y].inspect(k);
    }
    else
    {
    cout << "\n\nThere are no contents in that position\n\n";
    return NULL;
    }
    }
  2. #2
  3. No Profile Picture
    "Null, The Terminator"
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    11
    Rep Power
    0
    try
    (const char *) NULL
    or
    (const char *) 0

    just a guess......
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    55
    Rep Power
    12
    Why reuturn a const char? Casting it to a const won't change anything as it's not a compile time error, const only tells the compiler to watch for the developer changing constant data in the source code at some point.

    I'm not sure why your function isn't returning, there is no chance of it reaching below the else. Are you positive you are checking the return correctly?
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    13
    Rep Power
    0
    Hey..thanx for your reply...

    did you mean to try:

    return (const char*)NULL;
    or
    return (const char*)0;

    ?
    If that is what you meant...it didnt work unfortunately. THANX for the attempt anyhow :)
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    13
    Rep Power
    0
    Hey Xtor,

    The reason i have the const char* as the return type is simply becuase my professor wants it that way.. So i can't change that. I see your point but professor is preety stubborn on keeping the function signature as it is.

    If i remove the return NULL; line in the ELSE statment, then it compiles and it runs. The else statment works because it will do the cout statement. However once I put the retun NULL affter the cout statement, it compiles..runs..but once it hits the else statment it quits..without printing the cout statment even which is before the return NULL statement.

    I can not put in the return NULL statment and the assignment will work fine..BUT..becuase it is expecting to return SOMEHTINg and i havent stated it what to return..it prints some wierd character( as in rubbish character from blank memory i guess).

    any ideas?

    THANX agian!! :)
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    55
    Rep Power
    12
    It would probably be better if you pasted all of your code so we can see how you are handling data before and after the function call.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    13
    Rep Power
    0
    Ok..there are 5 files.

    main.cpp
    OrderedList.h
    OrderedList.cpp
    HashTable.h
    HashTable.cpp

    i will paste the code in that order.
    The HashTable inherits from the OrderedList.
    So the entire project istrying to create a hash table. An array of orderedlists. The code of the ordered list is taken from another assignment I had to do so that all works fine. The entire hashtable program too works fine..apart from when trying to get the contents of a hash table content that doesnt exist. As i said before, it couts that the item doesnt exist...but also returns a funny character on the screen. If i put return NULL after the cout statement then it just exits the program!

    so here is all the code..thanx

    Code:
    //-----------------------The main.cpp----------
    #include <iostream.h>
    #include <iostream>
    #include <string>
    #include "HashTable.h"
    
    using std::string;
    
    void main()
    {
    	int menuChoice = 0;
    	int position;
    	char word[80];
    	HashTable table;
    	
    	for(;;)
    	{
    		cout << "Enter\t1)  To insert a word\n";
    		cout << "\t2)  To get the contents of a position\n";
    		cout << "\t3)  To remove a position\n";
    		cout << "\t4)  To exit\n";
    		cin >> menuChoice;
    		cin.ignore(80,'\n');
    
    		switch(menuChoice)
    		{
    		case 1:
    			cout << "\nEnter the word you want to insert:\n\n";
    			cin.getline(word, 80, '\n');
    			cout << "\nWhat position do you want it in:\n\n";
    			cin >> position;
    			table.put(position, word);
    			cout << endl;
    			break;
    
    		case 2:
    			cout << "\nWhich position's contents do you want to check:\n\n";
    			cin >> position;
    			cout << "\n\n" << table.get(position) << "\n\n";
    			break;
    
    		case 3:
    			cout << "\nWhat position do you want to remove:\n\n";
    			cin >> position;
    			table.del(position);
    			cout << endl;
    			break;
    
    		case 4:
    			cout << "\nBye Bye!\n\n";
    			exit(1);
    		}
    
    	}
    }
    
    
    //--------------OrderedList.h----------
    #include <string>
    
    using std::string;
    
    class Node
    {
    private:
    	int key;
    	Node* next;
    	char* letters;
    
    	Node();
    
    	friend class OrderedList;
    };
    
    class OrderedList
    {
    public:
    	OrderedList();
    	~OrderedList();
    
    	void insert(char[], int);
    	void remove(int);
    	const char* inspect(int) const;
    	bool isIn(int) const;
    	bool isEmpty() const;
    	void printList() const;
    
    private:
    	Node* head;
    };
    
    
    //-------------OrderedList.cpp----------
    #include "OrderedList.h"
    #include <stddef.h>
    #include <iostream.h>
    #include <string.h>
    
    Node::Node()
    {
    	next = NULL;
    }
    
    OrderedList::OrderedList()
    {
    	head = NULL;
    }
    
    OrderedList::~OrderedList()
    {
    	Node* p;
    	Node* toGo;
    
    	for(p = head; p != NULL;)
    	{
    		toGo = p;
    		p = p -> next;
    		delete toGo -> letters;
    		delete toGo;
    	}
    }
    
    void OrderedList::insert(char word[], int k)
    {
    	Node* current = head;
    	Node* previous = NULL;
    
    	while(current != NULL && current -> key < k)
    	{
    		previous = current;
    		current = current -> next;
    	}
    
    	if(current != NULL && current -> key == k)
    	{
    		cout << "\nIt already exists in the list.\n\n";
    	}
    	else
    	{
    		Node* p = new Node;
    
    		if(p == NULL)
    		{
    			cout << "The memory is full\n";
    			exit(1);
    		}
    		else
    		{
    			p -> key = k;
    			p -> letters = new char[strlen(word) + 1];
    			strcpy(p -> letters, word);
    			p -> next = current;
    
    			if(previous == NULL)
    			{
    				head = p;
    			}
    			else
    			{
    				previous -> next = p;
    			}
    		}
    	}
    }
    
    void OrderedList::remove(int k)
    {
    	Node* current = head;
    	Node* previous = NULL;
    
    	while(current != NULL && current -> key < k)
    	{
    		previous = current;
    		current = current -> next;
    	}
    
    	if(current == NULL || current->key != k)
    	{
    		cout << "\nThat number is not in the list\n";
    	}
    	else
    	{
    		if(previous == NULL)
    		{
    			head = current->next;
    		}
    		else
    		{
    			previous->next = current->next;
    		}
    
    		delete current;
    	}
    }
    
    const char* OrderedList::inspect(int k) const
    {
    	Node* current = head;
    
    	while(current != NULL && current -> key != k)
    	{
    		current = current -> next;
    	}
    
    	return current -> letters;
    }
    
    bool OrderedList::isIn(int k) const
    {
    	Node* current = head;
    
    	while(current != NULL && current -> key < k)
    	{
    		current = current -> next;
    	}
    
    	if(current == NULL || current -> key != k)
    	{
    		return false;
    	}
    	else
    	{
    		return true;
    	}
    }
    
    bool OrderedList::isEmpty() const
    {
    	return head == NULL;
    }
    
    void OrderedList::printList() const
    {
    	Node* current = head;
    	cout << endl;
    
    	while(current != NULL)
    	{
    		cout << current -> letters << ", ";
    		current = current -> next;
    	}
    }
    
    
    //-------------HashTable.h----------
    
    #include "OrderedList.h"
    
    const int TABLE_SIZE = 101;
    
    class HashTable
    {
    public:
    	HashTable();
    	~HashTable();
    
    	void put(int k, char* s);
    	const char* get(int k) const;
    	void del(int k);
    	
    
    private:
    	OrderedList hash[TABLE_SIZE];
    };
    
    
    //------------HashTable.cpp----------
    
    #include <stddef.h>
    #include "HashTable.h"
    #include <iostream.h>
    
    HashTable::HashTable()
    {
    	
    }
    
    HashTable::~HashTable()
    {
    
    }
    
    void HashTable::put(int k, char* s)
    {
    	int x = k % TABLE_SIZE;
    
    	hash[x].insert(s, k);		
    }
    
    const char* HashTable::get(int k) const
    {
    	int y = k % TABLE_SIZE;
    
    	if(hash[y].isIn(k))
    	{
    		return hash[y].inspect(k);
    	}
    	else
    	{
    		cout << "\n\nThere are no contents in that position\n\n";
    		return NULL;
    	}
    }
    
    void HashTable::del(int k)
    {
    	int z = k % TABLE_SIZE;
    	hash[z].remove(k);
    }
    [edit]Placed code tags around code[/edit]
    Last edited by Onslaught; August 17th, 2003 at 07:51 PM.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    13
    Rep Power
    0
    by the way...I am new to this forum..how do i get the indentation to show on posts? becuase this makes it harder for people to follow the brackets...
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    55
    Rep Power
    12
    You put your code inbetween [ code ] [ /code ] tags, without the spaces. I'll edit this post with an answer (if I have one) after I've looked at your code.
  18. #10
  19. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,253
    Rep Power
    2222
    If you are going to return a value that indicates an error condition, then you need to test for that error condition when you return from that function. Instead, it looks like (too much code to slog through at the moment) you are using that return value whether it points to a string or is NULL.

    For example, instead of:
    Code:
    cout << "\n\n" << table.get(position) << "\n\n";
    try (I left const'ing out)
    Code:
    char *return_value;
    if ((return_value = table.get(position)) != NULL)
        cout << "\n\n" << return_value << "\n\n";
  20. #11
  21. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    793
    dwise_aol is correct IMHO
    what is causing your program to die is cout << NULL which isn't acceptable.
    Either do as dwise suggested, or instead of returning null, return the error statement you sent to cout in the function call instead of NULL.

IMN logo majestic logo threadwatch logo seochat tools logo