Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Increasing Order Array...bugs (http://www.programmingforums.org/showthread.php?t=1431)

TecBrain Dec 5th, 2004 5:59 PM

Well this was our lecturer code for sorting the numbers in increasing order, well I wanted to compile the codes but it seems to have some errors, appreciate any support.

:

#include <iostream>
#include <stdlib.h>

using namespace std;


 class list
  {
    public: int value;
    list *index;
  }
 
void main()
{
 
      list *head,*t,*newitem;
      char ch;
      head=null;
      cout << "Do you want to instert a Data ln (Y \ N) ln";
      cin >> ch;
      while ((ch=='y') || (ch=='Y'))
      {
        new item = new list
        new item -> next=null;
        cout << "Enter a Number ln";
        cin >> newelm -> value;
        if (head==null)
        head=newelm;
        else
        {
          if (newelm -> value < head -> value)
          {
            newelm -> next = head;
            head = newelm;
          }
          else
          {
            t=head
            while ((t -> next !=null) && ( t -> next -> value < newelm -> value))
            t=t -> next;
            newelm -> next=newelm;
          }
        }
        cout << "Do you wnat to continue ( Yes or No) ln";
        cin >> ch;
      }         
 
 
 
 system("PAUSE");       
 return 0;
}


Thanks.

andersRson Dec 6th, 2004 2:48 AM

first of all, it would have been a good idea to actually include those compiler errors in the post, so everyone don't have to compile the code themselves to see what happens.
Next thing to try is to READ THE CODE! There are a whole bunch of obvious typo's and little errors like missing semicolons.
Another thing is a lot of references to the "next"-field of list object - there is no next in the list class.
You can't do
:

new item = new item;
new item -> next = null;

you have to create a variable to put the bloody new item in, otherwise you can't refer to it. And again, the list class doesn't have a "next"-field. And, constructor calls need parentheses after them.
:

item* ni = new item();
ni.index = null;


I suggest you read the literature you're supposed to read, and read it properly. And don't try to pass it off as your lecturer's code, it's just obviously not written by anyone with a little experience, unless he/she was stoned. Which, of course, could have been the case.

here's a version that compiles, but it might not work:
:

#include <iostream>
#include <stdlib.h>

#define null 0

using namespace std;

class list {
public: int value;
  list *next;
};
 
int main(){
 
  list *head,*t,*newitem;
  head = null;
  char ch;

  cout << "Do you want to instert a Data ln (Y \\ N) ln";
  cin >> ch;
  while ((ch=='y') || (ch=='Y')){
        list* nelem = new list();
        nelem->next = null;
        cout << "Enter a Number ln";
        cin >> nelem->value;

        if (head == null) {
  head = nelem;
        } else {
  if (nelem->value < head->value)  {
 nelem->next = head;
 head = nelem;
  } else {
 list* t = head;
 while ((t->next !=null) && ( t->next->value > nelem->value))
  t = t->next;

 nelem->next=nelem;
  }
        }
        cout << "Do you wnat to continue ( Yes or No) ln";
        cin >> ch;
  }       
 
 
 
  system("PAUSE");
  return 0;
}



All times are GMT -5. The time now is 3:20 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC