-1

I have the following .txt file:

{{1,2,3,0}, {1,1,1,2}, {0,−1,3,9}}

This is a 3x4 matrix. I'm using strtok to extract the numbers and saving on a float matrix. The problem is, when p gets -1, it's being converted to zero when saved on matrix. How could I fix it?

    p = strtok(&matrix[0u], " {},");

    for (i = 0; i < m + 1; i++){
        for (j = 0; j < n + 1; j++) {
            aux[i][j] = atoi(p);
            if (p)
                p = strtok(NULL, " {},");
        }
    }

Is there a better way to extract the numbers, one at a time? How?

phuclv
  • 37,963
  • 15
  • 156
  • 475
Pat
  • 69
  • 2
  • 16
  • 1
    As a wild guess your loop should be `i – 6502 May 08 '17 at 06:07
  • 2
    `−` is the [minus sign](http://www.fileformat.info/info/unicode/char/2212/index.htm) which is not recognized by standard functions. Programming languages use the `-` instead. Morevoer why do you use `strtok` in C++? – phuclv May 08 '17 at 06:09
  • I don't know a better way to extract integers one at a time. What's the best way using c++? – Pat May 08 '17 at 06:12
  • @Pat Take a look at 'std::istringstream'. If you are using C++11 you can use 'std::stoi'. – CaptainTrunky May 08 '17 at 06:23
  • a simple google will return many ways to convert string to int [How to parse a string to an int in C++?](http://stackoverflow.com/q/194465/995714), [std::string to float or double](http://stackoverflow.com/q/1012571/995714) – phuclv May 08 '17 at 16:26

1 Answers1

4

Your minus sign doesn't work. Compare:

  • this - is the ASCII minus sign
  • this − is your character whixh might be called "minus sign" by Unicode, but it is not normally recognised as such by C++ library functions

Don't copy code from Word documents and like places. If in doubt, convert to ASCII with iconv or a similar utility.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243