the ifstream is not working assistance needed

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

41 Replies - 2954 Views - Last Post: 09 October 2014 - 11:33 PM Rate Topic: -----

#1 ragmar   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 107
  • Joined: 13-June 14

the ifstream is not working assistance needed

Posted 12 September 2014 - 06:49 AM

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

#include <conio.h>
using namespace std;
struct ddpl{
    string author, title, choice, user_name, borrowed_book_title, borrowed_book_author;
    long int isbn, shelf_no;
    char id[15], borrow_date[15], due_date[15];
};

//add function
void add_book(){
    ddpl adding;

    cout << "enter the Title of the book: " ;
    getline(cin, adding.title);

    ofstream added_books;
    added_books.open(adding.title.c_str(), ios_base::app);

    cout << "Author: ";
    getline(cin, adding.author);

    cout << "ISBN: ";
    cin >> adding.isbn;

    cout << "Shelf_No: ";
    cin >> adding.shelf_no;

    cout << "AUTHOR \t\t ISBN \t\t SHELF_No" << endl;
    cout << adding.author << "\t\t" << adding.isbn << "\t\t" << adding.shelf_no << endl;
    added_books << "AUTHOR \t\t ISBN \t\t SHELF_No" << endl;
    added_books << adding.author << "\t\t" << adding.isbn << "\t\t" << adding.shelf_no << endl;
}
//borrow function
void borrowed(){
	ddpl borrow;

	ofstream borrowed_books;
	borrowed_books.open("borrower_list.txt", ios_base::app);

	cout << "User NAME: ";
	getline(cin, borrow.user_name);

	cout << "User ID: ";
	cin >> borrow.id;cin.ignore();

	cout << "TITLE Of The Book: ";
	getline(cin, borrow.title);

	cout << "AUTHOR Of The Book: ";
	getline(cin, borrow.author);

	cout << "Borrowing Date: ";
	cin >> borrow.borrow_date;cin.ignore();

	cout << "Due Date: ";
	cin >> borrow.due_date;

	cout << "\nUser NAME \t\t\t User ID \t\t\t TITLE \t\t\t AUTHOR \t\t\t B_Date \t\t\t D_Date" << endl;
    cout << borrow.user_name << "\t\t\t" << borrow.id << "\t\t\t" << borrow.title << "\t\t" << borrow.author << "\t\t" << borrow.borrow_date << "\t\t" << borrow.due_date << endl;

    borrowed_books << borrow.user_name << "\t\t\t" << borrow.id << "\t\t\t" << borrow.title << "\t\t\t" << borrow.author << "\t\t\t" << borrow.borrow_date << "\t\t" << borrow.due_date << endl;
}
//search function
void search(){
    ddpl sch;
    cout << "for searching books -1" << endl;
    cout << "for searching user -2" << endl;
    int choice;
    cin >> choice;cin.ignore();
    
    switch(choice){
        case 1:{
        ifstream read;
        read.open(sch.title.c_str());
        cout << "enter the title of the book to search from the library: ";
        getline(cin, sch.title);

        if(sch.title==sch.title){//here i'm kinda lost i don't know what to do?
            cout << "AUTHOR \t\t ISBN \t\t SHELF_No" << endl;
        
        while(read >> sch.author >> sch.isbn >> sch.shelf_no){
            cout << sch.author << "t\t" << sch.isbn << "\t\t" << sch.shelf_no << endl;}
            }}}}
//main function
int main (){
    int choice;
    cout << "add book    -1" << endl;
    cout << "borrow book -2" << endl;
    cout << "search book -3" << endl;
   cin >> choice;cin.ignore();
    do{
    switch(choice){
    case 1: add_book();
    case 2: borrowed();
    case 3: search();
    }
    }while(choice!=0);

}


in the search function
void search(){
    ddpl sch;
    cout << "for searching books -1" << endl;
    cout << "for searching user -2" << endl;
    int choice;
    cin >> choice;cin.ignore();
    
    switch(choice){
        case 1:{
        ifstream read;
        read.open(sch.title.c_str());
        cout << "enter the title of the book to search from the library: ";
        getline(cin, sch.title);

        if(sch.title==sch.title){//here i'm kinda lost i don't know what to do?
            cout << "AUTHOR \t\t ISBN \t\t SHELF_No" << endl;
        
        while(read >> sch.author >> sch.isbn >> sch.shelf_no){
            cout << sch.author << "t\t" << sch.isbn << "\t\t" << sch.shelf_no << endl;}
            }}}}

in the line were the conditional statement that crosscheck the title from the user and the title saved in the file
but it is not working and no idea why may be is the structure but i can't really point out what is wrong.
and please and thank u in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: the ifstream is not working assistance needed

#2 vividexstance   User is offline

  • Tiocfaidh ár lá
  • member icon

Reputation: 792
  • View blog
  • Posts: 2,873
  • Joined: 31-December 10

Re: the ifstream is not working assistance needed

Posted 12 September 2014 - 07:29 AM

You're comparing the same string to itself, so it's always going to be true. You should have a separate variable that you use for input, fetch the name from the file, then compare the name with the sch.title variable.
Was This Post Helpful? 0
  • +
  • -

#3 ragmar   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 107
  • Joined: 13-June 14

Re: the ifstream is not working assistance needed

Posted 12 September 2014 - 11:01 PM

#include <iostream>
#include <fstream>

using namespace std;

struct ddpl{
    string author, title, show_title, choice, user_name, borrowed_book_title, borrowed_book_author;
    long int isbn, shelf_no;
    char id[15], borrow_date[15], due_date[15];
};

void addAndSearch(){
    ddpl adding, sch;
    cout << "for adding -1" << endl;
    cout << "for searching -2" << endl;
    int chs;
    do{
        cout << "enter choice: ";
        cin >> chs;cin.ignore();

        if(chs==1){
            cout << "enter the Title of the book to add: " ;
            getline(cin, adding.title);

            ofstream added_books;
            added_books.open(adding.title.c_str(), ios_base::app);

            cout << "Author: ";
            getline(cin, adding.author);

            cout << "ISBN: ";
            cin >> adding.isbn;

            cout << "Shelf_No: ";
            cin >> adding.shelf_no;

            cout << "AUTHOR \t\t ISBN \t\t SHELF_No" << endl;
            cout << adding.author << "\t\t" << adding.isbn << "\t\t" << adding.shelf_no << endl;

            added_books << "AUTHOR \t\t ISBN \t\t SHELF_No" << endl;
            added_books << adding.author << "\t\t" << adding.isbn << "\t\t" << adding.shelf_no << endl;
            }
        else
            if(chs==2){
                ifstream read;
                read.open(adding.title.c_str());

                cout << "enter the title of the book to search: ";
                getline(cin, sch.show_title);

                if(adding.title==sch.show_title){
                    cout << "AUTHOR \t\t ISBN \t\t SHELF_No" << endl;
                while(!read.eof()){
                        read >> adding.author;
                        read >> adding.isbn;
                        read >> adding.shelf_no;
                    cout << adding.author;
                    cout << adding.isbn ;
                    cout << adding.shelf_no;break;
                    }
                }else
                    if(adding.title!=adding.title)
                        cout << "the book is not found in the library." << endl;
                }
            else
                cout << "bye!!!" << endl;
    }while(chs!=0);
}
int main()
{
    addAndSearch();
}

you mean like this it is still not working but, at least is displaying the last item in the list
<the shelf_no>.
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg   User is offline

  • member icon

Reputation: 5521
  • View blog
  • Posts: 17,113
  • Joined: 25-December 09

Re: the ifstream is not working assistance needed

Posted 13 September 2014 - 06:23 AM

Why are you using adding.title for your file name? Do you realize that doing this will create many small files with two lines in each file? Each file will have your "header" and one book title line. What you probably want is one file, with one line per title.

I also recommend that you only write the "header" information once or not at all. And that you use a single tab instead of the multiple tabs you're now using.


Also why are id, borrow_date, and due_date C-strings instead of std::string?



Jim

This post has been edited by jimblumberg: 13 September 2014 - 06:26 AM

Was This Post Helpful? 1
  • +
  • -

#5 ragmar   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 107
  • Joined: 13-June 14

Re: the ifstream is not working assistance needed

Posted 14 September 2014 - 11:02 PM

Thank u Jim,

Quote

Do you realize that doing this will create many small files with two lines in each file? Each file will have your "header" and one book title line.
Thank u very much much there is no way for real that i'm going to see that problem.

And for ( id, borrow_date, and due_date) i used char data type because (particularly for borrow_date and due_date) the user use to insert the date in this format may be 9/15/2014. the scape character is not recognized by other data type since it is a character. this is my only reason for doing it.
thanks in advance your comment means a lot.
Was This Post Helpful? 0
  • +
  • -

#6 ragmar   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 107
  • Joined: 13-June 14

Re: the ifstream is not working assistance needed

Posted 15 September 2014 - 12:18 AM

And the ifstream is not working after the console is closed it only works when the console is open and migth be a problem for the user to access data or book title stored previously
getline(read, adding.author);//read >> adding.author;

i even tried this to read the whole "author " part of the string instead of "read >> " but still not working thanks in advance.
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg   User is offline

  • member icon

Reputation: 5521
  • View blog
  • Posts: 17,113
  • Joined: 25-December 09

Re: the ifstream is not working assistance needed

Posted 15 September 2014 - 01:05 AM

Quote

And for ( id, borrow_date, and due_date) i used char data type because (particularly for borrow_date and due_date) the user use to insert the date in this format may be 9/15/2014. the scape character is not recognized by other data type since it is a character.

But why are they C-strings instead of std::string?

Quote

Thank u very much much there is no way for real that i'm going to see that problem.

And why not? Do you know how to use your file manager to view the directory where the files are located after you run the program for multiple titles?

Quote

And the ifstream is not working after the console is closed it only works when the console is open and migth be a problem for the user to access data or book title stored previously

What? The primary reason you're having problems "searching" is that every "book" is in it's own file instead of all the "books" being in one file, because you are using the book title for the name of the file.

Jim
Was This Post Helpful? 0
  • +
  • -

#8 ragmar   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 107
  • Joined: 13-June 14

Re: the ifstream is not working assistance needed

Posted 15 September 2014 - 01:38 AM

Thanks Jim,

"But why are they C-strings instead of std::string?" about this may be i'm not sure or i don't have any idea about it may be a link.

I said impossible for me because i thought it might require some functions.

And finally, i tried to put the "title of the books" in one file name but it was impossible for me to access the titles of the book.
Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg   User is offline

  • member icon

Reputation: 5521
  • View blog
  • Posts: 17,113
  • Joined: 25-December 09

Re: the ifstream is not working assistance needed

Posted 15 September 2014 - 05:57 AM

Quote

"But why are they C-strings instead of std::string?" about this may be i'm not sure or i don't have any idea about it may be a link.

What are you talking about? You are already using std::string for several of the members of your structure, author for example so why are you using arrays of characters for the dates and id number?

Quote

And finally, i tried to put the "title of the books" in one file name but it was impossible for me to access the titles of the book.

Show the code where you tried and failed to use one file for all the books. Also show what you inputted into the program ans show the contents of the file or files. You did open the files with a text editor and see what they contain, right?

Quote

I said impossible for me because i thought it might require some functions.

So what's the big deal about functions? You seem to know how to use them since your current code contains a couple of functions already.

Lastly all this talk of impossible tells me that you have already given up, with this attitude I don't know if it's worth my time to continue. So you need to convince me that you're still trying.

Jim
Was This Post Helpful? 1
  • +
  • -

#10 ragmar   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 107
  • Joined: 13-June 14

Re: the ifstream is not working assistance needed

Posted 15 September 2014 - 07:59 AM

i ain't a quitter
#include <iostream>
#include <fstream>
using namespace std;

struct ddpl{
    string author, title, show_title, choice, user_name, borrowed_book_title, borrowed_book_author;
    long int isbn, shelf_no, id;

};

void addAndSearch(){
    ddpl adding, sch;
    string theLog, theSearchLog;

    cout << "for adding -1" << endl;
    cout << "for searching -2" << endl;
    int chs;
    do{
        cout << "enter choice: ";
        cin >> chs;cin.ignore();

        if(chs==1){
            //cout << "enter the Title of the book to add: " ;
            //getline(cin, theLog);

            ofstream added_books;
            added_books.open("the_folder.txt", ios_base::app);

            //added_books << "TITLE \t\t AUTHOR \t\t ISBN \t\t SHELF_No" << endl;

            cout << "Title: ";
            getline(cin, adding.title);

            cout << "Author: ";
            getline(cin, adding.author);

            cout << "ISBN: ";
            cin >> adding.isbn;

            cout << "Shelf_No: ";
            cin >> adding.shelf_no;

            cout << "TITLE    : " << adding.title << endl;
            cout << "AUTHOR   : " << adding.author << endl;
            cout << "ISBN     : " << adding.isbn << endl;
            cout << "SHELF_No : " << adding.shelf_no << endl;

             added_books << "Title    : " << adding.title << endl;
             added_books << "Author   : " << adding.author << endl;
             added_books << "ISBN     : " << adding.isbn << endl;
             added_books << "Shelf_No : " << adding.shelf_no << endl;
            }
        else
            if(chs==2){
                ifstream read;
                read.open("the_folder.txt", ios_base::app);

                cout << "enter the title of the book to search: ";
                getline(cin, theSearchLog);

                if(theSearchLog==adding.title){
                while(!read.eof()){
                        getline(read, adding.title);
                        getline(read, adding.author);//read >> adding.author;
                        read >> adding.isbn;//getline(read, adding.isbn);
                        read >> adding.shelf_no;//getline(read, adding.shelf_no);

                        cout << "Tile: " << adding.title << endl;
                        cout << "AUTHOR: " << adding.author << endl;
                        cout << "ISBN: " << adding.isbn << endl;
                        cout << "Shelf_No: " << adding.shelf_no << endl;
                        break;
                    }
                }else
                    if(theSearchLog!=adding.title)
                        cout << "the book is not found in the library." << endl;
                }
            else
                cout << "bye!!!" << endl;
    }while(chs!=0);
}
int main()
{
    addAndSearch();
}


this is what so far i came through i make all the files " the title of the books" in one folder, but the console is not still working it display random saved titles when it is asked for one.
fro the sake of simplicity now i only concerned with reading part from the file, i'll deal with it with steps latter, thanks in advance, 'll do my best to convince myself that i can do it.
Was This Post Helpful? 0
  • +
  • -

#11 vividexstance   User is offline

  • Tiocfaidh ár lá
  • member icon

Reputation: 792
  • View blog
  • Posts: 2,873
  • Joined: 31-December 10

Re: the ifstream is not working assistance needed

Posted 15 September 2014 - 08:24 AM

When opening a file stream, if its for writing, it should always succeed, but when opening it for reading, it might fail to open. So you should always check if the file was opened successfully or not with the fstream::is_open() function which returns true if the file opened or false if it didn't.

*EDIT*:
A suggestion I have is to not write out the labels for each data item like "ISBN:", this will make it more difficult to read from the file. If you always write a record to the file in the same format, then you should be able to read it back in the same way. With the labels, you will have to parse them out of the input you get from the file.

This post has been edited by vividexstance: 15 September 2014 - 08:26 AM

Was This Post Helpful? 1
  • +
  • -

#12 jimblumberg   User is offline

  • member icon

Reputation: 5521
  • View blog
  • Posts: 17,113
  • Joined: 25-December 09

Re: the ifstream is not working assistance needed

Posted 15 September 2014 - 08:29 AM

If you run the program with your debugger, setting a breakpoint somewhere in the search section before the first if() statement, and then single stepping through that part of the function you should see that the if() statement evaluates to false every time, thereby skipping the actual search routine and printing "the book is not found in the library.". What is the purpose of that if() statement (if(theSearchLog==adding.title){)? At this point you haven't read the file and adding.title doesn't necessarily have valid data.

Also even if your search routine was being executed you probably wouldn't find the title. Remember this is what your file looks like:

Quote

Title : This is book 1
Author : GB Shaw
ISBN : 12345
Shelf_No : 34
Title : This is book 2
Author : R Smith
ISBN : 3345
Shelf_No : 3
Title : This is book 3
Author : Big Boy
ISBN : 23445
Shelf_No : 23


It would be much easier, IMO if you didn't have all the text in front of the relevant parts, and it would probably also be easier to have all the information on the same line, separated by some delimiter, a comma perhaps.

Quote

This is book 1, GB Shaw, 12345, 34
This is book 2, R. Smith, 3345, 3
This is book 3, Big Boy, 23445, 23


Then you would get one complete line, use one of the string search functions to find if the titles match. To accomplish this you'll need to redo your write function (it should be in it's own function) to change the way you write the file in the first place.

You should also be considering moving the search function into it's own function, to make the logic easier, and troubleshooting easier.

Don't be afraid of functions, they will make your program much easier to read and the logic much easier to follow. Strive to have your functions do as little as possible. By using functions it becomes easier to troubleshoot the program because you can write a "test" main() to just call that function with known values.

Jim


Jim
Was This Post Helpful? 1
  • +
  • -

#13 ragmar   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 107
  • Joined: 13-June 14

Re: the ifstream is not working assistance needed

Posted 15 September 2014 - 11:51 PM

this is were i end up it is less little of being shit and still messy in the output in the console,
but i will try more to make it better
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

struct ddpl{
    string author, title, show_title, choice, user_name, borrowed_book_title, borrowed_book_author;
    string isbn, shelf_no, id;
    //char id[15], borrow_date[15], due_date[15];
};

void addAndSearch(){
    ddpl adding, sch;
    cout << "for adding -1" << endl;
    cout << "for searching -2" << endl;
    int chs;
    do{
        cout << "enter choice: ";
        cin >> chs;cin.ignore();

        if(chs==1){
            ofstream added_books;
            added_books.open("DIC.txt", ios_base::app);

            cout << "Title: " ;
            getline(cin, adding.title);

            cout << "Author: ";
            getline(cin, adding.author);

            cout << "ISBN: ";
            cin >> adding.isbn;

            cout << "Shelf_No: ";
            cin >> adding.shelf_no;

            cout << "TITLE \t\t AUTHOR \t\t ISBN \t\t SHELF_No" << endl;
            cout << adding.title << "," << adding.author << "," << adding.isbn << "," << adding.shelf_no << endl;

            added_books << adding.title << "," << adding.author << "," << adding.isbn << "," << adding.shelf_no << endl;
            }
        else
            if(chs==2){
                string reader;
                ifstream read;
                read.open("DIC.txt");


                while(read.is_open() && !read.eof()){

                        //getline(read, adding.title);

                        while(read >> adding.title >> adding.author >> adding.isbn >> adding.shelf_no){

                            cout << adding.title << "," << adding.author  << "," << adding.isbn << "," << adding.shelf_no << endl << endl;
                                    }

               /* cout << "enter the title of the book to search: ";
                getline(cin, reader);
                if(reader==adding.title)
                    cout << adding.title << "," << adding.author  << "," << adding.isbn << "," << adding.shelf_no << endl << endl;
*/
                }}else
                    if(adding.title!=adding.title)
                        cout << "the book is not found in the library." << endl;

            else
                cout << "bye!!!" << endl;
    }while(chs!=0);

}
int main()
{
    addAndSearch();
}



and 'm not sure were to put the conditional statement to check were the "title of the book" is going to be searched for i have put it before , after and in the middle of the while loop but there is no effect at all
Was This Post Helpful? 0
  • +
  • -

#14 jimblumberg   User is offline

  • member icon

Reputation: 5521
  • View blog
  • Posts: 17,113
  • Joined: 25-December 09

Re: the ifstream is not working assistance needed

Posted 16 September 2014 - 03:25 AM

First I suggest you just read and print each "book" to insure you're reading the file correctly. Right now I doubt you are reading what you expect from the file.

Something like:
...
      else if(chs==2)
      {
         string reader;
         ifstream read("DIC.txt"); // No need to call the open() method, just use the constructor.

         while(read >> adding.title >> adding.author >> adding.isbn >> adding.shelf_no)
         { // Print the "book" to insure you're reading what you think your are.
            cout << adding.title << "," << adding.author  << "," << adding.isbn << "," << adding.shelf_no << endl << endl;
         }

      }
      else
         cout << "bye!!!" << endl;
...


If this doesn't look right you'll need to modify how you're reading the file. But until you can read what you write you won't be able to find anything. Also this is where a function would help.

void readFile(ddpl &adding)
{
   string reader;
   ifstream read("DIC.txt"); // No need to call the open() method, just use the constructor.

   while(read >> adding.title >> adding.author >> adding.isbn >> adding.shelf_no)
   { // Print the "book" to insure you're reading what you think your are.
      cout << adding.title << "," << adding.author  << "," << adding.isbn << "," << adding.shelf_no << endl << endl;
   }
}

int main()
{
   ddpl adding; // You really should use a meaningful name for that structure.
   readFile(adding);

   return 0;
}



Now you don't need to enter anything, just run the program. If the output is not what you expect then you know you need to change how you're reading the file. Once you can properly read what you wrote it'll be easy to incorporate into your existing program.

Jim
Was This Post Helpful? 1
  • +
  • -

#15 ragmar   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 107
  • Joined: 13-June 14

Re: the ifstream is not working assistance needed

Posted 16 September 2014 - 06:30 AM

#include <iostream>
#include <fstream>
#include <string>


using namespace std;

struct ddpl{
    string author, title, show_title, choice, user_name, borrowed_book_title, borrowed_book_author;
    string isbn, shelf_no, id;
    //char id[15], borrow_date[15], due_date[15];
};

void addAndSearch(){
    ddpl adding, sch;
    cout << "for adding -1" << endl;
    cout << "for searching -2" << endl;
    int chs;
    do{
        cout << "enter choice: ";
        cin >> chs;cin.ignore();

        if(chs==1){
            ofstream added_books;
            added_books.open("DIC.txt", ios_base::app);

            cout << "Title: " ;
            getline(cin, adding.title);

            cout << "Author: ";
            getline(cin, adding.author);

            cout << "ISBN: ";

            cin >> adding.isbn;

            cout << "Shelf_No: ";
            cin >> adding.shelf_no;

            cout << "TITLE \t\t AUTHOR \t\t ISBN \t\t SHELF_No" << endl;
            cout << adding.title << "," << adding.author << "," << adding.isbn << "," << adding.shelf_no << endl;

            added_books << adding.title << "," << adding.author << "," << adding.isbn << "," << adding.shelf_no << endl;
            }
        else
            if(chs==2){
                string reader;
                ifstream read;
                read.open("DIC.txt");


                while(read.is_open() && !read.eof()){

					getline(read, adding.title, ',');
					getline(read, adding.author, ',');
					getline(read, adding.isbn, ',');
					getline(read, adding.shelf_no, '.');

					cout << adding.title << "," << adding.author  << "," << adding.isbn << "," << adding.shelf_no << endl << endl;

                }}else
                    if(adding.title!=adding.title)
                        cout << "the book is not found in the library." << endl;

            else
                cout << "bye!!!" << endl;
    }while(chs!=0);

}
int main()
{
    addAndSearch();
}


I think this is better from the last one.

This post has been edited by ragmar: 16 September 2014 - 06:32 AM

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3