Weird character when displaying data from .dat file in c++

  c++, fstream, ifstream

I have an ipk.dat file containing student name and their GPA separated by semicolon. I’m trying to display the names of students who have a GPA greater than 3, but I get output with strange characters like this in the console.

Hidayat Sari            3.60 
Susila Buana            3.27 
Krisna Sari             3.66 
Taufik Fatimah          3.38 
Bachtiar Darma          3.70 
Yohanes Anwar           3.93 
Harun Ratna             3.48 
Mega Zulfikar           3.32 
Zulfikar Abdul          3.50 
Rahman Nirmala          3.37 
Amir Cinta              3.30 
Firdaus Latifah         3.16 
Annisa Ali              3.65 
Eka Yuliana             3.14

This is my code:

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

int main() {
    ifstream inGPA;
    string studentGPA;
    string studentName;
    inGPA.open("ipk.dat");

    if (inGPA.is_open()) {
        string line;
        while (getline(inGPA, line)) {
            stringstream ss(line);
            getline(ss, studentName, ';');
            getline(ss, studentGPA);

            if ( stod(studentGPA) >= 3.0) {
                cout << studentName << "     t" << studentGPA << endl;
            }
        }
    }
    return 0;
}

And this is the inside of the ipk.dat file.The encoding for this file is UTF-8.

enter image description here

How do i fix this weird character issue?

Source: Windows Questions C++

LEAVE A COMMENT