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;
}
nyfan68
0
Newbie Poster
Recommended Answers
Jump to Posthow 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 …
Jump to PostAre you sure that you want to inherit from
runtime_error
(that is, that aName
is a specific example of aruntime_error
), or justthrow
such an error?
All 5 Replies
Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster
Schol-R-LEA
1,446
Commie Mutant Traitor
Featured Poster
nyfan68
0
Newbie Poster
nyfan68
0
Newbie Poster
nyfan68
0
Newbie Poster
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.