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;
}

Recommended Answers

All 5 Replies

Your description of the problem you're having wasn't very clear; you say you want to search from an input file, but right now, all you're doing is reading in data from the console. Can you explain the problem better?

If you don't mind me asking, why do you have the createNewBunny() function instead of a conventional constructor?

Also, can you please fix the code indentation? As it is, it's very inconsistent, making it hard to read the program; I had to copy it into an editor and use AStyle to be able to read it at all.

Finally, you might want to move the references to system("clear") into a function, for the reasons I explained in this thread, as well as because most of the posters here are running under Windows rather than *nix; having a function for it would make it easy to change it to system("cls") (the Windows equivalent of system("clear") ) if they want to test the code.

i'm using linux that's why i use system("clear");
all you're doing is reading in data from the console. - i know)) but the problem i meet , is the next task i have written above. that's why i asked for help). I don't know what to do... What i must do firstly, What must i start with.

Actually, as you have it now, it isn't going to work, because your createNewBunny() function is broken. Right now, you are assigning each of the different attributes - gender, color, age, name and radioactive - to a different Bunny object. You never create a single actual Bunny.

I would start to fix this by separating the declarations of the Bunny objects from the class, by deleting object g, c, a, n, and r objects.

I would then separate the creation of the Bunny objects from the input and output by creating a Bunny() constructor method for the class.

And re-write the createNewBunny() function so that it only gathers the information from the user.

Finally, change main() to reflect this new state of affairs Putting this all together you would have something like this:

#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <cstdlib>
#include <cctype>
#include <ctime>

using namespace std;

#define DEBUG 0
#define WINDOWS 0

void clearScreen()
{
#if (DEBUG != 1)
#if (WINDOWS == 1)
    system("cls");      // we're running under Windows
#else
    system("clear");    // we're in a civilized operating system
#endif
#endif
}


ofstream fp("text.txt");

class Bunny
{
private:
    string gender;
    string color;
    int age;
    string name;
    bool radioactive;
public:
    Bunny(string g, string c, int a, string n, bool r);
};


Bunny::Bunny(string g, string c, int a, string n, bool r): name(n), radioactive(r)
{
    if (g != "male" && g != "female")
        throw ("Invalid Gender");
    else
        gender = g;

    if (c != "white" && c != "brown" && c != "black" && c != "spotted")
        throw ("Invalid Color");
    else
        color = c;

    if (a < 0 || a > 10)
        throw ("Invalid age");
    else
        age = a;
}



//------------------------------------------------------------------------------
string bunnynames(string gender)
{
    string malenames[] = {"Jorik", "Vasik" , "BugsBunny" , "DonaldDuck"};
    string femalenames[] = {"Jennifer" , "LadyGaga" , "Madonna" , "Natalie"};
    srand(time(NULL));
    string randommale, randomfemale , choice;
    if(gender == "male")
    {
        randommale = malenames[rand() % (sizeof(malenames) / sizeof(malenames[0]))];
        choice = randommale;
    }
    else if(gender == "female")
    {
        randomfemale = femalenames[rand() % (sizeof(femalenames) / sizeof(femalenames[0]))];
        choice = randomfemale;
    }
    return choice;

}
//------------------------------------------------------------

bool radioactive()
{
    return rand() <  2 * 100;
}

Bunny* GetNewBunny()
{
    string gender;
    string color;
    int age;

    cout << endl;
    cout << "bunny's gender: ";
    cin  >> gender;

    //-------
    cout <<"Bunny's color (white, brown , black or spotted):";
    cin  >> color;
    //-------
    cout << "Bunny's age (1 - 10): ";
    cin  >> age;

    string name = bunnynames(gender);
    bool r = radioactive();

    return (new Bunny(gender, color, age, name, r));
}

bool menu(vector<Bunny*> bunnies)
{
    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:
        bunnies.push_back(GetNewBunny());
        break;
    case 3:
        //deletebunny();
        break;
    default:
        cout<<"incorrect number, Would you like to continue? (y/n)"<<endl;
        char ch;
        cin>>ch;
        if(ch == 'y')
        {
            clearScreen();
            return true;
        }
        else
        {
            return false;
        }
    }
    return true;
}

int main()
{
    vector<Bunny*> bunnies;

    for(int i = 0; i <2; i++)
    {
        bunnies.push_back(GetNewBunny());
    }

    clearScreen();
    while(menu(bunnies));

    for (unsigned i = 0; i < bunnies.size(); i++)
    {
        delete bunnies[i];
    }

    fp.close();
    return 0;
}

I realize that this doesn't solve the problem you're asking, but it is a necessary fix before you can address that one.

thank you very much ;)

Don't mention it. BTW, I think I found another thing you'll want to fix: the radioactive() function doesn't do what I gather you want it to. Try this:

bool radioactive()
{
    return (rand() % 100) <  2;
}

The modulo operator ( % ) returns the remainder of the first number divided by the second number, so if you have 123 % 100, you get 23 back. The new version of the function should return true 2 percent of the time, now (on 0 or 1).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.