Linked list: Head not displaying?

First member is always PRESIDENT. Last is always SECRETARY.
Rest are MEMBERS. Club cannot have more than 3 members.

INPUT (for 3 members):
1 a 1
2 a 2
3 a 3

OUTPUT shows 2 a 2 as PRESIDENT and 3 a 3 as SECRETARY. Where did 1 a 1 disappear?



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
  #include <iostream>
using namespace std;

struct stud
{
    int prn, yr;
    string name, pos;

    stud *next;
};

class Pinnacle
{
    public:
    stud *head;

    void takeUI();
    void showUI();

    Pinnacle()
    {
        head=NULL;
    }
};

void Pinnacle::takeUI()
{
    int n;
    cout<<"Enter no. of members: ";
    cin>>n;

    if(n<3)
    {
        cout<<"Minimum 3 members required.";
    }

    if(n>=3)
    {
        stud *temp;
        for(int i=0; i<n; i++)
        {
            temp= new stud;
            cout<<"\n";
            cout<<"Enter PRN, name, year: ";
            cin>>temp->prn>>temp->name>>temp->yr;
            temp->next=NULL;

            if(i==1)
            {
                head=temp;
                head->pos="PRESIDENT";
            }

            if(i==2)
            {
                head->next=temp;
                temp->pos="SECRETARY";
            }

            if(i>2)
            {
                temp->next=head->next;
                head->next=temp;
                temp->pos="MEMBER";
            }

        }

    }
}


void Pinnacle::showUI()
{


    if(head==NULL)
    {
        cout<<"Club is empty.";
    }

else
{
    stud*temp=head;

    while(temp!=NULL)
    {
        cout<<"PRN: "<<temp->prn<<endl;
        cout<<"NAME: "<<temp->name<<endl;
        cout<<"YEAR: "<<temp->yr<<endl;
        cout<<"("<<temp->pos<<")"<<endl<<endl;

    temp=temp->next;
    }
    }
}


int main()
{
    Pinnacle x;
    x.takeUI();
    x.showUI();
}




EDIT:
I just changed line 40:

 
for(int i=0; i<n; i++)


to

 
for(int i=1; i<=n; i++)


and it worked!

Aren't they the same conditions, though?
Last edited on
Aren't they the same conditions, though?

With regards to the loop repeating the same number of times, yes.
With regards to the code in the loop, no.
Topic archived. No new replies allowed.