-4

Can someone help me with understanding why this code isn't working, when program is running, "booklist.txt" can't be loaded.

When I call function listbooks while the program is running, it doesn't display anything on the screen, so that's the main problem. (file is not empty.)

  #include <iostream>
    #include <cstdlib>
    #include <fstream>

    using namespace std;
    void listbooks()
    {
        ifstream TheFile("booklist.txt");

        char autor;
        char bookname;
        long int isbn;

        while (TheFile >> autor >> bookname >> isbn)
    {
            cout << autor << " " << bookname << " " << isbn << endl;
    }
    }

    int main()
    {
        int choice;
    do {
            cout << "1.List all books" << endl;
            cout << "2.Borrow book" << endl;
            cout << "3. Exit" << endl;
            cout << "Enter your choice: ";
            cin >> choice;
            switch (choice)
    {
            case 1:
                listbooks; 
                break;
            case 2:
                break;
            default:
                cout << "";
    }
    } while (choice != 3);
        system("pause");
        return 0;
    }
  • I retracted the comment about the folder because this is just a typo. `listbooks;` does not call a function. I would have expected a warning on this line. – drescherjm May 24 '19 at 16:29
  • File is not located in wrong folder. – L.Krapic May 24 '19 at 16:29
  • Does the `author` really have a 1 letter name? Also the book? That would not be very interesting.. Note that `std::string` is a proper string in `c++` and `char` is a single character. – drescherjm May 24 '19 at 16:35
  • @L.Krapic You have a typo: `listbooks;` should be `listbooks();`. I voted to close your question accordingly. Also how do you know that the file exists in the same directory that's the programs current working directory. Can you elaborate about how you did this? – πάντα ῥεῖ May 24 '19 at 16:36
  • _"File is not located in wrong folder"_ Are you sure? Which folder are you looking in? How do you know it's the right one? Can you show us? – Lightness Races in Orbit May 24 '19 at 17:41

2 Answers2

4

In your switch you have

case 1:
    listbooks; 
    break;

This is not a function call: in C/C++, a function name (without parenthesis) is an address. Try

case 1:
    listbooks(); 
    break;

Related.

Trevor Keller
  • 510
  • 2
  • 10
-3

Problem was char variables, when I deleted "char autor" and "char bookname", program was running perfectly.