C++ School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

student name & id with linked list need help ASAP Rate Topic: -----

#1 anya_ritika  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 16-March 08


Dream Kudos: 0

Share |

student name & id with linked list

Post icon  Posted 14 April 2009 - 04:19 PM

my code is supposed to give three situations...
if user inputs 1, then code has to list the student #include <iostream>
name and id already in the code.
if user inputs 2, then the code should allow the user to add student name to the linked list and id will be automaticallly inserted as (previous id+1).
if usre inputs 3, response should be exit the code.
however, my program, even though it compiles, it doesnt workn properly..
#include <cstdlib>

using namespace std;

struct node
  {  char name[20];	// Name of up to 20 letters
	 int id;		  // D.O.B. would be better
	
	 node *nxt;		// Pointer to next node
  };
int main()
{
node *start_ptr = NULL;

node * temp = new node;
if (start_ptr == NULL)
start_ptr = temp;
	
temp = start_ptr;
int n;
cout<<"enter:";
cin>>n;

while (temp != NULL);
{

if (temp == NULL)
{cout << "End of list" << endl;
return 1;
}
switch (n){
		  case 1:	  cout<<"Name : something"<<"  "<<"id: 1"<<endl;break;
		  case 2:	  cout << "Name : ";
					   cin>>temp->name;
					   cout << "id : ";
					   cin>>temp->id;
		  
		  cout << endl;break;	   // Blank line

		  // Move to next node (if present)
		  temp = temp->nxt;
		  case 3:		  return 1;
	   
  }
}
system ("pause");

return 0;
}


any help would be much appreciated.
Was This Post Helpful? 0
  • +
  • -


#2 bodom658  Icon User is offline

  • Villiage Idiom
  • Icon

Reputation: 81
  • View blog
  • Posts: 1,022
  • Joined: 22-February 08


Dream Kudos: 350

Re: student name & id with linked list

Posted 14 April 2009 - 04:36 PM

Are you supposed to be using structures or classes?
Was This Post Helpful? 0
  • +
  • -

#3 BlakeJustBlake  Icon User is offline

  • D.I.C Regular
  • Icon

Reputation: 23
  • View blog
  • Posts: 441
  • Joined: 15-February 09


Dream Kudos: 75

Re: student name & id with linked list

Posted 14 April 2009 - 04:51 PM

I'm not going to lie, there is quite a bit that would lead this to not work. First of all, the first while statement has a semicolon at the end, keeping everything inside its scope from being executed.

Then, if you enter 1 then there's going to be an infinite loop, so I'm hoping that's just a place holder for future code. Then, in case 2, you break before you move onto temp->nxt, and even if you do move on to that, then you'll get a Segmentation Fault because you haven't declared a nxt yet.
Was This Post Helpful? 0
  • +
  • -

#4 anya_ritika  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 16-March 08


Dream Kudos: 0

Re: student name & id with linked list

Posted 15 April 2009 - 12:43 AM

i have made some changes to my code...and now it somewhat works...however, there are a few more things it should do...
this is my code..
include <iostream>
#include <cstdlib>

using namespace std;

struct node
  {  char name[20];	// Name of up to 20 letters
	 int id;		  // D.O.B. would be better
	 //float height;	 // In metres
	 node *nxt;		// Pointer to next node
  };
int main()
{
node *start_ptr = NULL;

node * temp = new node;
if (start_ptr == NULL)
start_ptr = temp;
	
temp = start_ptr;
int n;
cout<<"enter:";
cin>>n;

if (temp == NULL)
{cout << "End of list" << endl;
return 1;
}
switch (n){
		  case 1:	  cout<<"Name : something"<<"  "<<"id: 1"<<endl;break;
		  case 2:	  cout << "Name : ";
					   cin>>temp->name;
					   cout << "id : ";
					   cin>>temp->id;
		  //cout << "Height : ";
		  //cin>>temp->height;
		  cout << endl;	   // Blank line

		  // Move to next node (if present)
		  temp = temp->nxt;break;
		  case 3:		  return 1;
	   
  }

system ("pause");

return 0;
}


i m supposed to do it using structures not classes.
but the code should also be doing the following.

1)Implement an option to display current value (ID and Address) only.
20Implement an option to allow the user to move the current point to the next value and display it. (If next value is NULL, reset the point to the head)
3)Implement a search function by entering ID, if ID is founded, display the Name or otherwise, “record not found” will be displayed#.
4)Implement a search function by entering Name, if Name is founded, display the ID or otherwise, “record not found” will be displayed.
5)Implement an add function to allow a user to add a value next to the current value. (ID is equal to current ID + 1)
6)Implement an add function to allow a user to add a value at the head of the listed. (ID is equal to 0)
Was This Post Helpful? 0
  • +
  • -

#5 ashishshevale  Icon User is offline

  • D.I.C Head
  • PipPip

Reputation: 3
  • View blog
  • Posts: 92
  • Joined: 13-April 09


Dream Kudos: 0

Re: student name & id with linked list

Posted 15 April 2009 - 01:01 AM

View Postanya_ritika, on 15 Apr, 2009 - 12:43 AM, said:

i have made some changes to my code...and now it somewhat works...however, there are a few more things it should do...
this is my code..
include <iostream>
#include <cstdlib>

using namespace std;

struct node
  {  char name[20];	// Name of up to 20 letters
	 int id;		  // D.O.B. would be better
	 //float height;	 // In metres
	 node *nxt;		// Pointer to next node
  };
int main()
{
node *start_ptr = NULL;

node * temp = new node;
if (start_ptr == NULL)
start_ptr = temp;
	
temp = start_ptr;
int n;
cout<<"enter:";
cin>>n;

if (temp == NULL)
{cout << "End of list" << endl;
return 1;
}
switch (n){
		  case 1:	  cout<<"Name : something"<<"  "<<"id: 1"<<endl;break;
		  case 2:	  cout << "Name : ";
					   cin>>temp->name;
					   cout << "id : ";
					   cin>>temp->id;
		  //cout << "Height : ";
		  //cin>>temp->height;
		  cout << endl;	   // Blank line

		  // Move to next node (if present)
		  temp = temp->nxt;break;
		  case 3:		  return 1;
	   
  }

system ("pause");

return 0;
}


i m supposed to do it using structures not classes.
but the code should also be doing the following.

1)Implement an option to display current value (ID and Address) only.
20Implement an option to allow the user to move the current point to the next value and display it. (If next value is NULL, reset the point to the head)
3)Implement a search function by entering ID, if ID is founded, display the Name or otherwise, “record not found” will be displayed#.
4)Implement a search function by entering Name, if Name is founded, display the ID or otherwise, “record not found” will be displayed.
5)Implement an add function to allow a user to add a value next to the current value. (ID is equal to current ID + 1)
6)Implement an add function to allow a user to add a value at the head of the listed. (ID is equal to 0)

your code to add a new node is fine
you have not added anything to check for last node like when you say
temp = temp->nxt;

then come out of switch and check what is temp
also maintain next pointer to null when you add new
i didnt understand your exect problem as you always said its working a bit
be more clear
Was This Post Helpful? 0
  • +
  • -

#6 anya_ritika  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 16-March 08


Dream Kudos: 0

Re: student name & id with linked list

Posted 15 April 2009 - 03:06 AM

using structure and linked list my code is supposed to be doing all these....
1)Implement an option to display current value (ID and name) only.
2)Implement an option to allow the user to move the current point to the next value and display it. (If next value is NULL, reset the point to the head)
3)Implement a search function by entering ID, if ID is founded, display the Name or otherwise, “"record not found"” will be displayed.
4)Implement a search function by entering Name, if Name is founded, display the ID or otherwise, “"record not found"” will be displayed.
5)Implement an add function to allow a user to add a value next to the current value. (ID is equal to current ID + 1)
6)Implement an add function to allow a user to add a value at the head of the listed. (ID is equal to 0)

i dont understand every bit of the instruction, but this is my improved code(with few changes).
#include <iostream>
#include <cstdlib>

using namespace std;

struct node
  {  char name[20];	// Name of up to 20 letters
	 int id;		 
		 node *nxt;		// Pointer to next node
  };
int main()
{
node *start_ptr = NULL;

node * temp = new node;
if (start_ptr == NULL)
start_ptr = temp;
	
temp = start_ptr;
int n;
cout<<"enter:";
cin>>n;
int id=1;

if (temp == NULL)
{cout << "End of list" << endl;
return 1;
}
switch (n){
		  case 1:	  cout<<"Name : something  id:"<<id<<endl;break;
		  case 2:	  cout << "Name : ";
					   cin>>temp->name;
					   cout << "id : "<<id+1<<endl;
					   //cin>>temp->id;
				   cout << endl;	   // Blank line

		  // Move to next node (if present)
		  temp = temp->nxt;break;
		  case 3:		  return 1;
	   
  }

system ("pause");

return 0;
}


my code only fulfills these criteria--
---if user inputs 1, then code has to list the student #include <iostream>
name and id already in the code.
i---f user inputs 2, then the code should allow the user to add student name to the linked list and id will be automaticallly inserted as (previous id+1).
---if usre inputs 3, response should be exit the code.
but rest it doesnt do...
so please help me with the other criterias listed at the top of this reply...

This post has been edited by anya_ritika: 15 April 2009 - 03:09 AM

Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is online

  • Dreaming Coder
  • Icon

Reputation: 1109
  • View blog
  • Posts: 5,813
  • Joined: 16-October 07


Dream Kudos: 600

Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

Re: student name & id with linked list

Posted 15 April 2009 - 03:21 AM

I'd use some functions. Really, functions are your friends. I'd also make another struct.

Here's the idea:

struct List {
	node *top;
	node *current;
};

//1)Implement an option to display current value (ID and Address) only.
void show(node *item) { /* your code here */ }

// not in your specs, but will be useful
void moveToTop(List &list) { list.top = list.current; }

//2)Implement an option to allow the user to move the current point to the next value and display it. (If next value is NULL, reset the point to the head)
void move(List &list) {
	if (list.top==NULL) { return; }
	if (list.current==NULL || list.current->next==NULL) { 
		moveToTop(list);
	} else {
		// the actual move
		list.current = list.current->next;
		show(list.current);
	}
}

//3)Implement a search function by entering ID, if ID is founded, display the Name or otherwise, "record not found" will be displayed#.
void searchId(List &list, int id) { /* your code here */ }

//4)Implement a search function by entering Name, if Name is founded, display the ID or otherwise, "record not found" will be displayed.
void searchId(List &list, char *name) { /* your code here */ }

// 5 & 6 are a little confusing.  It would appear that id is assigned by the program and not user input

//5)Implement an add function to allow a user to add a value next to the current value. (ID is equal to current ID + 1)
// adding at the end is wasteful.  This will probably be your hardest function.
void addTail(List &list, char *name) { /* your code here */ }

//6)Implement an add function to allow a user to add a value at the head of the listed. (ID is equal to 0)
// standard add
void add(List &list, char *name) { /* your code here */ }



Good luck.
Was This Post Helpful? 0
  • +
  • -

#8 David W  Icon User is offline

  • DIC supporter
  • Icon

Reputation: 130
  • Posts: 1,013
  • Joined: 20-September 08


Dream Kudos: 1075

Re: student name & id with linked list

Posted 15 April 2009 - 03:31 AM

Or ... to keep it simple, this may give you some ideas and a working start ...

#include <iostream>
#include <string>

using namespace std;

struct Node
{
    string name;
    int id;
    Node *next;
};

typedef Node* pNode;

void push_back(pNode& head, pNode& rear, int new_id, string new_name)
{
    pNode pNew = new Node;
    pNew->id = new_id;
    pNew->name = new_name;
    pNew->next = NULL;

    if(head == NULL)
        head = rear = pNew;
    else
    {
        rear->next = pNew;
        rear = pNew;
    }
}

void display_all(pNode front)
{
    while(front)
    {
        cout << front->id << " " << front->name << endl;
        front = front->next;
    }
}


void delete_all(pNode& front, pNode& back, int& size)
{
    while(front)
    {
        back  = front; // using 'back' for 'copy' of the moving 'front' address
        front = front->next; // Note: when done ... front = NULL
        delete( back ); // ok ... delete the memory 'pointed to' ...
    }
    back = NULL;
    size = 0;
}



int main()
{
    pNode front = NULL, rear = NULL;
    int size = 0;
    
    push_back(front, rear, ++size, "Susan");
    push_back(front, rear, ++size, "Sarah");
    push_back(front, rear, ++size, "Karry");
    display_all(front);
    cout << endl;

    push_back(front, rear, ++size, "Joe");
    display_all(front);
    cout << endl;

    delete_all(front, rear, size);

    push_back(front, rear, ++size, "Moe");
    push_back(front, rear, ++size, "Bob");
    push_back(front, rear, ++size, "Pam");
    display_all(front);
    
    delete_all(front, rear, size);
    display_all(front);
    
    cout << "\nPress 'Enter' to continue ... " << flush;
    cin.sync();
    cin.get();
}



Shalom,

David
http://developers-he.../index.p...opic,127.0.html

This post has been edited by David W: 15 April 2009 - 08:34 AM

Was This Post Helpful? 0
  • +
  • -

#9 baavgai  Icon User is online

  • Dreaming Coder
  • Icon

Reputation: 1109
  • View blog
  • Posts: 5,813
  • Joined: 16-October 07


Dream Kudos: 600

Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

Re: student name & id with linked list

Posted 15 April 2009 - 03:55 AM

View PostDavid W, on 15 Apr, 2009 - 05:31 AM, said:

Or ... to keep it simple, this may give you some ideas and a working start ...


Yes, because giving some canned code is always simpler than actually considering the question. :P

// I'd agree with capitalizing the struct name
struct Node {
    string name; // user wanted a char array
    int id;
    Node *next;
};

// obfuscating a pointer, always a bad idea
typedef Node* pNode;

// next_num is simply wrong.
// The assignment want iteration over the list for id discovery.
// passing a reference to a pointer is the opposite of clarity
// maintaining a rear reference is also counter to the assignment
void push_back(pNode& head, pNode& rear, int next_num, string new_name)

//...
// nowhere is the entire list requested to be printed
void display(pNode head) { while(head)




To the OP, take what you can from code offered, but leave the rest. Particularly, using something like "typedef Node* pNode" will only cause confusion and I'd avoid it at all costs.
Was This Post Helpful? 0
  • +
  • -

#10 David W  Icon User is offline

  • DIC supporter
  • Icon

Reputation: 130
  • Posts: 1,013
  • Joined: 20-September 08


Dream Kudos: 1075

Re: student name & id with linked list

Posted 15 April 2009 - 05:46 AM

typedef is great if one can read a program 'top down' ...

Programming is much about defining new things,

like functions,
classes,
types,
structs,
#include "helper.h",
etc ...

to aid smooth program logic flow ...

And whatever helps to make a program flow more smoothly ... including good type names ... is highly recommended ... no?

Shalom,
David

P.S.
Check out the style of HLA fame, Prof. Randall Hyde, via the link here ...
http://developers-he...index.php/topic,46.0.html

This post has been edited by David W: 15 April 2009 - 06:15 AM

Was This Post Helpful? 0
  • +
  • -

#11 baavgai  Icon User is online

  • Dreaming Coder
  • Icon

Reputation: 1109
  • View blog
  • Posts: 5,813
  • Joined: 16-October 07


Dream Kudos: 600

Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

Re: student name & id with linked list

Posted 15 April 2009 - 06:23 AM

View PostDavid W, on 15 Apr, 2009 - 07:46 AM, said:

typedef is great if one can read a program 'top down' ...

Programming is much about defining new things,
...
And whatever helps to make a program flow more smoothly ... including good type names ... is highly recommended ... no?


In general, I'd agree with this. I also recommend typedef is most cases. However, using a typedef to hide pointer and reference directives I would not recommend.

Additionally, if the student is trying to learn pointers, then hiding the pointer syntax is probably not helping. In C++, the unholy fusion of reference and pointer syntax is bad enough, offering a hidden version of "Node *&" may not be the best approach.

This post has been edited by baavgai: 15 April 2009 - 06:24 AM

Was This Post Helpful? 0
  • +
  • -

#12 David W  Icon User is offline

  • DIC supporter
  • Icon

Reputation: 130
  • Posts: 1,013
  • Joined: 20-September 08


Dream Kudos: 1075

Re: student name & id with linked list

Posted 15 April 2009 - 07:03 AM

Quote

...Additionally, if the student is trying to learn pointers, then hiding the pointer syntax is probably not helping. In C++, the unholy fusion of reference and pointer syntax is bad enough, offering a hidden version of "Node *&" may not be the best approach.


Yes ... that is scary looking syntax ... Node*& ...

But then ... why not smooth it up ... with typedef ?
typedef Node* pNode;

void do_something( pNode& pHead );


Now ... all that smooth stuff, of C++ passing by reference, just flows ...

Many beginning C++ students can use C++ passing by reference quite comfortably ... and this first use (of hidden pointers) seems to be a preferred approach these days ... and then to learn about pointers next ... when needed ... as is the case with linked-lists.

Learning about pointers might be eased considerably, by being exposed some, to a teaching language like HLA, that was carefully designed especially for students. Then, C and C++ pointers would not be as problematic as they often are to CS students.

Shalom,
David

P.S.

One of the first things, like day 2 or 3, in HLA is that (HLA) 'strings are pointers' ... and so from get-go ... you are using pointers all the time you are using strings ... and the HLA string library is VERY extensive ... and HLA can be used with C or C++ (and much of the HLA syntax is like C/C++ syntax) ... so even half serious CS students will be glad to spend some time with HLA.

This post has been edited by David W: 15 April 2009 - 07:50 AM

Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users