vendredi 27 octobre 2017

compare two strings from different text files

i am trying to compare between two strings from 2 different text files and then if these 2 strings are the same then i should do something else i am getting a crash bcz of the second loop here is my codes i tried everything possible but i didnt find anything usefull for me

#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 ;
    string save_lastname ;
    int a = 10;
    int i = 0 ;
    int x = 0 ;
    for(; i<a; i++){
        in_customer >> ID ;
        getline(in_customer ,name, ' ') ;
        getline(in_customer ,save_lastname  , ',') ;
        name.append(save_lastname) ;
        in_customer >> email >> numberOfBooks;
        cout<<ID<<endl;
        cout<<name<<endl;
        cout<<email<<endl;
        cout<<numberOfBooks<<endl;
        // the program print the above data and then the crash start
        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(ID.compare(books[x].ISBN) == 0){
                cout<< books[x].title <<"\t"<<books[x].author ;
            }*/
       }
    }
    calculateCost();
}

thanks in advance

Aucun commentaire:

Enregistrer un commentaire