I'm working on a project for a Student Course Registration System. I'm having problems reading data from a text file and storing it in singly linked list, which has to get updated every time a new student is added. The data is stored in an formatted way. The problem is my struct has type char variables, so it gives me as Assignment Error.
The struct is defined as:
struct Student {
char stdID[10];
char stdName[30];
char stdSemester[5];
Student *next; } *Head, *Tail;
The code that saves the struct is:
// For Saving:
SFile << std->stdID << '\t' << std->stdName << '\t' << std->stdSemester << '\n';
The code to read the text file and display the struct is:
// Display:
system("cls");
cout << "\n\n\n";
cout << "\t\t\t\t LIST OF COURSES" << endl;
cout << "\t\t\t ====================================================\n" << endl;
cout << "\t" << "ID" << "\t" << setw(15) << "Course Name" << "\n\n";
// Initialize:
char ID[10];
char Name[30];
char Sem[5];
ifstream SFile("StudentRecord.txt");
Student *Temp = NULL;
while(!SFile.eof()) {
// Get:
SFile.getline(ID, 10, '\t');
SFile.getline(Name, 30, '\t');
SFile.getline(Sem, 5, '\t');
Student *Std = new Student; //<======== OUCH! Assignment error here
//node*c=new node;
// Assign:
Std->stdID = *ID;
if (Head == NULL) {
Head = Std;
}
else {
Temp = Head;
{
while ( Temp->next !=NULL ) {
Temp=Temp->next;
}
Temp->next = Std;
}
}
}
SFile.close();
system("pause"); }
P.S: I'm Having Problem at Assign Comment;
Will I have to change data type and make the whole project in string? I preferred char because I was able to format the output, and in string i'm sure it reads line by line, so I wont be able stores values from single line.
To use strings ?
If IDs would be
std:string, you could do:And you could use
std::getline():You wouldn't have to worry about maximum length, but you could still decide to check the lngth of the string and shorten it if necessary.
Or not to use strings ?
But if you prefer (or have to) to use
char[]instead, then you need to use thestrncpy()for doing assignments :Honnestly, in the 21st century, I'd go for std::string, and not stick to the old
char[]that date back to the 70s...File loop
It's unrelated, but you should never loop on
eof:Why ? Look here for more explanations
And by the way, your last
getline()should certainly look for'\n'as delimiter, according to your writing function.