vendredi 27 octobre 2017

compare two strings from different text files in c++

i am trying to get 2 different strings from two different text files and compare them . i am getting a crash when the second loop start

here is my customer text file

912345678 Saleh Al-Yaaroubi, 912345678@abc.ac.ae, 8

0-239-23635-0 2-99521-453-1 0-521-43681-8 0-7637-8060-x

3-7908-1335-4 0-201-40015-4 0-19-431735-1 3-7908-1325-7

567891234 Khaulah Al-Lawati, 567891234@abc.ac.ae, 1

0-521-43681-8

and here is my book text file

5-17-525281-3 C++ Programing, Davender Malik, 210

3-7908-1335-4 Discrete Structures, Davender Malik, 185

...

and here is what i am trying to do :

#include<iostream>
#include<string>
#include<fstream>
using namespace std;
ifstream in_books("book.txt");
ifstream in_customer("customer.txt");

struct book {
    string ISBN , title , author;
    int price;
};

class Customer{
private:
    int  numberOfBooks , totalCost;
    string ID , name , email ;
    book books[];
    void calculateCost();
public:
    void set(string );
    void print() const;
    int getTotalCost() const;
    Customer(string = "000000000" );
};

int main(){
    Customer w("789123456");
    return 0;
}

void Customer::set(string id ) {
    ID = id ;
    // just to save the full name of the customer name  append it to the name of the customer
    string save_lastname ;
    int a = 10;
    int i = 0 ;
    int x = 0 ;
    for(; i<a; i++){
        // getting the ID , NAME , EMAIL AND THE NUMBER OF BOOKS OF THE CUSTOMER FROM THE CUSTOMER TEXT FILE
        getline(in_customer ,ID, ' ') ;
        getline(in_customer ,name, ' ') ;
        getline(in_customer ,save_lastname  , ',') ;
        name.append(save_lastname) ;
        getline(in_customer , email , ',' ) ;
        in_customer >> numberOfBooks;
        cout<<ID<<endl;
        cout<<name<<endl;
        cout<<email<<endl;
        cout<<numberOfBooks<<endl;
        // the compiler shows the data above 
        // CRASHING START HERE !
            for( ; x < numberOfBooks  ; x++ ) {
            getline(in_books ,books[x].ISBN, ' ') ;
            getline(in_books ,books[x].title, ',') ;
            getline(in_books ,books[x].author, ',') ;
            in_books >> books[x].price ;
            cout<<books[x].ISBN<<endl;
            cout<<books[x].title<<endl;
            cout<<books[x].author<<endl;
            cout<<books[x].price <<endl ;
           /*if(books[x].ISBN.compare(ID) == 0){
                cout<< books[x].title <<"\t"<<books[x].author ;
            }*/
       }
}
calculateCost();
}

thanks in advance

Aucun commentaire:

Enregistrer un commentaire