0

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;

}

Phil Cho
  • 131
  • 3
  • 14
  • Your question fails to meet all the requirements for a [mcve], as explained in the stackoverflow.com's [help], and, as such, an authoritative answer will not be possible. What is "sums"? Where is the code that generates the output? If the output is not being generated correctly, how exactly do you expect anyone to figure out a problem in the code that's not shown? How did you reach the conclusion that the problem must be in the code that parses the input, and not in the code that generates the output? And when you used your debugger to step through your program, what observations did you make? – Sam Varshavchik May 03 '18 at 00:43
  • Sorry, I didn't want to add too many lines and I thought those would just be extraneous lines of codes that wouldn't be necessary to show. I'll edit it to include it – Phil Cho May 03 '18 at 00:47
  • It is true that stackoverflow.com is not a web site where one can dump their entire program and wait for someone to figure out what's wrong with it. This is why the requirement is for a "[mcve]", and not "an entire program". Furthermore, as I asked "when you used your debugger to step through your program, what observations did you make". Specifically: after setting a breakpoint at the end of the function that you showed in your question, and you used your debugger to examine the values in the `values` array, what did your debugger tell you was in there? – Sam Varshavchik May 03 '18 at 00:48
  • As I expected, your code that attempts to print the values is broken. `sizeof()` does not do what you think it does. A `sizeof()` of an `int **` that's pointing to one pointer is the same value as a `sizeof()` of an `int **` that points to one billion pointers. – Sam Varshavchik May 03 '18 at 01:04
  • I first saw that there are 8 columns in values and in each array there are 8 elements. However, after the 4th array, the program terminates, as when I tried printing array[4][0] before I printed all the numbers, it also crashed. I will be editing my question to include this detail as well. – Phil Cho May 03 '18 at 01:04
  • Hmmm, I somewhat understand what you mean; however is there any clear explanation as to why it is 8 and how to properly find the size? I use sizeof(values) and sizeof(values[x]) in other functions where I use it in a for loop as well, so this may cause a problem. Also, am I correct in assuming that the load_values_from_files does it's function correctly? – Phil Cho May 03 '18 at 01:12
  • I think I'm starting to understand what you mean, and I believe I can solve the problem on my own. I will be using the variable num_rows instead of sizeof(values), and base the size of the second for_loop based on x. Thank you. – Phil Cho May 03 '18 at 01:17
  • It is 8 because you are running your program on a 64-bit operating system, which uses pointers that are 8 bytes long. `sizeof()` is a ***compile-time*** constant expression that tells you how many bytes a particular object takes. Your `int **` is always 8 bytes long, it's a pointer, and it's always 8 bytes whether it points to one pointer, or to a billion of them. – Sam Varshavchik May 03 '18 at 01:19

1 Answers1

1

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;
}
seccpur
  • 4,996
  • 2
  • 13
  • 21
  • Your output is missing several entries from the first column – bcr May 03 '18 at 04:12
  • 1
    @bcr: there were spaces before numbers 75(removed earlier), 95,17 & 18 , using copy paste from OP,which clashes with the delimiter, so they are missing. I remove output to avoid confusion. – seccpur May 03 '18 at 07:16