1

I'm trying to write a function that takes a string array as a parameter, converts it to a int array, and returns the new array. I thought it would be pretty simple with the use of "Atoi" but apperently you cant use it the way i tried to.

here is my code so far.

int GameHandler::convertToInt(string array[])
{
    int tmp=0;
    string arr[20]=array;
    int values[20];
    for(int i=0;i<20;i++)
        values[i]=0;

    for(int i=0;i<20;i++)
    {
        tmp=atoi(arr[i]);
        values[i]=tmp;

    }
    return values;
}

Here is the error msg i get from my compiler:

GameHandler.cpp: In member function ‘int GameHandler::convertToInt(std::string*)’: GameHandler.cpp:60:20: error: conversion from ‘std::string* {aka std::basic_string*}’ to non-scalar type ‘std::string {aka std::basic_string}’ requested GameHandler.cpp:67:24: error: cannot convert ‘std::string {aka std::basic_string}’ to ‘const char*’ for argument ‘1’ to ‘int atoi(const char*)’ GameHandler.cpp:71:12: error: invalid conversion from ‘int*’ to ‘int’ [-fpermissive] GameHandler.cpp:61:9: warning: address of local variable ‘values’ returned [enabled by default]

S.C
  • 41
  • 1
  • 1
  • 3

5 Answers5

2

The signature of atoi is

int atoi(const char *str);

Thus you need to pass a const char* to atoi, in your case:

tmp=atoi(arr[i].c_str());
muehlbau
  • 1,897
  • 13
  • 23
  • i replaced the line with tmp=atoi(arr[i].c_str()); Still get errors tho when i compile. – S.C Oct 10 '12 at 14:32
0

You need get the underlying const char* of the std:string. You can do it with the c_str() method in the std:string class.

http://www.cplusplus.com/reference/string/string/c_str/

muehlbau
  • 1,897
  • 13
  • 23
Jakub Zaverka
  • 8,816
  • 3
  • 32
  • 48
  • cplusplus.com is not regarded as reputable source for information on C++ –  Oct 10 '12 at 15:54
0

What might work for you is this solution with a (better) static function:

const int ELEM_CNT = 20;
static void convertToInt(string const strings[ELEM_CNT], int ints[ELEM_CNT])
{
  for (int i=0; i<ELEM_CNT; ++i)
    ints[i] = atoi(strings[i].c_str());
}


[...]
string sArr[ELEM_CNT];
int iArr[ELEM_CNT];
convertToInt(sArr, iArr);

Be careful about the following things:

  • int values[20]; // this is an array on the stack whose memory is not usable outside the enclosing block (e.g. function)
  • string array[] // array of unknown size; for dynamic sizes (set at runtime) you might want to use std::vector, which knows its size by size() member function
coproc
  • 6,027
  • 2
  • 20
  • 31
0

As well as the atoi error, this line is also incorrect.

string arr[20]=array;

You cannot copy arrays like this in C++.

Just delete the above line and replace

tmp=atoi(arr[i]);

with

tmp=atoi(array[i].c_str());

There's no need to copy the string array for what you want to do.

john
  • 7,897
  • 29
  • 27
  • Alright, done that, that took care of some of the errors Now, the debbuger gives me the folowing error: GameHandler.cpp:71:12: error: invalid conversion from ‘int*’ to ‘int’ [-fpermissive] – S.C Oct 10 '12 at 14:46
0

I'd suggest not to use C-style arrays.

void GameHandler::convertToInt(std::vector<int>& values, 
                              const std::vector<std::string>& array)
{
    values.resize(array.size());

    for(int i=0;i!=array.size();i++)
    {
        values[i]=atoi(array[i].c_str());
    }
}
Andriy
  • 8,486
  • 3
  • 27
  • 51