I am working on my project and stuck on this point.
I am writing my data in Heavy Bike.txt
file and everything works perfectly except the searching feature, I am searching the details of bike with respect to its color that user enter.
For Example : When user enter color Black
the black coloured bikes in the .txt file should display on console
Problem : When I enter the color then my program is not displaying the correct information
The data I entered the .txt file are like this
234
543
Black
Heavy Bike
2020
123
943
Red
Heavy Bike
2019
These are details of 2 Bikes
But the output I got is this
Enter Color of Bike you want : Black
Found
0 //registration number
943 // id
Red // color
Heavy Bike // bike type
2019 //model
I enter the color black but it is showing the red color bike details. In addition, it is showing the registration number 0
Here is my code so far
Writing data to the text file
void writingDet()
{
ofstream file;
if (bike_typ == "Heavy Bike")
{
file.open("Heavy Bike.txt", ios::app);
if (file.is_open())
{
file << reg_no << endl;
file << id << endl;
file << Bike_clr << endl;
file << bike_typ << endl;
file << model << endl;
file << endl;
}
else
{
cerr << "Your File cannot be open" << endl;
}
}file.close();
}
Search Function
int search(string clr)
{
string line;
ifstream fm;
fm.open("Heavy Bike.txt");
for (line; getline(fm, line);)
{
if (line.find(clr) != string::npos)
{
cout << "\n\nFound" << endl;
fm >> reg_no;
cout << reg_no << endl;
fm >> id;
cout << id << endl;
getline(fm, Bike_clr);
cout << Bike_clr << endl;
getline(fm, bike_typ);
cout << bike_typ << endl;
fm >> model;
cout << model << endl;
}
}
return 1;
}
input data function
void inputData()
{
cout << endl;
cout << "Enter registration no of Car : " << ' ';
cin >> reg_no;
cout << "Enter ID of Car : " << ' ';
cin >> id;
cin.ignore();
cout << "Enter Car Color : " << ' ';
getline(cin, car_clr);
cout << "Enter Car Type : " << ' ';
getline(cin, car_typ);
cout << "Enter Model (year) : " << ' ';
cin >> crmodel;
cin.ignore();
cout << endl;
cout << endl;
}
main
int main()
{
Vehical* vc[5];
Bike bk;
string clr;
bool found;
for (int i = 0; i < 2; i++)
{
vc[i] = new Bike;
vc[i] = &bk;
vc[i]->inputData();
vc[i]->writingDet();
}
cin.ignore();
cout << "\nEnter Color of Bike you want : " << ' ';
getline(cin, clr);
for (int k = 0; k < 2; k++)
{
found = vc[k]->search(clr);
if (found)
{
break;
}
else
{
continue;
}
}
cout << endl;
system("PAUSE");
return 0;
}
In short, when I input specific color then that colored bike details in .txt file
should display. Thanks you so much .
PS. I am using abstract class in my program and here is the complete code https://ideone.com/flXnOU
Aucun commentaire:
Enregistrer un commentaire