-2

I have an input file that looks like this:

3, 2, 5
2, 4, 9
6, 5, 9

And I've made an array[3][3].

My question is: how can I read this into my array, while skipping the spaces and commas?

Also: I can't use stoi (don't have C++11) and I haven't covered vectors yet.

I've tried the code block below, but my resulting array is full of zeroes.

    string sNum;                     // Current number (as string)
    int num = 0;                     // Current number (as int)
    ifstream inFile;                 // Make input file stream
    inFile.open("Numbers.txt");      // Open my input file of 3x3 numbers

     while (getline(inFile, sNum, ',')) {                                 // Gets 'line' from infile ..
                                                                             // puts it in sNum .. 
                                                                             // delimiter is comma (the problem?).
        for (int rowCount=0; rowCount<3; rowCount++) {                    // Go through the rows
                for (int columnCount=0; columnCount<3; columnCount++) {   // Go through the columns

                        num = atoi(sNum.c_str());                         // String 'sNum' to int 'num'
                        array[rowCount][columnCount] = num;               // Put 'num' in array


                } // end columnCount for
        } // end rowCount for
    } // end while

I think it is reading in the spaces. How do I ignore the spaces in between getting my ints?

  • Does this answer your question? [Parsing a comma-delimited std::string](https://stackoverflow.com/questions/1894886/parsing-a-comma-delimited-stdstring) – cwbusacker Feb 16 '20 at 00:52
  • 1
    Reading the spaces is the least of your problems. What do you think is going to happen when the shown inner loop sets ***every*** value in the 2d array to the ***same*** `sNum`? The shown code reads one value from the file (whether it reads it correctly or not -- and it's not -- is besides the point), then merrily loops over the entire 2D array, setting every value in the array to `sNum`. Then it reads the next value in the file (again, incorrectly, but that's still besides the point), and there it goes again, setting every value in the 2D array to it. That's clearly not what you want to do. – Sam Varshavchik Feb 16 '20 at 00:53
  • 2
    similar questions have been asked so many times before that don't even think people look around the web before asking on SO anymore... – Aykhan Hagverdili Feb 16 '20 at 00:55
  • @cwbusacker Yes I saw that thread before but I think I will need to use vect after all – JohnSizzle Feb 16 '20 at 01:01
  • What do you mean by vect? Stringstream is the best option to help you. – cwbusacker Feb 16 '20 at 01:06
  • @cwbusacker Oops I meant vector from the linked question but nvm. And OK. – JohnSizzle Feb 16 '20 at 01:10
  • @SamVarshavchik I just realized that error, I've been moving the loops around in the code for a while and then somehow ended up setting the same number repeatedly. – JohnSizzle Feb 16 '20 at 01:11
  • @Ayxan I searched for a while but probably not well enough – JohnSizzle Feb 16 '20 at 01:14
  • 1
    Now that you realized that error, here's another one: you forgot the Golden Rule Of Computer Programming: your computer always does exactly what you tell it to do instead of what you want it to do. You told your computer: `getline(inFile, sNum, ',')`, or: "computer, I order you to read from `inFile` until the next comma". So, your computer will do exactly that. Specifically, the third time it does this, with your input, it will read exactly the following into `sNum`: `" 5\n2"`, or: a space, character '5', the newline character, and character '2'. Is this what you wanted your computer to do? – Sam Varshavchik Feb 16 '20 at 01:25
  • This question has been asked close to a bazillion times. This week alone. – IInspectable Feb 16 '20 at 09:07

1 Answers1

0

Here's one solution:

string sNum;                                                                                                                                                                     
int array[3][3];
int num = 0;                                                                                                                                                                        
    ifstream inFile;                                                                                                                                                                   
    stringstream ss;
    inFile.open("Numbers.txt");      // Open the input file of 3x3 numbers                                                                                                                                       
    int row = 0;
    int col = 0;
     while (getline(inFile, sNum)) {  // Gets entire line from inFile and stores in sNum                                                                                                             
        ss.str(sNum); //puts the line into a stringstream

        while(ss >> num)
        {
           if(col == 3) //handles our 2D array indexes.
                        //when col reaches 3, set it back to 0 and increment the row.
           {
              row++;
              col = 0;
           }

           array[row][col] = num;
           col++;

           //eliminate any commas and spaces between each number.
           while(ss.peek() == ',' || ss.peek() == ' ')
              ss.ignore();
        }
        ss.clear();
     } //        end while                                                                                                                                                                                      

}

cwbusacker
  • 507
  • 2
  • 12