-2

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.

Christophe
  • 54,708
  • 5
  • 52
  • 107
Vikesyy
  • 19
  • 4
  • 2
    `while(!SFile.eof()) {` https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – drescherjm Oct 22 '18 at 18:00
  • I assume you are not permitted to use `std::string`? – drescherjm Oct 22 '18 at 18:00
  • `he Problem is my Struct has type "char" variables, so it gives me as Assignment Error` What is an Assignment error? – drescherjm Oct 22 '18 at 18:01
  • 1
    ***I preferred char because I was able to format the output,*** char should rarely be preferred over `std::string`. Your program would be simpler if you used `std::string`. – drescherjm Oct 22 '18 at 18:02
  • 1
    @drescherjm `Std->stdID = *ID;` is an "assign error". – Swordfish Oct 22 '18 at 18:03
  • @Vikesyy "The Problem is my Struct has type "char" variables" No, it has not. It has members of type *array of `char`* – Swordfish Oct 22 '18 at 18:04
  • 1
    @Vikesyy "and in String i'm sure it reads line by line, so I wont be able stores values from single line." you can use `std::string`s much the same way with `getline()` and a delimiter as you do now with `char` arrays. – Swordfish Oct 22 '18 at 18:06
  • @Swordfish Yes, that is what I meant sorry. Do you think I'll have to change the members to String in order to go further on? – Vikesyy Oct 22 '18 at 18:07
  • 1
    @Vikesyy You don't ***have to***, but it would make your life easier. – Swordfish Oct 22 '18 at 18:08
  • You could use a c-string function to copy the c-string. Although with `std::string` the assignment with `=` would just work.. – drescherjm Oct 22 '18 at 18:09
  • @Vikeyy: With "`Std->stdID = *ID;`" you are trying to assign a `char` (`*ID (the same as `ID[0]`)) to an *array of `char`*. This won't work because arrays dont have value semantics in C++. You'd have to use `std::strcpy()` to copy `ID` to `Std->stdID`. – Swordfish Oct 22 '18 at 18:11
  • @Swordfish So I'll be able to achieve it by the following; getline(cin, ID, "\t"); ? – Vikesyy Oct 22 '18 at 18:11
  • Okay Thank You @Swordfish this was really helpful, I'll research a bit. – Vikesyy Oct 22 '18 at 18:13
  • @drescherjm Thank you! – Vikesyy Oct 22 '18 at 18:13
  • @Vikesyy `std::getline(the_ifstream_you_want_to_read_from, your_std_string, '\t');` – Swordfish Oct 22 '18 at 18:14

1 Answers1

1

To use strings ?

If IDs would be std:string, you could do:

Std->stdID = ID;

And you could use std::getline():

getline(SFile, ID, '\t');

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 the strncpy() for doing assignments :

strncpy( Std->stdID, ID, 10 );  // Std->stdID = *ID;

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:

while (SFile.getline(ID, 10, '\t') 
     && SFile.getline(Name, 30, '\t')  && SFile.getline(Sem, 5, '\n') {
   ...
}

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.

Christophe
  • 54,708
  • 5
  • 52
  • 107