1

Details-

I am taking input of n students

Their number- rollno./mobile/UID in union, their full name, their course name, their age, their branch name.

I am using a linked list to store the data in a sorted manner. As soon as I insert a node, the list becomes sorted simultaneously. In the end, I print the whole list. The list is sorted by age of the candidates.

Can someone please help me why am I getting a runtime error while taking the string input in this program. What should I correct so that the program does not give a runtime error. Thanks in advance.

#include<bits/stdc++.h>
using namespace std;
typedef struct Node node;
struct Node
{
    union number
    {
        string roll;
        long long int mobile;
        string other;
    }id;
    string name;
    string course;
    int age;
    string branch;
    node *next;
    int type;
};
node *head=NULL;
int main()
{
    int n;
    cout<<"Enter the number of students: ";
    cin>>n;
    char ch;
    string roll;
    for(int i=1;i<=n;i++)
    {
        node *temp=(node*)(malloc(sizeof(node)));
        cin>>ch;
        if(ch=='R')
        {
            cin>>temp->id.roll;
            temp->type=0;
        }
        else if(ch=='M')
        {
            cin>>temp->id.mobile;
            temp->type=1;
        }
        else if(ch=='O')
        {
            cin>>temp->id.other;
            temp->type=2;
        }
        getline(cin,temp->name);
        getline(cin,temp->course);
        cin>>temp->age;
        getline(cin,temp->branch);

        if(head==NULL)
        {
            head=temp;
            head->next=NULL;
            continue;
        }
        if(head->age>=temp->age)
        {
            temp->next=head;
            head=temp;
            continue;
        }
        node *ptr;
        ptr=head;
        while((ptr->next)!=NULL&&(ptr->next)->age<temp->age)
        {
            ptr=ptr->next;
        }
        temp->next=ptr->next;
        ptr->next=temp;
    }
    node *ptr;
    ptr=head;
    while(ptr!=NULL)
    {
        if(ptr->type==0)
        {
            cout<<ptr->id.roll<<",";
        }
        else if(ptr->type==1)
        {
            cout<<ptr->id.mobile<<",";
        }
        else if(ptr->type==2)
        {
            cout<<ptr->id.other<<",";
        }
        cout<<ptr->name<<","<<ptr->course<<","<<ptr->age<<","<<ptr->branch<<endl;
        ptr=ptr->next;
    }
}

1 Answer 1

5

The first thing you should do is replace

   node *temp=(node*)(malloc(sizeof(node)));

with

   node *temp=new node;

Don't use malloc in a C++ program. The reason is that malloc does not call the constructors for the objects it allocates. std::string has a constructor and this must be called or you will get errors.

The second thing you should do is replace the union. When objects have constructors it difficult to place them in a union. The reason is that only one object in the union is alive at any one time, so which object has been constructed? C++ allows you to place objects with constructors in unions but then you must handle the construction of such objects yourself. This is quite an advanced topic, and my advice would be to simply delete the union and place roll, mobile and other directly in the node struct. But if you want to do some research you should look into std::variant, this is a replacement for a union, or look into placement new, this is the technique for manually constructing objects.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot :), but in the question i was asked to do with union, that's why i was having a doubt. If I use a char array of fixed size say 20 instead of string , it gives the correct answer but then i cannot take string as input with spaces in between. Only a string with no space is working.
@aman I think you must have missed the point of my answer. As I explained, with a union you have to manage the construction of the objects in the union yourself. A char array doesn't have a constructor but a string does. So by putting a string in a union you give yourself extra work to do. You can make it work, but you would need to use the placement new technique I mentioned. An easier option would be to use std::variant. An even easier option would be to not use a union in the first place.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.