4

So I'm trying to read a input file into a 2-dimensional array.

The problem I'm having is that I want only certain lines in my input file to be read but I just don't know where to put the second ignore in my code

Here is the input file called "Fruit.txt":

Oroblanco Grapefruit
Winter
Grapefruit

Gold Nugget Mandarin
Summer
Mandarin

BraeBurn Apple
Winter
Apple

And my code:

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

const int MAX_ROW = 6;
const int MAX_COL = 4;

void FileInput(string strAr[MAX_ROW][MAX_COL])
{

ifstream fin;

fin.open("Fruit.txt");

int columnIndex;
int rowIndex;

rowIndex = 0;

while(fin && rowIndex < MAX_ROW)
{
    columnIndex = 0;

    while(fin && columnIndex < MAX_COL)
    {
        getline(fin, strAr[rowIndex][columnIndex]);
        fin.ignore(10000,'\n');

        columnIndex++;

    }

    rowIndex++;
}


fin.close();
}

My code for now stores it like this:

Oroblanco Grapefruit // strAr[0][0]
Grapefruit           // strAr[0][1] 

Gold Nugget Mandarin // strAr[0][2]
Mandarin             // strAr[0][3]

BraeBurn Apple       // strAr[1][0]
Apple                // strAr[1][1]

I want it to be like this:

Oroblanco Grapefruit // strAr[0][0]

Gold Nugget Mandarin // strAr[0][1]

BraeBurn Apple       // strAr[0][2]

I just don't know where I should put the second ignore at. If I put it right after the first ignore, then it would skip more than what I want.

kaya3
  • 47,440
  • 4
  • 68
  • 97
Ken Casimiro
  • 85
  • 2
  • 9

2 Answers2

4

Your code is fine, just fix the variable columnIndex.

and use 3 ignores because you need to ignore the empty line too.

fin.ignore(10000,'\n');
fin.ignore(10000,'\n');
fin.ignore(10000,'\n');
Kamal Zidan
  • 474
  • 2
  • 9
  • 1
    As another technique to do it, you can go to lines that is divisible by 4 and read them only. See [*my answer here*](http://stackoverflow.com/questions/43731500/reverse-content-of-a-file-without-using-an-array-in-c/43734484#43734484), you may learn more techniques. – Shadi May 07 '17 at 03:53
2

There are several problem with your code:

  1. colIndex is a unused variable, maybe a typo problem;
  2. MAX_COL should be 3 not 4;
  3. The order of rowIndex, columnIndex is wrong.

Instead of

const int MAX_COL = 4;
while(fin && rowIndex < MAX_ROW)
{
    colIndex = 0;  // unused variable

    while(fin && columnIndex < MAX_COL)   // MAX_COL should be 3
    {
        getline(fin, strAr[rowIndex][columnIndex]);  // order problem
        fin.ignore(10000,'\n');

        columnIndex++;

    }

    rowIndex++;
}

use this:

const int MAX_COL = 3;  // MAX_COL should be 3
while(fin && rowIndex < MAX_ROW)
{

    columnIndex = 0;   // variable name fixed.
    while(fin && columnIndex < MAX_COL)
    {
        getline(fin, strAr[columnIndex][rowIndex]); // order matters
        if (strAr[columnIndex][rowIndex].empty() == false || 
            no_need_to_ignore()) {  // your sikp logic added here
            columnIndex++;
        }
    }
    rowIndex++;
}
luoluo
  • 5,353
  • 3
  • 30
  • 41
  • Is there anyway to fix this with a placement of another ignore in my code? and its 6 (variety of fruits) and 4 for being the number of seasons. (forgot to mention in my post) – Ken Casimiro May 07 '17 at 03:25
  • `strAr[columnIndex][rowIndex].empty() == false` this line helps you to ignore the empty lines. Is there any other lines to skip?@WuWuKennyWu – luoluo May 07 '17 at 03:26
  • yes, I wish to ignore the season and the fruit type. I only wish to keep the name of the fruit. for example: Oroblanco Grapefruit, Winter, Grapefruit is in my input file. I only want it to read in Oroblanco Grapefruit and skip the next two to grab the next fruit name. – Ken Casimiro May 07 '17 at 03:29
  • @WuWuKennyWu updated the answer, maybe you need to add a logic to check whether or not you should do the ignore operation. – luoluo May 07 '17 at 03:32