0
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

6 Answers 6

3

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!
}
Sign up to request clarification or add additional context in comments.

Comments

1
  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.)

Comments

1

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().

Comments

0

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?

3 Comments

ah, prob getting index goof ups. I forgot I was already inside the loop. I would post all my code, but it's > 200 lines /p
Okay. Well, is converting each character what you want to do? because atoi() takes an entire "string" (null terminated char array)
each of the characters is a number =\
0

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.

1 Comment

it's not, it's in an outer loop. I didn't post all my code, cause there is a ton.
0

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.