Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 5th, 2004, 4:59 PM   #1
TecBrain
Hobbyist Programmer
 
Join Date: Sep 2004
Location: Cyprus
Posts: 147
Rep Power: 5 TecBrain is on a distinguished road
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.
__________________
Personal Portfolio
TecBrain Support Forum
Linux VS Windows ... Dont Even Think of it ..
Distribution: Slackware
if (OS==Linux) return success
There are 10 kinds of people, those who can read binary numbers and those who can't.
TecBrain is offline   Reply With Quote
Old Dec 6th, 2004, 1:48 AM   #2
andersRson
Newbie
 
Join Date: Nov 2004
Posts: 16
Rep Power: 0 andersRson is on a distinguished road
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;
}
andersRson is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:25 AM.

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