0

Here in the following code which is a linked list for taking student placement information here while using display function the first character of string variables are skipped I am not understanding why it is happening Following is the code:

#include<iostream>
using namespace std;

class node
{
    public:
        
        string stud_name;
        string stud_branch;
        string stud_coll;
        string post_name;
        float stud_pac;
        long long int stud_con;
        node* next; 
};

class list
{
    public:
        node* head;
    
    list() //Default constructor
    {
        head=NULL;
    }

void create();
void display();
};

void list :: create()
{
    node *temp,*ptr;
    int add=0;
    
    do
    {
        temp=new node;
        cout<<"\nEnter name of Student hired :- ";
        cin.ignore();
        getline(cin,temp->stud_name);
        cout<<"Branch of the Student hired :- ";
        cin.ignore();
        getline(cin,temp->stud_branch);
        cout<<"Enter the name of the Institute of hired Student :- ";
        cin.ignore();
        getline(cin,temp->stud_coll);
        cout<<"Enter the post at which student is hired :- ";
        cin.ignore();
        getline(cin,temp->post_name);
        cout<<"Enter the Package given to the hired student(in LPA) :- ";
        cin>>temp->stud_pac;
        cout<<"Enter contact number of the Student hired :- ";
        cin>>temp->stud_con;    
        temp->next=NULL;

        
        if(head==NULL)
        {
            head=temp;
        }
        
        else
        {
            ptr=head;
            while(ptr->next!=NULL)
            {
                ptr=ptr->next;
                
            }
            ptr->next=temp;
        }       
        cout<<"Type 1 to Insert more records :- ";
        cin>>add;
        
    }while(add==1);
    
}


void list :: display()
{
    node *ptr=head;
    cout<<"\nHired Students details are as follows :- \n";
    
    while(ptr!=NULL)
    {
        cout<<"\nName of student :- "<<ptr->stud_name;
        cout<<"\nBranch of student :- "<<ptr->stud_branch;
        cout<<"\nName of Institute :- "<<ptr->stud_coll;
        cout<<"\nName of Hired Post :- "<<ptr->post_name;
        cout<<"\nPackage given(in LPA) :- "<<ptr->stud_pac;
        cout<<"\nContact Number :- "<<ptr->stud_con<<"\n";
        ptr=ptr->next;
    }
}

int main()
{
    list l1;
    l1.create();
    l1.display();
    return 0;
}

Following is the output:

PS E:\Programming\C++> cd "e:\Programming\C++\" ; if ($?) { g++ Trial.cpp -o Trial } ; if ($?) { .\Trial }

Enter name of Student hired :- ABC EFG
Branch of the Student hired :- Computer Engineering
Enter the name of the Institute of hired Student :- XYZ College
Enter the post at which student is hired :- Software Developer
Enter the Package given to the hired student(in LPA) :- 5.6
Enter contact number of the Student hired :- 4152637485
Type 1 to Insert more records :- 1

Enter name of Student hired :- XYZ UVW
Branch of the Student hired :- Mechanical Engineering
Enter the name of the Institute of hired Student :-  ABC College
Enter the post at which student is hired :- Manager
Enter the Package given to the hired student(in LPA) :- 3
Enter contact number of the Student hired :- 9865327441
Type 1 to Insert more records :- 0

Hired Students details are as follows :- 

Name of student :- BC EFG
Branch of student :- omputer Engineering 
Name of Institute :- YZ College
Name of Hired Post :- oftware Developer  
Package given(in LPA) :- 5.6
Contact Number :- 4152637485

Name of student :- XYZ UVW
Branch of student :- echanical Engineering
Name of Institute :- ABC College
Name of Hired Post :- anager
Package given(in LPA) :- 3
Contact Number :- 9865327441
PS E:\Programming\C++> 

Please tell me why this problem is occuring and how to solve this

Anon2002
  • 3
  • 3
  • 1
    you're throwing away the first character of every input with `cin.ignore()`, you only need to use `cin.ignore()` once when switching between formatted input and `std::getline` – Alan Birtles Oct 22 '21 at 15:04
  • Please extract and provide a [mcve]. Also, take the [tour] and read [ask]. – Ulrich Eckhardt Oct 22 '21 at 15:05

1 Answers1

2

It doesn't contain the first character because you explicitly ignore the first character:

cin.ignore();
getline(cin, temp->stud_name);

This pattern is being repeated for your other inputs, too.

You only need ignore() when switching from formatted input (stream >> variable) to unformatted input (like getline()). So, put the ignore() after your cin >> variable (formatted input) lines instead:

// ...
cin >> temp->stud_con;
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108