-1

I'm trying to find the simplest way to convert a string array of numbers to an int array so that I can use it in calculations. Stringstream and stoi have so far both crashed the program or not worked. Any help? Thanks!

int counter = 0;
string nums[51];
while (! rel.eof())
{       
    getline(rel, tuple);
    cout<<tuple<<endl;
    //minituple[counter] = tuple.substr(0,4);
    //counter++;
    istringstream iss(tuple);

    do
    {
        string found_num;
        iss >> found_num;

        if (iss.fail())
            break;

        char goaway;
        goaway = found_num.at(0);
        if (goaway == '\n')
            counter--;
        else if (goaway == ' ')
            counter--;
        {
            nums[counter] = found_num;
            cout << "Substring: " << found_num << endl;
        }
        counter++;
    } while (iss);      
}

int int_nums[51];
for (int i = 0; i <= 51; i++)
{
    if (!nums[i].empty())
    {
        stoi(nums[i]);
    }
}
TartanLlama
  • 63,752
  • 13
  • 157
  • 193
John M.
  • 1
  • 1

1 Answers1

0

Once you've loaded your strings array you could just do:

std::transform(std::begin(nums), std::end(nums), std::begin(int_nums), 
               [](auto const &str) { return std::stoi(str); });

LIVE DEMO

101010
  • 41,839
  • 11
  • 94
  • 168