small bug

This is a discussion on small bug within the C++ Programming forums, part of the General Programming Boards category; I use c++ to write a query exercise. My program can search int with it's return value. But I search ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    3

    Post small bug

    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?

    I change char* get_name() { return &name; }
    it doesn't work...... with two erroers


    Code:
    #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;
    }
    
    person operator+(const person & second);
    bool operator==(person&);
    
    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 *);     //bug 
            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[i]; // 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);
    }
    
    
    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),
             perrson("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[i]->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); 
    }
    Last edited by doublin; 10-24-2002 at 08:20 AM.

  2. #2
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    I cant see to read it without indentations, edit your post and use code tags!
    Couldn't think of anything interesting, cool or funny - sorry.

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    Please use code tags.

    I think the problem lies in the function get_name() you have it set to return a char * but you return a char []. Try changing it to this:

    char* get_name() { return &name; }

    That may do it.
    Best Regards,

    Bonkey

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    Originally posted by bonkey
    Please use code tags.

    Tags added....smilies killed

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    classic mistake. Your compairing pointer addresses rather than strings? ever heard of strcmp()?
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  6. #6
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,138
    You might want to work on your const-correctness. It's not neccessary, but helps a lot.

    Code:
    char* get_name() { return name; } 
    
    should be
    
    const char* get_name() const { return name; }
    Code:
    person(char* na, char* bk, int nu)
    
    should be
    
    person( const char* na, const char* bk, int nu)

    Code:
    void main(void)
    {
    ...
    }
    
    should be 
    
    int main()
    {
    ...
    	return 0;
    }
    To your problem: As pointed out, you should use the strcmp function.
    In addition, I left out all the variables and put some const-correctness in.
    Make sure the whole array arr is filled with valid persons or NULLs.

    PHP Code:
    personBorrowb::search_name( const charname_to_find)
    {
        for(
    int i 010i++) 
        {
            if( 
    arr[i] != NULL )
            {
                if( 
    strcmparr[i]->get_name(), name_to_find ) == 
                {
                    return 
    arr[i];
                }
            }
        }

        return 
    NULL;

    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    3
    Thank you very much.
    Your solution is great.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ATL bug of CComPtr?
    By George2 in forum Windows Programming
    Replies: 6
    Last Post: 04-07-2008, 07:52 AM
  2. want to make this small program...
    By psycho88 in forum C++ Programming
    Replies: 8
    Last Post: 11-30-2005, 01:05 AM
  3. FILES in WinAPI
    By Garfield in forum Windows Programming
    Replies: 46
    Last Post: 10-02-2003, 06:51 PM
  4. yhatzee, small straight
    By uglyjack in forum C++ Programming
    Replies: 2
    Last Post: 06-13-2002, 03:09 AM
  5. need help with a small bug
    By drharv in forum C Programming
    Replies: 3
    Last Post: 04-23-2002, 02:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21