I was making a multi linked list program, but my function for enrolling to a course is not working. When I use cin
, it takes only course name and doesn't take course instructor name as input, and when I use getline()
function, it doesn't accept input of both of them. Why it is behaving like this?
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iostream> //for cin and cout
using namespace std;
struct course
{
string cName;
string cInst;
int cNo;
struct course* next;
struct student* start;
} *Cstart = NULL;
struct student
{
string sName;
int sNo;
int Class;
struct student* link;
};
void EnrollCourse()
{
struct course* temp;
temp = (struct course*) malloc(sizeof(struct course));
printf("Enter your Course No :");
scanf("%d", &temp->cNo);
printf("\nEnter your Course Name: "); //getline didn't work on it
getline(cin, temp->cName);
cout << temp->cName;
printf("\nEnter your Instructor Name: ");
getline(cin, temp->cInst);
cout << temp->cInst;
temp->next = NULL;
temp->start = NULL;
if (Cstart == NULL)
{
Cstart = temp;
}
else
{
struct course* curr = Cstart;
while (curr->next != NULL)
{
curr = curr->next;
}
curr->next=temp;
}
}
int main()
{
EnrollCourse();
EnrollCourse();
EnrollCourse();
EnrollCourse();
return 0;
}