Pregunta

I am trying to convert a CharArrayPtr to int format, which I have managed to do so;

    std::string str;

    for(int i = 0; i < numberofvalues; i++)
    {   



        str = cmemblock[i];
        std::stringstream stream;
        stream <<str;
        int n;
        if (!(stream >>n)){

        }

        cout << n<<endl;
        }

However, the issue I am having is if I were to cout << str; It will display characters exactly how it is displayed in the .txt file. Unfortunately after the conversion to int format the output is not as expected, I will demonstrate below;

cout << str;

Displays this;

11
22
33
44
55
66
77
88
99

cout << n;

displays this;

1
1
1
2
2
2
3
3
3
4
4
4
5
5
5
6
6
6
7
7
7
8
8
8
9
9
9
9
9
9
9
9
9
9

I'm not entirely sure as of why this is happening but I believe the code I have entered is not coded correctly to interpret a new line perhaps?

¿Fue útil?

Solución

for(int i = 0; i < numberofvalues; i++)

The problem was here, this was amended to the correct value and has now been solved.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top