Home > Enterprise >  Weird character when displaying data from .dat file in c
Weird character when displaying data from .dat file in c

Time:11-26

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?

CodePudding user response:

Thank you very much for the comment. I exported a spreadsheet as .csv then convert it to .dat in notepad. Aparenly the "space" isn't actually a space. So i removed the space between word and add new "space" manually. It fixed the problem.

CodePudding user response:

Since you have already fixed your issue(which seem to with the input file rather than with the program), i want to propose one modification to ͟r͟e͟m͟o͟v͟e͟ ͟t͟h͟e͟ ͟r͟e͟d͟u͟n͟d͟a͟n͟c͟i͟e͟s͟. In particular, you don't need

//no need for these three statements
stringstream ss(line);
getline(ss, studentName, ';');
getline(ss, studentGPA);

in your program. Instead you can just directly use getline on studentName and studentGPA as shown below.

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

int main() {
    ifstream inGPA;
    string studentGPA;
    string studentName;
    inGPA.open("input.txt");

    if (inGPA.is_open()) {
        string line;
        //just getline directly into variables studentName and studentGPA
        while (getline(inGPA, studentName, ';'), getline(inGPA, studentGPA)) {

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

CodePudding user response:

The non-breaking space might be unwanted input, but if you have names with non-ASCII characters, you will have the same problem.

Part of the problem here is that your terminal doesn't know that you are sending UTF-8 encoded characters.

If you are on Windows you can refer to this question.
The basic idea is to set the terminal to understand UTF-8 first:

#include <Windows.h>

int main() {
    SetConsoleOutputCP(CP_UTF8); // set output to UTF-8
    // your code
}

This will print your non-breaking space characters normally.

Note: This change doesn't only last for the execution of your program.
If you run your unfixed program after your fixed one, the program will seemingly work, until you run it in a fresh terminal.

  • Related