0
typedef struct
{
    int isbn_code, year_published, quantity, rack, level_no;
    char author[50], title[100], publisher[50];
    double price;
}DATA;

Shown above is my typedef struct. And when i debug, it only shows "1. ". So here is my code. Please tell me what's wrong and what do i need to do to make it execute properly. I'm just trying to read from a text file and categorize them into different members of structure.

int file_reader()
{
    DATA d;
    ifstream infile("books.txt", ios::in);
    if (infile.is_open())
    {
        while (!infile.eof())
        {
            for (int row = 1; row < 100; row++)
            {
                cout << row << ". ";
                char line[200];
                cin.ignore();
                cin.getline(line, 200, '\n');

                char* column = strtok(line, ",");

                while(column)
                {
                    cin >> d.isbn_code;
                    column = strtok(NULL, ",");
                    cin.getline(column, 50);
                    strcpy(d.author,column);
                    column = strtok(NULL, ",");
                    cin.getline(column, 100);
                    strcpy(d.title, column);
                    column = strtok(NULL, ",");
                    cin.getline(column, 50);
                    strcpy(d.publisher, column);
                    column = strtok(NULL, ",");
                    cin >> d.year_published;
                    column = strtok(NULL, ",");
                    cin >> d.quantity;
                    column = strtok(NULL, ",");
                    cin >> d.price;
                    column = strtok(NULL, ",");
                    cin >> d.rack;
                    column = strtok(NULL, ",");
                    cin >> d.level_no;
                }
                cout << d.isbn_code << "," << d.author << "," << d.title << "," << d.publisher << "," 
                    << d.year_published << "," << d.quantity << "," << d.price << "," << d.rack << "," << d.level_no;
            } cout << endl;
        } infile.close();
    }
    else
        cout << "File is not open\n";
    return 0;
}
  • 1
    Unrelated to your problem (well, maybe a little related) but I think you need to invest in [some good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282). I also recommend you learn about [rubber duck debugging](https://en.wikipedia.org/wiki/Rubber_duck_debugging) and try it out on the piece of code you show. – Some programmer dude Apr 01 '20 at 13:40
  • @john I think they did say it only prints "1." but then again they said "when I debug" so \*shrug\* – Object object Apr 01 '20 at 13:55

1 Answers1

0

This code is wrong

cin >> d.isbn_code;

That reads the ISBN code from cin not from your file. Probably what you intended is this

d.isbn_code = atoi(column);

Then on the next three lines

column = strtok(NULL, ",");
cin.getline(column, 50);
strcpy(d.author,column);

Again you are reading from cin not from your file. I think that you meant this

column = strtok(NULL, ",");
strcpy(d.author,column);

Basically you are making the same mistake over and over, you are reading from cin when you said you wanted to read from a file. That's why your program isn't doing anything, it's waiting for you to type something.

There's other problems as well (your loops are all wrong, you only have one DATA struct when clearly you want many, for example). But fix the input first and then have another look.

john
  • 85,011
  • 4
  • 57
  • 81