unknown error! that couse no output!

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 2188 Views - Last Post: 04 February 2014 - 04:05 PM Rate Topic: -----

#16 #define   User is offline

  • Cannot compute!
  • member icon

Reputation: 1868
  • View blog
  • Posts: 6,763
  • Joined: 19-February 09

Re: unknown error! that couse no output!

Posted 03 February 2014 - 12:39 PM

Hi, you could add an if statement in your print function to print zero when the list is empty. Or you could print something else such as nan.

void print(ostream &out)
{
  binaryNode *p=head;

  if( head == NULL)
  {
    // print 0

    return;
  }

  while  (p != NULL)
  {
    out << p->data << " ";
    p = p->next;
  }
}



Was This Post Helpful? 0
  • +
  • -

#17 susieym   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 28-January 14

Re: unknown error! that couse no output!

Posted 03 February 2014 - 12:51 PM

View Post#define, on 03 February 2014 - 12:39 PM, said:

Hi, you could add an if statement in your print function to print zero when the list is empty. Or you could print something else such as nan.

void print(ostream &out)
{
  binaryNode *p=head;

  if( head == NULL)
  {
    // print 0

    return;
  }

I tried to do as you said, however, there is nothing changed. 

  while  (p != NULL)
  {
    out << p->data << " ";
    p = p->next;
  }
}



Was This Post Helpful? 0
  • +
  • -

#18 susieym   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 28-January 14

Re: unknown error! that couse no output!

Posted 03 February 2014 - 01:10 PM

View Postsusieym, on 03 February 2014 - 12:51 PM, said:

View Post#define, on 03 February 2014 - 12:39 PM, said:

Hi, you could add an if statement in your print function to print zero when the list is empty. Or you could print something else such as nan.

void print(ostream &out)
{
  binaryNode *p=head;

  if( head == NULL)
  {
    // print 0

    return;
  }

I tried to do as you said, however, there is nothing changed. 

  while  (p != NULL)
  {
    out << p->data << " ";
    p = p->next;
  }
}





I tried to do wast you told me, however there is no changes in the results.
Was This Post Helpful? 0
  • +
  • -

#19 #define   User is offline

  • Cannot compute!
  • member icon

Reputation: 1868
  • View blog
  • Posts: 6,763
  • Joined: 19-February 09

Re: unknown error! that couse no output!

Posted 03 February 2014 - 02:05 PM

hi , susieym there is no need to quote what is in the previous post everytime.

In the code I left a little for you to do. Here I've added the code.


void print(ostream &out)
{
  binaryNode *p=head;

  // If list is empty print zero
  if( head == NULL)
  {
    // print zero
    cout << 0;  // <- I added this line
    return;
  }

  // print each item in the list
  while  (p != NULL)
  {
    out << p->data << " ";
    p = p->next;
  }
}



See if that works, and you will need to start reading the code to check it is correct.
Was This Post Helpful? 0
  • +
  • -

#20 #define   User is offline

  • Cannot compute!
  • member icon

Reputation: 1868
  • View blog
  • Posts: 6,763
  • Joined: 19-February 09

Re: unknown error! that couse no output!

Posted 03 February 2014 - 03:14 PM

Hi, if you comment out this line then the progam will continue, you haven't any code in the assignment function.

{
  //...

    //numberArray[numId1] = numberArray[numId2];




To assign a value the old list would be deleted and the new list copied. You are deleting in the destructor so it would be good to have a function that can be used in both. A copy list function is useful as well.

  void deleteList();

  void copyList(const binarynumber &rhs);



In the print function I used cout when it should be out.

Edit: there is a problem with your destructor code as well.
If you constructed the list with a value of zero then you might not need the check in print.
.

This post has been edited by #define: 03 February 2014 - 03:33 PM

Was This Post Helpful? 0
  • +
  • -

#21 #define   User is offline

  • Cannot compute!
  • member icon

Reputation: 1868
  • View blog
  • Posts: 6,763
  • Joined: 19-February 09

Re: unknown error! that couse no output!

Posted 04 February 2014 - 02:11 PM

Hi, we can look at the problem with the destructor code and also a problem with pushBack.


As I said the functionality to delete the list can be used elsewhere, so a function would be useful.

  //declaration of the destructor 
  ~binaryNumber()
  {
    delete_list();
  }



There was a couple of problems with your destructor now delete_list.

I've used a pointer curr to iterate over the list, as your p pointer did. I've added another pointer temp which will be used in the call to delete. You were using p = head->next which doesn't iterate along the list. You also deleted head and tail, they should already be deleted in the loop and are not required. The variable length was also cleared.


  void delete_list()
  {
    binaryNode *curr=head;
    binaryNode *temp;  // added another pointer

    cout << "Deleting=";  // for debugging

    while( curr )
    {
      temp = curr;
      cout << curr->data << ",";  // for debugging
      curr = curr->next;  // iterate using curr not head
      delete temp;
    }

    cout << endl;  // for debugging 

    head=NULL;
    tail=NULL;
    length = 0;
  }




With the pushBack function, firstly the data parameter was given a const modifier since the binaryNode constructor uses a const parameter. You had p = tail shouldn't that be tail = p. Lastly, since you are using the tail pointer I used tail->next to jump to the end of the list, although p->next would have gotten there too.

    // push back function (recursive) that will be helpful
    void pushBack (binaryNode *&p, const int & data)  // *** const
    {         
      if (p == NULL)
      {
        p = new binaryNode (data, NULL);
        // p = tail; // ***
        tail = p;  // remember last node location
        length++;
      }
      else
      {
        pushBack(tail->next,data); //*** p to tail
      }
      
    }




I was asking if the push_back should not be in the public section, I was thinking of containers like vectors. In your case you don't seem to need a public version.
Was This Post Helpful? 0
  • +
  • -

#22 SplinteredChaos   User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 115
  • Joined: 15-September 11

Re: unknown error! that couse no output!

Posted 04 February 2014 - 03:42 PM

As most of this is a bit beyond my abilities (and concentration levels) at the moment I am not sure if this will be of any assistance, but one thing that did jump out at me is your print function. You have out << head->data<<" "; Does it have to be cout, so the program knows to print the results to the console?

void print(ostream &out)
{
	binaryNode *p=head;
	while  (head!=NULL)
	{
		out<<head->data<<" ";
		head = head->next;
	}
	head=p; 
}

Was This Post Helpful? 0
  • +
  • -

#23 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: unknown error! that couse no output!

Posted 04 February 2014 - 04:05 PM

Quote

Does it have to be cout, so the program knows to print the results to the console?

No you use out, the parameter so the information is printed to whatever stream you passed into the function. Remember cout is just another stream.

Jim
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2