开发者

Trouble converting string from commandline to array of floats

char * temp_array;

strcpy(temp_array, argv[i + 1]);

            for(int j = 0; j < 8; j++)
            {
                fann_input[j] = atoi(temp_array[j]);
                printf("%f\n", fann_input[j]);
                printf("o%c\n", temp_array[j]);
            }

fann_input is a float array.

on the atoi line, I get the error:

src/main.cpp: In function ‘int main(int, const char**)’:
src/main.cpp:117: error: invalid conversion from ‘char’ to ‘const char*’
src/main.cpp:117: error:   initializing argument 1 of ‘int atoi开发者_JS百科(const char*)’

Any ideas?

each of the characters is either a 1 or a 0


All kinds of Bad here:

  1. You are trying to copy a string in to an uninitialized char* (temp_array is declared but never initialized)

  2. atoi expects a pointer to a whole string, but you are passing it a single char (temp_array[j])

  3. fann_input is an array of float (you say) but you are trying to fill it with ints (which is what atoi returns)

  4. You are using C constructs (pointers, atoi, etc) in C++

  5. You are coying the same command line argument over and over.

Do something more along these lines. Uncompiled psudocode follows. Error handling is left as an excercise for you.

for( int j = 0; j < 8; ++j )
{
  stringstream ss;
  ss << argv[j+1]; // copy cmd line param to stream
  float val = 0.0f;
  ss >> val;  // convert to a float
  fann_input[j] = val;  // save it!
}


  1. The prototype of main is int main(int argc, char **argv), even though argv should be considered constant.
  2. You're copying into a random part of memory because temp_array is not initialized.
  3. You're calling atoi on individual characters instead of on a string.
  4. atoi converts to int, not to float, although that shouldn't be a problem with {0,1} inputs.

To convert an individual digit in a string s to a float, do

float x = s[i] - '0';

(Assuming an ASCII- or EBCDIC-compatible character set where 1 follows 0. I've never heard of a character set where that isn't true.)


temp_array[i] is just what the compiler is telling you it is - a char. atoi() takes a string as its only parameter. You need to correct your algorithm to pass a string to atoi().


Can you post the complete code?

You use "i" outside the loop, then you redefine it in the for loop, and then it looks like you're trying to convert each CHARACTER of the string into its own float?


For one yo don't actually allocate any space in temp_array. That will give you a great runtime bug.

The following line of code is wrong and giving you your errors:

fann_input[i] = atoi(temp_array[i]

You are passing 1 character to atoi when it expects a string.

Also

strcpy(temp_array, argv[i + 1]);

Is using i before it is declared.


You haven't allocated any space for temp_array so you'll write to random locations when you strcpy into it. You're treating temp_array as an array of strings when it's an array of characters, and then trying to convert single characters to a float. atoi will convert to an int NOT to a float or double. You need to use atof.

What about a C++-esque way like this, even better would use streams:

#include <iostream>
#include <string>
#include <vector>

int main(int argc, char* argv[])
{
    typedef std::vector<std::string> Args;
    Args args(&argv[1], &argv[argc]);
    std::vector<double> results;

    for(Args::const_iterator i = args.begin(); i != args.end(); ++i)
    {
        results.push_back(atof(i->c_str()));
    }

    return 0;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜