-2

This is an assignment and I am stuck in the first information reading process.. I need to read information from a file "bank.txt". Every line of the file is like this: rice(China),13,2016-8-3,5kg

I have wrote a cpp for testing:

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

struct Food
{
    string name;
    int quantity;
    string expireDate;
    string unit;
    bool selected;
};

void all(Food item[]);

int main()
{
    ifstream infile;
    Food bank[100];
    infile.open("bank.txt");

    string text, temp[400];
    string dummy;
    int num;
    stringstream linestream;

    while(infile.good())
    {
        for(int i = 0; i < 100; i++)
        {
            for(int j = 0; j < 4; j++)
            {
                getline(infile, text, ',');
                temp[4 * i + j] = text;
            }

            istringstream(temp[4 * i + 1]) >> num;
            bank[i].name = temp[4 * i];
            bank[i].quantity = num;
            bank[i].expireDate = temp[4 * i + 2];
            bank[i].unit = temp[4 * i + 3];
        }
    }

    all(bank);
    return 0;
}

void all(Food item[])
{
    for(int i = 0; i < 100; i++)
        cout << item[i].name << ".." << item[i].quantity << ".." << item[i].expireDate << ".." << item[i].unit << endl;
}

But this runs into some problems and the lines of odd numbers only displayed name.

How can I modify my code to make it behave normally??

LogicStuff
  • 19,397
  • 6
  • 54
  • 74
jyh0206
  • 7
  • 1

2 Answers2

1

The use of ',' as getline's separator leaves newlines in the stream.

Instead, first read a whole line, then use a stringstream built from that to extract the parts.
(For some reason, you have declared a variable that seems to be for this, but you never use it.)

string line;
if (getline(infile, line))
{
    istringstream linestream(line);
    for (int j=0;j<4;j++)
    {
        getline(linestream,text,',');
        temp[4*i+j]=text;
    }
    //...

There's also the problem of while (infile.good()), which you shouldn't do - you probably need to restructure your code.
Read more about it in this question and answers (it's about eof, but the same principle about "good" and "bad" streams hold here).

Community
  • 1
  • 1
molbdnilo
  • 64,751
  • 3
  • 43
  • 82
0

You should also take into account the new line

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

struct Food
{
    string name;
    int quantity;
    string expireDate;
    string unit;
    bool selected;
};

void all(Food item[], int count);

int main()
{
    ifstream infile;
    Food bank[100];
    infile.open("bank.txt");


    string text, temp[400];
    string dummy;
    int num;
    stringstream linestream;

    int i = 0;
    while (i < 100)
    {
        getline(infile, text, ',');
        if (!infile.good()) {
            break;
        }
        temp[4 * i] = text;
        for (int j = 1; j<3; j++)
        {
            getline(infile, text, ',');
            temp[4 * i + j] = text;
        }
        getline(infile, text, '\n');
        temp[4 * i + 3] = text;

        istringstream(temp[4 * i + 1]) >> num;
        bank[i].name = temp[4 * i];
        bank[i].quantity = num;
        bank[i].expireDate = temp[4 * i + 2];
        bank[i].unit = temp[4 * i + 3];
        i++;
    }

    all(bank, i);
    return 0;
}

void all(Food item[], int count)
{
    for (int i = 0; i<count; i++)
        cout << item[i].name << ".." << item[i].quantity << ".." << item[i].expireDate << ".." << item[i].unit << endl;
}
Selçuk Cihan
  • 1,979
  • 2
  • 17
  • 30
  • the information are displayed properly, thx. but there are many lines of "340g..340..340g..340g" after that. don't know why.. – jyh0206 Apr 21 '15 at 07:02