I'm kinda stuck with the first part of this assignment.

Design and implement a program for a phone book application.  Create classes Name, PhoneNumber, and PhoneBook that are employed by the provided phone book program, PhoneBookProgram.cpp.

The Name class is where I'm at. 

Class Name
•   Represents the name for a listing in a phone book (example: Phil’s Pizza)
•   Overloads stream insertion (<<) and stream extraction (>>) operators
•   Stream extraction operator validates text entered by user for a name
o   skips any initial white space typed by user
o   throws a runtime exception if input name is invalid
   name must be at least 1 character and a maximum of 25 characters
o   does not modify Name object if input name is invalid

So here is my code for the Name section.  I am having trouble with figuring out how to throw the exception if the name is invalid, due to not remembering how to check if the name is in between 1 and 25 characters.  I know I need to use the >, <, || operators, but I can't remember how to compare a string.  Any help would be appreciated.

#ifndef Name_H
#define Name_H

#include <iostream>
#include <string>
#include <stdexcept>
using namespace std;


class Name : public runtime_error

{
    friend ostream &operator<<( ostream &, const Name & );
    friend istream &operator>>( istream &, Name & );
public:
Name()
    : runtime_error("Invalid Name") {}





private
string name;
};
#endif

#include <iostream>
#include <iomanip>
#include "Name.h"
using namespace std;

ostream &operator<< ( ostream &output, const Name &name)
{
    output << name.name;
    return output;

}

istream &operator>>(istream &input, Name &name )
{

    input>>name.name;
}

how to check if the name is in between 1 and 25 characters. I know I need to use the >, <, || operators, but I can't remember how to compare a string. Any he

Just use std::string's length() method to find out how many characters are in the string. Just remember < is less than, > is greater than == is equal to, then there are <= and >=. Write that down on a piece of paper and keep it by your computer so that you can refere to it until you have it memorized.

Are you sure that you want to inherit from runtime_error (that is, that a Name is a specific example of a runtime_error), or just throw such an error?

Schol -R- LEA I just noticed that I am supposed to THROW the exception not inherit. My main is suppose to contain the try/catch blocks. The main is supplied but I need to modify it to make it work correctly. I will post the full problem in a sec

Here is the full problem along with the main.

Design and implement a program for a phone book application. Create classes Name, PhoneNumber, and PhoneBook that are employed by the provided phone book program, PhoneBookProgram.cpp.

Class Name
•   Represents the name for a listing in a phone book (example: Phil’s Pizza)
•   Overloads stream insertion (<<) and stream extraction (>>) operators
•   Stream extraction operator validates text entered by user for a name
o   skips any initial white space typed by user
o   throws a runtime exception if input name is invalid
   name must be at least 1 character and a maximum of 25 characters
o   does not modify Name object if input name is invalid

Class PhoneNumber
•   Represents the phone number for a listing in a phone book (example: 919-555-1234)
•   Overloads stream insertion (<<) and stream extraction (>>) operators
•   Stream extraction operator validates text entered by user for a phone number
o   skips any initial white space typed by user
o   throws a runtime exception if input phone number is invalid
   phone number must be 12 characters in length 
   phone number must be in the format xxx-xxx-xxxx
   phone number must only contain digits (0-9) except for two dashes (-)
o   does not modify PhoneNumber object if input phone number is invalid

Class PhoneBook
•   Represents the listings in a phone book
•   Overloads stream insertion (<<) operator
•   Stream insertion operator outputs phone book in tabular format:
Phil's Pizza                  919-555-1234
Sarah's Shoes                 800-111-1111
Blue's Clues PI               363-712-3434
•   add member function adds a listing to the phone book
o   throws a runtime exception if name already added to the phonebook
o   throws a runtime exception if maximum number of listings in the phone book (maximum listings = 1000)
o   prototype:
void add(const Name&, const PhoneNumber&);
•   find member function finds a listing to the phone book
o   returns the phone number for the listing found with name
o   throws a runtime exception if name not found in phonebook
o   prototype:
const PhoneNumber& find(const Name&) const;
#include <iostream>
using namespace std;

#include "PhoneBook.h"

int main()
{
    PhoneBook phoneBook;

    bool quit = false;    // Flag to stop looping
    while (! quit)
    {
        // Output phone book menu
        cout << endl
            << "Phone Book Menu\n"
            << "1: add a listing\n"
            << "2: find a listing\n"
            << "3: print phone book\n"
            << "q: quit\n\n"
            << "Choice: ";

        // Input text typed by user
        string choice;
        cin >> ws;            // skip initial whitespace
        getline(cin, choice); // input user text

        Name name;
        PhoneNumber number;

        if (choice == "1")       // Add a listing
        {
            cout << "Add a Listing\n";
            cout << "Enter listing name: ";
            cin >> name;
            cout << "Enter listing phone number: ";
            cin >> number;
            phoneBook.add(name, number);
        }

        else if (choice == "2")  // Find a listing
        {
            cout << "Find a Listing\n";
            cout << "Enter name to find: ";
            cin >> name;
            cout << phoneBook.find(name) << endl;
        }

        else if (choice == "3")  // Print phone book
        {
            cout << "Print Phone Book\n";
            cout << phoneBook;
        }

        else if (choice == "q")  // Quit
        {
            cout << "Good-bye" << endl;
            quit = true;
        }

        else                     // Invalid user input
        {
            cerr << "Invalid choice: " << choice << endl;
        }
    }
}
This article has been dead for over six months. Start a new discussion instead.