![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Sep 2004
Location: Cyprus
Posts: 147
Rep Power: 5
![]() |
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. |
|
|
|
|
|
#2 |
|
Newbie
Join Date: Nov 2004
Posts: 16
Rep Power: 0
![]() |
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; 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;
} |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|