jeudi 8 octobre 2020

Searching through csv file in c++? [duplicate]

I need to search from csv file, for the first input before comma per line and get it, but my program only reads first line, how can i make it to go through every line

My courseid and save are different, cus courseid point to the first line only and cant find a mistake

void Courses::viewStudentsByCourseID(){
    


    string id;
    cout<<"Insert a courseID to see list of students: ";
    cin>>id;
    ifstream infile;    
    string line;
    string save;

    CourseNode *ptr = head;
    while(ptr!=NULL)
    {   
        if (id==ptr->elem.CourseID) {
        save=ptr->elem.CourseID;
    }
    ptr=ptr->next;
    }
    
    string list_of_students;
    infile.open("course_students.csv");
    if(infile.is_open()){  
        while(getline(infile, line)) { 
            stringstream sst(line); 
            string courseid;
            getline(getline (sst,courseid,','),list_of_students, ',');
            cout<<courseid;
            cout<<save;
            if (courseid==save) {
                cout<<list_of_students;
            }
            else {
                cout<<"This course has no students listed or it is not exist in database";
            }
        


        }

    }
    
    
}

SOLVED!

Any help will be appreciated!

Aucun commentaire:

Enregistrer un commentaire