hello, I have a problem.I want to search by color , gender (these are the options) in a text file (called text.txt). My text file looks like that.
How can i do that?) ex. cin>>s , then find for "s" , if found cout all data about him (gender , brown , age , name , radioactive), if not , blabla...
male - Gender
brown - color
5 //and so on
Jorik
0
---------------------------
female
spotted
8
Madonna
0
---------------------------
female
black
2
LadyGaga
0
---------------------------
this is my code
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <ctype.h>
using namespace std;
ofstream fp("text.txt");
class Bunny
{
public:
string gender;
string color;
int age;
string name;
bool radioactive;
}g, c, a ,n ,r;
//------------------------------------------------------------------------------
string bunnynames(string x) {
string malenames[] = {"Jorik", "Vasik" , "BugsBunny" , "DonaldDuck"};
string femalenames[] = {"Jennifer" , "LadyGaga" , "Madonna" , "Natalie"};
srand(time(NULL));
string randommale, randomfemale , choice;
if(x == "male") {
randommale = malenames[rand() % (sizeof(malenames) / sizeof(malenames[0]))];
choice = randommale;
}
else if(x == "female")
{
randomfemale = femalenames[rand() % (sizeof(femalenames) / sizeof(femalenames[0]))];
choice = randomfemale;
}
return choice;
}
//------------------------------------------------------------
bool radioactive() {
return rand() < 2 * 100;
}
void CreateNewBunny() {
//string colors[] = {"white", "brown", "black", "spotted"};
cout<<endl;
cout<<"bunny's gender: ";
cin>>g.gender;
if(g.gender != "male" && g.gender != "female")
{
cout<<"Incorect Gender , exiting...";
exit(1);
}
else
{
fp<<g.gender<<endl;
}
//-------
cout<<"Bunny's color (white, brown , black or spotted):";
cin>>c.color;
//-------
cout<<"Bunny's age (1 - 10): ";
cin>>a.age;
if(a.age > 10) {
cout<<"Too old";
}
else if ( a.age < 1) {
cout<<"Impossible";
exit(1);
}
else
{
fp<<a.age<<endl;
}
//-------
string x = g.gender;
fp<<bunnynames(x)<<endl;
//-------
fp<<radioactive()<<endl;
//-------
fp<<"---------------------------"<<endl;
}
void menu() {
cout<<"---------Bunny Tree--------"<<endl;
cout<<"(1). Show all Bunnies"<<endl;
cout<<"(2). Add a new bunny"<<endl;
cout<<"(3). Delete a bunny"<<endl;
int i;
cin>>i;
switch(i) {
case 1:
//showallbunnies();
break;
case 2:
CreateNewBunny();
break;
case 3:
//deletebunny();
break;
default:
cout<<"incorect number, Would you like to continue? (y/n)"<<endl;
char ch;
cin>>ch;
if(ch == 'y')
{
system("clear");
return menu();
}
else
{
cout<<"Exiting...";
exit(1);
}
}
}
int main()
{
for(int i = 0; i <2; i++)
{
CreateNewBunny();
}
system("clear");
menu();
fp.close();
return 0;
}