简体   繁体   中英

How to convert multiple numbers in a string into ints

I am confused as to how to take multiple strings that have numbers in them and convert them into an int. I have multiple lines of strings that are stored in data into ints and then insert them into a 2 dimensional arrays called values. A similar question was posted earlier on StackOverflow; however it does not seem to be working for me. I printed out what each line in data is, each string in data is as follows.

 75
 95 64
 17 42 82
 18 35 87 10

However when I output the numbers from value by using two for loops in main, it outputs as this.

75 0 0 0 95 64 0 0

95 64 0 0 17 42 82 0

17 42 82 0 18 35 87 10

18 35 87 10 0 0 0 0

I found that there are 8 columns and 8 elements in the array values when I printed the sizeof(values) and sizeof(values[0]); however, it appears that the program terminated as the last print statement, where I print hello does not occur. I provided the code I'm using below. I would like to know why this is occurring and how I can fix it? Thanks.

//const char DELIMITER = ' ';

int **values, // This is your 2D array of values, read from the file.
    **sums;   // This is your 2D array of partial sums, used in DP.

int num_rows; // num_rows tells you how many rows the 2D array has.
          // The first row has 1 column, the second row has 2 columns, and
          // so on...

bool load_values_from_file(const string &filename) {
ifstream input_file(filename.c_str());
if (!input_file) {
    cerr << "Error: Cannot open file '" << filename << "'." << endl;
    return false;
}
input_file.exceptions(ifstream::badbit);
string line;
vector<string> data;
try {
    while (getline(input_file, line)) {
        data.push_back(line);
        num_rows ++;
    }
    input_file.close();
} catch (const ifstream::failure &f) {
    cerr << "Error: An I/O error occurred reading '" << filename << "'.";
    return false;
}
for(int x = 0; x < data.size(); x++){
    cout << data[x] << endl;
}

//https://stackoverflow.com/questions/1321137/convert-string-containing-several-numbers-into-integers
//Help on making multiple numbers in a string into seperate ints
values = new int*[num_rows];
vector<int> v;
for(int y = 0; y < data.size(); y++){
    istringstream stream(data[y]);
    values[y] = new int[y + 1];
    int z = 0;

    while(1) {
       int n;
       stream >> n;
       if(!stream)
          break;
       values[y][z] = n;
       z++;
    }
    z = 0;
}
sums = values;

return true;
}

int main(int argc, char * const argv[]) {

if (argc != 2) {
    cerr << "Usage: " << argv[0] << " <filename>" << endl;
    return 1;
}
string filename(argv[1]);
if (!load_values_from_file(filename)) {
    return 1;
}

cout << sizeof(values) << endl;
cout << sizeof(values[0]) << endl;

for(int x = 0; x < sizeof(values); x++){
    for(int y = 0; y < sizeof(values[x]); y++){
        cout << values[x][y] << endl;
    }
    cout << endl;
}
cout << "hello" << endl;


return 0;

}

See this :

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <algorithm>

using namespace std;
int main()
{
    string str;
    ifstream fs("D:\\Myfolder\\try.txt", ios::in);
    stringstream ss;
    std::string item;
    if (fs.is_open())
    {       
        ss << fs.rdbuf();
        str = ss.str();
        cout << str << endl;
        fs.close();
    }

    cout << "\n\n Output \n\n";
    while (std::getline(ss, item, ' '))
    {
        cout << std::atoi(item.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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM