Thread: Inheritance Phonebook Program

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    86

    Inheritance Phonebook Program

    The program is supposed to allow the user to create student/staff entries, search by last name, and print all phonebook entries on the screen by using arrays. All of the sections of code are giving me fits debugging and I'm receiving quite a few errors I don't think I should.

    Questions:

    How come StudentIndividual is not a class nor are my set functions within student "found"?

    How come within StaffIndividual.h it says no ";" before using?

    And is my declaration of the arrays for my objects correct? I'm getting an error for the "]".

    Any other constructive comments or pointers that will help me progress would be greatly appreciated.


    Code:
    //Phonebook program
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include "Individual.h"
    #include "StudentIndividual.h"
    #include "StaffIndividual.h"
    
    using namespace std;
    ofstream fileout;
    int main()
    {
     int selection = 0;
     StudentIndividual studentInfo[10];
     StaffIndividual staffInfo[10];
     cout<<"Welcome to your digital phone book!"<<endl;
     cout<<"Please choose from one of the following commands"<<endl;
     cout<<"1. Create new student entry\n2. Create new staff entry\n3. Search by last name\n4. Display all entries"<<endl;
     switch(selection)
     {
     case 1:
      cout<<"Contact Info:\nFirst Name: "<<endl;
      cout<<"Last Name: "<<endl;
      cout<<"Phone Number: "<<endl;
      cout<<"Hours taken: "<<endl;
      cout<<"Grade Point Average: "<<endl;
      cin>>studentInfo[++];
      break;
     case 2:
      cout<<"Contact Info:\nFirst Name: "<<endl;
      cout<<"Last Name: "<<endl;
      cout<<"Phone Number: "<<endl;
      cout<<"Job title: "<<endl;
      cout<<"Year employeed: "<<endl;
      cin>>staffInfo[++];
      break;
     case 3:
      cout<<"Please enter the last name of the contact you want to retrieve."<<endl;
      //for loop to compare strings
      break;
     case 4:
      cout<<"All entries in phonebook:"<<endl;
      //for loop to run through arrays
      break;
    }
    Code:
    // Base class
    // Individual.h
    #ifndef INDIVIDUAL_H
    #define INDIVIDUAL_H
    #include <string>
    using namespace std;
    class Individual
    {
    public:
     //constructor
     Individual (string, string, string);
     //set functions
     void setFirstName (string);
     void setLastName (string);
     void setPhoneNumber (string);
     //get functions
     string getFirstName();
     string getLastName();
     string getPhoneNumber();
     //overloaded
     friend std::istream& operator>> (istream &, Individual &);
     friend std::ostream& operator<< (ostream &, Individual &);
    protected:
     string firstName;
     string lastName;
     string phoneNumber;
    }
    #endif
    Code:
    // Derived class
    // StudentIndividual.h
    #ifndef STUDENTI_H
    #define STUDENT_H
    #include <string>
    #include "Individual.h"
    class StudentIndividual : public Individual
    {
    public:
     //constructor
     StudentIndividual::StudentIndividual(double, int);
     //set functions
     void setGPA (double);
     void setHours (int);
     //get functions
     double getGPA();
     int getHours();
    private:
     double gpa;
     int hours;
    }
    #endif
    Code:
    // Base class
    // Individual.h
    #ifndef STAFF_H
    #define STAFF_H
    #include <string>
    #include "Individual.h"
    using namespace std;
    class StaffIndividual : public Individual
    {
    public:
     //constructor
     StaffIndividual::StaffIndividual(string, int);
     //set functions
     void setPosition(string);
     void setYearEmployeed (int);
     //get functions
     string getPosition();
     int getYearEmployeed();
    private:
     string position;
     int yearEmployeed;
    }
    #endif
    Code:
    // C++
    // Individual.cpp
    // Individual class member functions
    #include "Individual.h"
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    using namespace std;
    extern ofstream fileout;
    //constructor
    Individual::Individual(string first, string last, string number)
    {
     setFirstName(first);
     setLastName (last);
     setPhoneNumber(number);
    }
    //set functions
    void Individual::setFirstName(string f)
    {
     firstName = f;
    }
    void Individual::setLastName(string l)
    {
     lastName = l;
    }
    void Individual::setPhoneNumber (string p)
    {
     phoneNumber = p;
     
    }
    // get functions
    string Individual::getFirstName() 
    {
     return firstName;
    }
    string Individual::getLastName() 
    {
     return lastName;
    }
    string Individual::getPhoneNumber() 
    {
     return phoneNumber;
    }
    istream& operator>> (istream &cin, Individual &obj)
    {
     string f, l, s;
     cin >> f >> l >> s;
     obj.firstName = f;
     obj.lastName = l;
     obj.phoneNumber = s;
     return cin;
     
    }
    ostream& operator<< (ostream &out, Individual &obj)
    {
     fileout << "\n" << obj.getFirstName() << " " << obj.getLastName() << " " << obj.getPhoneNumber();
     return fileout;
    }
    Code:
    // C++
    // StudentIndividual.cpp
    // StudentIndividual class member functions
    #include "StudentIndividual.h"
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    using namespace std;
    extern ofstream fileout;
    //constructor
    StudentIndividual::StudentIndividual(double average, int hour)
    {
     //:Individual (first, last, number);
     setGPA(average);
     setHours (hour);
    }
    //set functions
    void StudentIndividual::setGPA(double g)
    {
     gpa = g;
    }
    void StudentIndividual::setHours(int h)
    {
     hours = h;
    }
    // get functions
    double StudentIndividual::getGPA() 
    {
     return gpa;
    }
    int StudentIndividual::getHours() 
    {
     return hours;
    }
    Code:
    // C++
    // StaffIndividual.cpp
    // StaffIndividual class member functions
    #include "StaffIndividual.h"
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    extern ofstream fileout;
    //constructor
    StaffIndividual::StaffIndividual(string pos, int yr)
    {
     //:Individual (first, last, number);
     setPosition(pos);
     setYearEmployeed (yr);
    }
    //set functions
    void StaffIndividual::setPosition (string pos)
    {
     position = pos;
    }
    void StaffIndividual::setYearEmployeed(int year)
    {
     yearEmployeed = year;
    }
    // get functions
    string StaffIndividual::getPosition() 
    {
     return position;
    }
    int StaffIndividual::getYearEmployeed() 
    {
     return yearEmployeed;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    How come StudentIndividual is not a class nor are my set functions within student "found"?
    Probably caused by the following error.
    How come within StaffIndividual.h it says no ";" before using?
    It looks like you're missing the closing semicolon of your class.

    Question: Why are you using the using namespace declaration inside your header file? That is considered a bad practice. You really should be using the scope resolution operator:: in your headers.

    As far as the rest of the errors, post the complete error messages, these messages have important information embedded within them to aid in finding and fixing the errors.

    Also inside your class definitions you don't scope the class. And you really should name the parameters in the declaration, this will help document the class.

    [
    Code:
     //constructor
    // StudentIndividual::StudentIndividual(double, int);
    StudentIndividual(double, int);

    Jim
    Last edited by jimblumberg; 04-17-2013 at 11:56 AM.

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    86
    My instructor preferred the using namespace because he says its makes the body of the code look "cleaner"

    But most of my errors are coming from namespace though, also I have changed the constructors to how Jim recommended.

    Here are the list of errors.

    1> StudentIndividual.cpp
    1>f:\project4\project4\studentindividual.h(11): error C2143: syntax error : missing ';' before 'using'
    1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\iostream(10): error C2143: syntax error : missing ';' before 'namespace'
    1>f:\project4\project4\studentindividual.cpp(16): error C2512: 'Individual' : no appropriate default constructor available
    1> StaffIndividual.cpp
    1>f:\project4\project4\staffindividual.h(11): error C2143: syntax error : missing ';' before 'using'
    1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\iostream(10): error C2143: syntax error : missing ';' before 'namespace'
    1>f:\project4\project4\staffindividual.cpp(15): error C2512: 'Individual' : no appropriate default constructor available
    1> Phonebook.cpp
    1>f:\project4\project4\studentindividual.h(11): error C2143: syntax error : missing ';' before 'using'
    1>f:\project4\project4\staffindividual.h(11): error C2143: syntax error : missing ';' before 'using'
    1>f:\project4\project4\phonebook.cpp(11): error C2143: syntax error : missing ';' before 'using'
    1>f:\project4\project4\phonebook.cpp(17): error C2512: 'StudentIndividual' : no appropriate default constructor available
    1>f:\project4\project4\phonebook.cpp(18): error C2512: 'StaffIndividual' : no appropriate default constructor available
    1>f:\project4\project4\phonebook.cpp(32): error C2059: syntax error : ']'
    1>f:\project4\project4\phonebook.cpp(40): error C2059: syntax error : ']'
    1>f:\project4\project4\phonebook.cpp(52): fatal error C1075: end of file found before the left brace '{' at 'f:\project4\project4\phonebook.cpp(15)' was matched
    1> Individual.cpp
    1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\iostream(10): error C2143: syntax error : missing ';' before 'namespace'
    1> Generating Code...

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by carpeltunnel
    My instructor preferred the using namespace because he says its makes the body of the code look "cleaner"
    That is acceptable in a source file, after the headers have been included, or in a local scope in a header file, but is not acceptable at file scope in a header file because it would affect wherever the header is included.

    Have you checked to see if your class definition has the terminating semi-colon as suggested?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    You are missing the closing semicolon on most if not all your classes. Classes and structures must have a semicolon following the closing brace:
    Code:
    class Test
    {
    
    };  // The semicolon is mandatory.
    Also you should stop using the global variable, the ofstream instance. Learn to properly pass this variable to and from your functions.

    Jim
    Last edited by jimblumberg; 04-17-2013 at 12:15 PM.

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    86
    Quote Originally Posted by laserlight View Post
    Have you checked to see if your class definition has the terminating semi-colon as suggested?
    EDIT: Jim's second post answered my question.

    But I did add the terminating colon's and the using namespace issues still poses.

    Also, I am not trying to question you guys but the text I am lookin at uses "using namespace std" globablly in both the .cpp and .h files. I just want to understand fully what I'm doing and why.
    Last edited by carpeltunnel; 04-17-2013 at 12:30 PM. Reason: Solved

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by carpeltunnel
    I'm sorry this is clicking but I am look at some code in my textbook, I don't see where I could be missing a ";" except in my contructor (which I have).
    Refer to jimblumberg's post #5 for an example. If you still don't get it, compile this program:
    Code:
    class X
    {
    };
    
    int main()
    {
        X x;
    }
    Then try to compile this program:
    Code:
    class X
    {
    }
    
    int main()
    {
        X x;
    }
    and observe the difference.

    Quote Originally Posted by carpeltunnel
    Also, I am not trying to question you guys but the text I am lookin at uses "using namespace std" globablly in both the .cpp and .h files.
    Then the text that you are looking at is wrong where the header files are concerned.

    Quote Originally Posted by carpeltunnel
    I just want to understand fully what I'm doing and why.
    Consider this program:
    Code:
    #include <string>
    #include "header1.h"
    #include "header2.h"
    
    using library2::string;
    
    int main()
    {
        string str; // #1
        // ...
    }
    The library2 namespace is declared in header2.h and happens to define a class type named string within that namespace. The author of this program wants to use that string class (guitar strings?), and so decided to have that using declaration, and then qualify std::string where needed. Unfortunately, header1.h has a using namespace std; Consequently, the line labelled #1 uses an ambiguous name: does string refer to std::string or library2::string? This is surprising: the author of this program expected that the name should refer to library2::string because of that using declaration. The using directive in header1.h has "poisoned" his/her code.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >>StudentIndividual studentInfo[10];
    Have a read: SourceForge.net: Safer arrays in Cpp - cpwiki

    >>cin>>staffInfo[++];
    Syntax error. What are you trying to do?

    >>Individual (string, string, string);
    Have a read: SourceForge.net: Do not remove parameter names - cpwiki

    >>StudentIndividual::StudentIndividual(double, int);
    StudentIndividual(double, int);
    is enough.

    >>StaffIndividual::StaffIndividual(string, int);
    Same here.

    >>Individual::Individual(string first, string last, string number)
    Prefer passing objects via const reference, ie const std::string&.

    Code:
     string f, l, s;
     cin >> f >> l >> s;
     obj.firstName = f;
     obj.lastName = l;
     obj.phoneNumber = s;
    f, l, s is not descriptive names.
    Prefer to use the public interface, if possible.

    >>istream& operator>> (istream &cin, Individual &obj)
    Don't name the parameter cin. You will hide the global cin or possibly confuse people with the global cin.

    Code:
    ostream& operator<< (ostream &out, Individual &obj)
    {
     fileout << "\n" << obj.getFirstName() << " " << obj.getLastName() << " " << obj.getPhoneNumber();
     return fileout;
    }
    fileout is not declared. Possibly you meant out.

    Finally,
    Missing semicolon after all your class definitions,
    and one space for indentation is not acceptable. I recommend a minimum of 4.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Sep 2012
    Posts
    86
    I'm not processing namespace. I have googled and read many links plus looked over my code.

    When I do take using namespace std; out of my header files I receive many errors that none of the identifiers can be found in the class.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by carpeltunnel View Post
    I'm not processing namespace. I have googled and read many links plus looked over my code.
    What does that mean?

    When I do take using namespace std; out of my header files I receive many errors that none of the identifiers can be found in the class.
    Yes, you have to explicitly quality them with "std::".
    The problem is that you are forcing "using namespace std" on everyone who includes the header, which is a Bad Thing™.
    (Remember: headers are essentially global, while .cpp files are local - the code inside isn't going to be "reused".)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    What does that mean?
    To put it in our jargon: he doesn't grok namespaces.

    Soma

  12. #12
    Registered User
    Join Date
    Sep 2012
    Posts
    86
    Alright all, thanks for the help I have got my code to work except for my find by last name and display. I am struggling to figure out how to compare the array element to the string variable I read in from the user.

    Here is my current attempt

    Code:
    case 3:
                
    cout<<"Please enter the last name of the contact you want to retrieve."<<endl;
    cin>>lastname;
                
    
    for (i = 0; i < stucount; i++){
         if (studentInfo[i] == lastname){
                  cout<<studentInfo[i];
          }
    }
    
                
    for (i = 0; i < staffcount; i++){
             if (staffInfo[i] == lastname){
                     cout<<staffInfo[i];
              }
    } 
    
                
    break;

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Provide more context. What is not working? What is your full current working code - or even better - a minimal compilable working example that demonstrates the problem?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Sep 2012
    Posts
    86
    1>f:\project4\project4\phonebook.cpp(77): error C2678: binary '==' : no operator found which takes a left-hand operand of type 'StudentIndividual' (or there is no acceptable conversion)

    That is the error I receive, same for Staff. Same error for a single "=" to compare the object in the array to the last name the user inputs.

    The goal is compare the specific object in the array to the inputted last name and display a match on the screen.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Do you intend to match the last name of the individuals? If so, how about using .GetLastName()?
    To be precise, you are trying to compare an individual to a name. How do you do that? I don't know. The compiler doesn't know. It doesn't make sense.
    Also, = is for assignment, == is for comparison.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program of inheritance
    By student111 in forum C++ Programming
    Replies: 14
    Last Post: 06-06-2012, 01:21 PM
  2. PHONEBOOK in c
    By chie22 in forum C Programming
    Replies: 6
    Last Post: 10-17-2011, 03:26 PM
  3. Need a hand with a Phonebook program
    By spade914 in forum C Programming
    Replies: 2
    Last Post: 10-05-2009, 10:28 PM
  4. phonebook help
    By noob2c in forum C Programming
    Replies: 5
    Last Post: 04-19-2003, 01:51 PM
  5. phonebook
    By jk81 in forum C Programming
    Replies: 6
    Last Post: 09-25-2002, 04:41 AM