Smiley face when assigning the wrong type of value to a struct!

I am somewhat surprised if I am not losing my opinion, but I swear to you, this code displays emoticons as .name values ​​!! What is happening in the world? Until now, it seems to work only when the value is 1, something else is giving errors properly.

I understand that the code is spoiled → I do not need help with this.

#include <iostream>
#include <fstream>
#include <regex>
#include <string>
#include <list>

using namespace std;
using namespace tr1;


struct CollectedData
{
public:
    string name;
    float grade;

};

int main()
{
    string line;
    list<CollectedData> AllData;
    int count;

    ifstream myFile("test_data.txt");
    if (myFile.fail()) {cout << "Error opening file"; return 0;}
    else
    {
        cout << "File opened... \n";
        while( getline(myFile, line) ) {
            CollectedData lineData;
            lineData.name = 1;
            lineData.grade = 2;
            AllData.push_back(lineData);
        }
    }

    cout << "\n\n File contents: \n";

    list<CollectedData>::iterator Iterator;
    for(Iterator = AllData.begin(); 
            Iterator != AllData.end();
            Iterator++)
    {
        cout << "\t" << (*Iterator).name << " - ";
        cout << "\t" << (*Iterator).grade << "\n";
    }


    getchar();
    return 1;
}

:-) http://img21.imageshack.us/img21/4600/capturekjc.jpg

I
KNOW THAT THE CODE IS NECESSARY, I WANT TO KNOW WHY THAT THIS GIVES ME TO SEE FACE INSTEAD OF ERRORS

comforting. mocking

+3
source share
5 answers

- ASCII- 1. , , , , char, .

+8

, ,

string, char 0x01 . , 0x31, 1, ASCII.

+9

:

lineData.name = 1;
lineData.grade = 2;

, , , ASCII 1 ( , , lineData.name).

while( getline(myFile, line) )

, lineData.name lineData.grade.

+6

(1), ASCII.

+3

, string, :

lineData.name = "1";

inverted commas will let the compiler know that this value is a string, and you will stop receiving emoticons.

who said ...

The coldest time. Mistake. Someday.

+2
source

Source: https://habr.com/ru/post/1716462/


All Articles