how can i compare the a private variable of a class and a value in the column of a text file.
there is a syntax error in my code while comparing. senario is i am getting bus details like busno, source , destination,typ e of bus, price/head . I should compare the busno which i get through object to the bus no which is present in a .txt file. and contents in the text file is as the format below.
|1488|xxx|uuuu| A/c|150.
|1422|mmm|oooo| nonA/C|900.
there is a syntax error in my code while comparing. senario is i am getting bus details like busno, source , destination,typ e of bus, price/head . I should compare the busno which i get through object to the bus no which is present in a .txt file. and contents in the text file is as the format below.
|1488|xxx|uuuu| A/c|150.
|1422|mmm|oooo| nonA/C|900.
Code:
#include<iostream> #include<iomanip> #include<fstream> #include<string> class add { private: int bus_no; int route_no; string source; string dest; int time; int ticket_price; string type; public: void enter_bus() { char temp[1000],busno[5]; string type("A/C"); cout<<"Enter Bus_No::"; cin>>busno; bus_no=string(busno); cout<<"Enter Bus_Route::"; cin>>route_no; cout<<"Enter Source::"; cin>>source; cout<<"Enter Destination::"; cin>>dest; cout<<"Enter Time(Hrs:Min)::"; cin>>time; cout<<"Enter Ticket_Price::"; cin>>ticket_price; // Type: cout<<"Type of Bus (A/C or NonA/C)::"; // cout<<"Enter Type::"; cin>>type; } void get_busno() { bus_no; } void display__bus() { cout<<"Bus details"<<endl; cout<<"|"<<bus_no<<"|"; <<setw(10)<<route_no<<"|"; <<setw(15)<<source<<"|"; <<setw(15)<<dest<<"|"; <<setw(8)<<time<<"|"; <<setw(6)<<type<<"|"; <<setw(3)<<no_of_seats<<"|"; <<setw(4)<<ticket_price<<endl; } void intofile__bus() { cout<<"|"<<bus_no<<"|"; <<setw(10)<<route_no<<"|"; <<setw(15)<<source<<"|"; <<setw(15)<<dest<<"|"; <<setw(8)<<time<<"|"; <<setw(6)<<type<<"|"; <<setw(3)<<no_of_seats<<"|"; <<setw(4)<<ticket_price<<endl; } }; void main { add obj; fstream file; file.open("Bus.txt",std::ios_base::ate | std::ios_base::in | std::ios_base::out ); file.seekg(0, std::ios_base::end); unsigned long length = file.tellg(); int obj_length=sizeof(obj); int p=length/obj_length; int n; int option; char choice; int flag=0; add obj2[p]; file.seekg(0,ios::beg); cout<<"Want to enter details in the file (y/n)?"<<endl; cin>>choice; while((choice=='y') || (choice=='Y')) { cout<<"Enter the option 1 for adding details:"<<endl; cin>>option; switch (option) { case 1: obj.enter_bus(); for(n=0;n<=p;n++) { file.read((char *)&obj2[n],sizeof(obj2[n])); if(strcmp(obj2[n].get_busno(),obj.get_busno())==0) { cout<<"Sorry the bus already exist"<<endl; flag=2; break; } else { flag=1; } } default: cout<<"Please enter the option 1 for adding details"<<endl; } if(flag==1) { obj.enter_bus(); obj.intofile_bus(); file.write((char *)&obj,sizeof(obj)); cout<<"Record inserted successfully..."; } else { cout<<"Want to enter the details again(y/n)?"<<endl; cin>>choice; } }
Comment