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(const char*)’
Any ideas?
each of the characters is either a 1 or a 0
All kinds of Bad here:
You are trying to copy a string in to an uninitialized char*
( temp_array
is declared but never initialized)
atoi
expects a pointer to a whole string, but you are passing it a single char ( temp_array[j]
)
fann_input
is an array of float
(you say) but you are trying to fill it with int
s (which is what atoi
returns)
You are using C constructs (pointers, atoi
, etc) in C++
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!
}
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()
.
main
is int main(int argc, char **argv)
, even though argv
should be considered constant. temp_array
is not initialized. atoi
on individual characters instead of on a string. 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.)
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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.