Advertisement

small bug

Started by October 24, 2002 06:17 AM
0 comments, last by doublin_ken 22 years ago
I use c++ to write a query exercise. My program can search int. with it's return value. But I search the char*, when I use the same way, I can't get any return value. Does anyone know how to solve the problem?

#include <iostream.h>             
#include <string.h>
#include <windows.h>
class person                      // class of persons
   {
   private:
      enum {SIZE=40};             // size of name buffer
      char name[SIZE];            // person's name
      char book[SIZE];
      int age;                    // person's age
   public:
    	   person() {}
	   person(char* na, char* bk, int nu) : age(nu)
            { strcpy(name, na), strcpy(book, bk); }

 	   char* get_name()    { return name; } 	
	   char* get_book()    { return book; } 	
	   int get_age() const { return age; }

      void getData()              // get data from keyboard
         {
         cout << "\n   Enter name: ";
	cin >> name;//cin.getline(name, SIZE);
	cout << "\n   Enter book: "; cin >> book;
         cout << "   Enter age: ";  cin >> age;
         cin.ignore(10, '\n');
         }
      void putData()   const      // display data on screen
      {
         cout << "\n   Name = " << name;
         cout << "\n   Book = " << book;
         cout << "\n   Age = " << age;
      }

      friend istream& operator >> (istream& s, person& d);
      friend ostream& operator << (ostream& s, person& d);
      person operator+(const person & second);
      bool operator==(person&);
      void persin(istream& s)     // read file into ourself
      {
         s.read( (char*)this, sizeof(*this) );                                // data from file
       }
      void persout(ostream& s)    // write our data to file
      {
         s.write( (char*)this, sizeof(*this) );
      }
};
class Borrowb {
   private:
      enum {SIZE=100};         // size of array
      person* arr[SIZE];       // define array
      int total;               // number of objects in array
   public:
      Borrowb() : total(0) { }
      person* operator[](int) const;
      void insert(person*);
      person* search_name(char *); 
      person* search_age(int);
};

person* Borrowb::operator[](int n) const
   { return arr[n]; }

void Borrowb::insert(person* data)
{
   int j = 0;
   // find correct place
   while(j < total && data->get_age() > arr[j]->get_age() )   
      j++;
   for(int k=total; k>j; k--) // move higher elements up
      arr[k] = arr[k-1];
   arr[j] = data;             // insert new data
   total++;                   // now it has one more element
}

person* Borrowb::search_age(int num_to_find)
{
   for(int i = 0; i < 10; i++) {
	  person* ptr = arr;             // get ptr to person
      int num = ptr ->get_age();
	  if(num == num_to_find)          
         return ptr;                  
	}
   return NULL;                       
}

//this is a big bug that I can't find, why can't return it's ptr
person* Borrowb::search_name(char* name_to_find)
{
	//person per1(name_to_find," ",24),per2(" "," ",25);
	
	for(int i = 0; i < 10; i++) {
         person* ptr = arr;             // get ptr to person
         //per2.name = ptr -> get_name();
         char* name = ptr ->get_name();
	if(name == name_to_find)          
         return ptr;                  
         }
      return NULL;
}


person person::operator+(const person& second)
{
	person temp;
	int i = 0, j = 0;
	//char* a = new char[40];
	char a[40]; 
	char b[40];
	char* string3;

	cout << "input first string" << endl;
	cin >> a ;
	cout << "input second string" << endl;
	cin >> b ;
	string3 = strcat(a, b);
	cout << string3;
	return temp;
}

bool person::operator==(person& second)
{
    return(strcmp(this -> name, second.name) == 0);
}

// get data from disk
istream& operator >> (istream& s, person& d)
{
   d.persin(s);
   return s;
}

// write data to disk
ostream& operator << (ostream& s, person& d)
{
   d.persout(s);
   return s;
}

void main(void)
{  
   Borrowb sa;
   person* pemp; 
   char name[30], choice;
   int num;
   //search_ name, search_book, search_age
   // array of persons   //overloading [] 
   person perarr[10] =     
     { person("Webley", "Mfc book" , 46),    
       person("Suzuki", "Math book" , 67),  
       person("Smith", "Chinese book" ,37),
       person("Gonzalez", "English book", 27),
       person("Wong", "Computer book", 43),
       person("LeMonde", "Science book", 78),
       person("Weinstein", "Art book" , 14),
       person("DeCarlo", "Physical book" , 22),
       person("Nguyen", "Philophy book" , 39),
       person("O'Grady", "Algorithm book" , 57) };
   //insert ten data
   for(int j=0; j<10; j++)       // insert address of each
      sa.insert(perarr+j);      // person into SortedArray

   //display ten data
   for(int i=0; i<10; i++)       // display data in order
   {
      cout << "\nperson " << (i+1);
      sa->putData();
   }
   
   do {
   cout << "\n  'N'search_age  \n 'M'search_name  \n 'Q'exit " << endl;
   cout << "Which is your choice?" ;
   cin >> choice; choice = toupper(choice);   
   if (choice == 'Q') break;
   switch(choice)
   {
   case 'N':
	   cout << "enter query num" << endl ;   //query age
	   cin >> num;
	   pemp = sa.search_age(num);   
	   if(pemp != NULL)
	   {
	       cout << "\nPerson with that number is";
		   pemp->putData();
	   }
	   else
	       cout << "\n No such peson name in database.";
	   break;
   case 'M':
          //query name
	cout << "enter query name" << endl ;  		cin >> name;
	pemp = sa.search_name(name);   
	if(pemp != NULL)
	{
	   cout << "\nPerson with that name is";
	   pemp->putData();
         }
	else
	   cout << "\n No such peson name in database.";
	   break;
	default: break;
    }
   }while (true);  
}  
</pre>




<SPAN CLASS=editedby>[edited by - doublin_ken &#111;n October 24, 2002 10:43:02 AM]</SPAN>  </i>   </pre> 
1: For a small bug, that is alot of code.
2: Include iostream and string instead of iostream.h and string.h.
3: You are comparing the pointers to the strings, not the strings them selves, as you are using an array of characters.

If you are going to include the string template, you might as well using real strings! It would have worked then!

You can change the line if(name == name_to_find) to if(strcasecmp(name, name_to_find) == 0) and it should work.

[edited by - smart_idiot on October 24, 2002 7:38:50 PM]
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.

This topic is closed to new replies.

Advertisement