Issue with linkedlist program, embarrasingly elusive!

This linked list works fine if i am using just first-names, but it messes up whenever I try including surnames. what do you think is the matter?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <string>
#include <sstream>

using namespace std;

class student
{
	public:
		student(char* pN = "No Name", int ssid = 0)
		{
			name = new char[strlen(pN)+1];
			if (name)
			{
				strcpy(name,pN);
			}
			Id = ssid;
		}
		student(student& s)
		{
			name = new char[strlen(s.name)+1];
			if (name)
			{
				strcpy(name,s.name);
			}
			Id = s.Id;
		}
		student& operator=(student& s)
		{
			deletename();
			name = new char[strlen(s.name)+1];
			if (name)
			{
				strcpy(name,s.name);
			}
			Id = s.Id;
		}
		~student()
		{
			deletename();
		}
		void deletename()
		{
			if (name)
			{
				delete [] name;
				name = 0;
			}
			return;
		}
		string display();
		student* pNext;
	protected:
		char* name;
		int Id;
};
//outlined student method
string student::display()
{
	ostringstream out;
	out<<name<<" - "<<Id<<endl;
	return out.str();
}
student* pHead = (student*)0;
//adds students to a list
void addobject(student* pS)
{
	pS->pNext = pHead;
	pHead = pS;
	return;
}
//gets the name and id of each student object
student* getData()
{
	char nam[128]; int ssd;
	cout<<"Enter the name: ";
	cin>>nam;
	//cin.getline(nam,128);
	cout<<"Enter the ID: ";
	cin>>ssd;
	if (stricmp(nam,"exit") == 0 && ssd == 0 )
	{
		return 0;
	}
	student* pS = new student(nam,ssd);
	pS->pNext = (student*)0;
	return pS;
}
int main(int nNumberofArgs, char* pszArgs[])
{
	student* pS = new student;
	while (pS = getData())
	{
		addobject(pS);
	}
	pS = pHead;
	//prints the list in reverse order
	for (int i=1; pS!=0; i++)
	{
		cout<<i<<" - "<<pS->display()<<endl;
		pS = pS->pNext;
	}
	//wait for the user to be ready before terminating the program
	system("PAUSE");
	return 0;
}
closed account (iGLbpfjN)
In line 80, use cin.getline(nam, 128); instead of cin >> nam;
After line 83 use cin.ignore(); (flush the input buffer)
Last edited on
@omni

thanks, it worked but why did i need to flush the buffer and why didnt cin>>nam; work?
Last edited on
why didnt cin>>nam; work?

The extraction operator stops processing the line when it encounters a white space character.

it worked but why did i need to flush the buffer

This is being caused because you are mixing the extraction operator and getline(). The extraction operator leaves the end of line character in the input buffer and getline(), by default, stops processing when it encounters the end of line character.

@jlb

sorry, do u mean the other way round because now, i m using the getline() before the extracter i.e does the getline() leave an end of line character in the input buffer which the extracter operator sees and starts to misbehave ?
i.e does the getline() leave an end of line character in the input buffer

No, getline() extracts and discards the delimiter.

Post the code that you're now having problems with and maybe someone can identify the problem.

By the way why are you using all those C-strings? You seem to be aware of C++ strings why not use them, they are much safer and don't require the nasty manual memory management you're trying to use.

Topic archived. No new replies allowed.