0

I've experienced a problem in correctly parsing and displaying my CSV file in the terminal in VS Code.

The Student Names and Roll Numbers are displayed correctly but not the rest of the entries. What might be the reason behind this? Moreover the next part of my project is to calculate the GPA using these entries, the program will take the input from the user about the weightage and apply the formula accordingly. How may I be able to achieve that?

I tried this:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

struct StudentRecord{
    public:
    StudentRecord(
        string studentnames,
        int rollnumbers,
        int mid,
        int final,
        int project
    ){
        StudentNames = studentnames;
        RollNumbers = rollnumbers;
        Mid = mid;
        Final = final;
        Project = project;
    }

    void displayInfo(){//method (function inside a struct)
        cout << "Student Name: " << StudentNames << endl;
        cout << "Roll Number: " << RollNumbers << endl;
        cout << "Mid: " << Mid << endl;
        cout << "Final: " << Final << endl;
        cout << "Project: " << Project << endl;
    }

    string StudentNames;
    int RollNumbers;
    int Mid;
    int Final;
    int Project;
};

void displayStudents(vector<StudentRecord>& students){
    for(auto student : students){
        student.displayInfo();
    }
}

int main(){
ifstream inputFile;
inputFile.open("C:\\Users\\hasan\\Desktop\\file_handling project\\NCA.csv");
string line = "";

vector<StudentRecord> students;

while(getline(inputFile, line)){
    stringstream inputString(line);

    string StudentNames;
    int RollNumbers;
    int Mid;
    int Final;
    int Project;
    string tempString;

    getline(inputString, StudentNames, ',');
    inputString >> RollNumbers;
    inputString >> Mid;
    inputString >> Final;
    inputString >> Project;

    StudentRecord student(StudentNames, RollNumbers,
     Mid, Final, Project);
students.push_back(student);
line = "";     
    
}

displayStudents(students);

return 0;
}

and this doesn't seem to work. It doesn't display the rest of the entries correctly apart from Student Name and Roll Numbers.

This is the CSV files I read from Notepad:

Student Names,Roll Numbers,Mid,Final,Project
,Total Out of,100,100,40
Muhammad Abdullah Khan,190536,65,54,29  
Abdullah Khan,210355,31,57,25  
Saram Rehman Qureshi,210764,80,59,1  
Hassam Ahsan,211216,97,44,22  
Safwan Sami,211243,30,86,16  
Huzaifa Munir Janjua,211264,42,37,6  
Muhammad Zeeshan Khan,212087,51,45,37  
Hamza Shoaib,212125,39,66,9  
Armaghan Hussain,221119,100,76,36  
Hira Nusrat,221123,82,55,1  
Areeba Gulzar,221125,77,54,14  
Rohma Binte Azeem,221127,80,70,33  
Sohail Zafar,221131,100,87,17  
Muhammad Majid Sajjad,221133,80,69,31  
Urwa Hassan,221135,100,93,30  
Humda Fatima,221137,83,34,19  
Shaheer Ahmad Sherazi,221139,83,39,1  
Abuzar Khan,221143,96,35,30  
Syed Muhammad Hamza,221145,72,31,16  
Abdul Rehman,221151,30,41,40  
Nimra Waheed,221153,59,92,28  
Talha Mehmood,221155,100,90,6  
Muhammad Obaidullah Aqil,221157,87,52,8  
Muhammad Nauman,221159,85,51,12  
Abdul Wasey,221161,79,47,27  
Syed Abdul Rafay,221165,78,49,34  
Maham Azam,221167,75,83,35  
Muhammad Abdur Rafay,221175,89,77,17  
Muhammad Haseeb Ullah,221179,73,41,22  
Huzaifa,221187,52,73,29  
Kehkshan Zafar,221195,81,68,30  
Hassan Siddique Malik,221199,40,88,16  
Muhammad Bilal,221203,34,43,13  
Muhammad Huzaifa Shahzad,221207,57,77,3  
Wasi Haider,221209,62,99,37  
Muhammad Waqar Ashraf,221213,67,38,18  
Sarim Badar,221605,44,79,16  
Tehreem Khan,221643,91,73,23  
Maher Shuja,221698,55,79,20  
Abdul Ahad,221725,56,52,19  
Taqi Faza,221752,35,41,27  
Ayesha Ansar,222584,96,88,34  

This is the output I receive in the terminal, the expect the same output but with the correct entries:

Student Name: Student Names
Roll Number: 0
Mid: 0
Final: 212
Project: -1300236016       
Student Name: 
Roll Number: 0
Mid: 0
Final: 212
Project: -1300236016
Student Name: Muhammad Abdullah Khan
Roll Number: 190536
Mid: 0
Final: 212
Project: -1300236016
Student Name: Abdullah Khan
Roll Number: 210355
Mid: 0
Final: 212
Project: -1300236016
Student Name: Saram Rehman Qureshi
Roll Number: 210764
Mid: 0
Final: 212
Project: -1300236016
Student Name: Hassam Ahsan
Roll Number: 211216
Mid: 0
Final: 212
Project: -1300236016
Student Name: Safwan Sami
Roll Number: 211243
Mid: 0
Final: 212
Project: -1300236016
Student Name: Huzaifa Munir Janjua
Roll Number: 211264
Mid: 0
Final: 212
Project: -1300236016
Student Name: Muhammad Zeeshan Khan
Roll Number: 212087
Mid: 0
Final: 212
Project: -1300236016
Student Name: Hamza Shoaib
Roll Number: 212125
Mid: 0
Final: 212
Project: -1300236016
Student Name: Armaghan Hussain
Roll Number: 221119
Mid: 0
Final: 212
Project: -1300236016
Student Name: Hira Nusrat
Roll Number: 221123
Mid: 0
Final: 212
Project: -1300236016
Student Name: Areeba Gulzar
Roll Number: 221125
Mid: 0
Final: 212
Project: -1300236016
Student Name: Rohma Binte Azeem
Roll Number: 221127
Mid: 0
Final: 212
Project: -1300236016
Student Name: Sohail Zafar
Roll Number: 221131
Mid: 0
Final: 212
Project: -1300236016
Student Name: Muhammad Majid Sajjad
Roll Number: 221133
Mid: 0
Final: 212
Project: -1300236016
Student Name: Urwa Hassan
Roll Number: 221135
Mid: 0
Final: 212
Project: -1300236016
Student Name: Humda Fatima
Roll Number: 221137
Mid: 0
Final: 212
Project: -1300236016
Student Name: Shaheer Ahmad Sherazi
Roll Number: 221139
Mid: 0
Final: 212
Project: -1300236016
Student Name: Abuzar Khan
Roll Number: 221143
Mid: 0
Final: 212
Project: -1300236016
Student Name: Syed Muhammad Hamza
Roll Number: 221145
Mid: 0
Final: 212
Project: -1300236016
Student Name: Abdul Rehman
Roll Number: 221151
Mid: 0
Final: 212
Project: -1300236016
Student Name: Nimra Waheed
Roll Number: 221153
Mid: 0
Final: 212
Project: -1300236016
Student Name: Talha Mehmood
Roll Number: 221155
Mid: 0
Final: 212
Project: -1300236016
Student Name: Muhammad Obaidullah Aqil
Roll Number: 221157
Mid: 0
Final: 212
Project: -1300236016
Student Name: Muhammad Nauman
Roll Number: 221159
Mid: 0
Final: 212
Project: -1300236016
Student Name: Abdul Wasey
Roll Number: 221161
Mid: 0
Final: 212
Project: -1300236016
Student Name: Syed Abdul Rafay
Roll Number: 221165
Mid: 0
Final: 212
Project: -1300236016
Student Name: Maham Azam
Roll Number: 221167
Mid: 0
Final: 212
Project: -1300236016
Student Name: Muhammad Abdur Rafay
Roll Number: 221175
Mid: 0
Final: 212
Project: -1300236016
Student Name: Muhammad Haseeb Ullah
Roll Number: 221179
Mid: 0
Final: 212
Project: -1300236016
Student Name: Huzaifa
Roll Number: 221187
Mid: 0
Final: 212
Project: -1300236016
Student Name: Kehkshan Zafar
Roll Number: 221195
Mid: 0
Final: 212
Project: -1300236016
Student Name: Hassan Siddique Malik
Roll Number: 221199
Mid: 0
Final: 212
Project: -1300236016
Student Name: Muhammad Bilal
Roll Number: 221203
Mid: 0
Final: 212
Project: -1300236016
Student Name: Muhammad Huzaifa Shahzad
Roll Number: 221207
Mid: 0
Final: 212
Project: -1300236016
Student Name: Wasi Haider
Roll Number: 221209
Mid: 0
Final: 212
Project: -1300236016
Student Name: Muhammad Waqar Ashraf
Roll Number: 221213
Mid: 0
Final: 212
Project: -1300236016
Student Name: Sarim Badar
Roll Number: 221605
Mid: 0
Final: 212
Project: -1300236016
Student Name: Tehreem Khan
Roll Number: 221643
Mid: 0
Final: 212
Project: -1300236016
Student Name: Maher Shuja
Roll Number: 221698
Mid: 0
Final: 212
Project: -1300236016
Student Name: Abdul Ahad
Roll Number: 221725
Mid: 0
Final: 212
Project: -1300236016
Student Name: Taqi Faza
Roll Number: 221752
Mid: 0
Final: 212
Project: -1300236016
Student Name: Ayesha Ansar
Roll Number: 222584
Mid: 0
Final: 212
Project: -1300236016
7
  • What is the expected output and how does is differ from current output? Post the csv file content as is also. We need a minimal reproducible example that includes all other resources(like the csv file) needed to reproduce the problem. Commented Dec 27, 2023 at 7:30
  • I've edited the post and added the csv. file and the output I receive. Commented Dec 27, 2023 at 7:44
  • 1
    When you read the data, you're not doing anything to skip over the commas between the numbers, so when you try to read another number, but there's a comma waiting to be read, the stream get out of sync with what you're expecting to read, and things kind of fall apart. Commented Dec 27, 2023 at 8:09
  • @Jerry Coffin beat me to the punch. inputString >> RollNumbers; leaves a trailing comma on the input stream. You can read it with char c; inputString >> RollNumbers >> c; Do a similar thing for the other integer fields, except for the last, which does not have a trailing comma. Commented Dec 27, 2023 at 8:20
  • The first two lines of the file, which contains field names and other data, are another problem. You need to read those outside the loop, at the top. Commented Dec 27, 2023 at 8:27

1 Answer 1

2

Student Names and Roll Numbers are displayed correctly but not the rest of the entries. What might be the reason behind this?

There is a comma after RollNumbers that your program is not reading. It remains in the stream, and is the first character encountered when you attempt to read Mid (another int). Thus, the extraction of Mid fails, and the stringstream inputString is placed into a failed state. After that, all subsequent extractions from inputString fail, because it is in a failed state.

Fortunately, there is an easy solution. Declare a dummy variable of type char, and use it to read the commas.

        char c;
        inputString >> RollNumbers >> c;
        inputString >> Mid >> c;
        inputString >> Final >> c;
        inputString >> Project;  // No comma here!

Weighted score

The next part of my project is to calculate the GPA using these entries, the program will take the input from the user about the weightage and apply the formula accordingly. How may I be able to achieve that?

The first two lines of the input file contain field names and information about "perfect scores" for Mid, Final, and Project. You need to read that before the loop that reads student records.

The data file does not seem to have any weighting information, so prompt the user to enter that from std::cin.

Suppose your weights are:

double weightMid = 0.3;
double weightFinal = 0.4;
double weightProject = 0.3

Note that these add to 100%. That means that the highest score (calculated below), should be 100%.

Suppose further that perfect scores are stored in these variables:

double perfectMid, perfectFinal, perfectProject;

You read the values from the file. (They are, respectively, 100, 100, and 40.)

Then, the weighted score for a given student is:

double score = weightMid * Mid / perfectMid
    + weightFinal * Final / perfectFinal
    + weightProject * Project / perfectProject;

Side notes

  1. In struct StudentRecord, member function display_info should be declared const (after the parameter list).

  2. The loop in function displayStudents should use const&, i.e., it should be: for (auto const& student : students).

  3. Consider changing field RollNumbers from int to std::string. As a string, you won't lose leading zeros at the beginning of the field (if there are any).

  4. There is nothing wrong with using final as one of your parameter names. You should be aware, however, that "final is an identifier with a special meaning when used in a member function declaration or class head. In other contexts, it is not reserved and may be used to name objects and functions." – CppReference

  5. Given the large number of textbooks that use it, you may be surprised to learn that professional programmers do not use using namespace std; in production code. Neither should you. Instead, professionals just type std:: every time they need to reference a name from the Standard Library. There are a few exceptions to this, mostly centered around something called argument-dependent lookup, but there is not enough space to go into them here. (Hint: overloads for std::swap frequently use ADL.)

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.