为啥这个循环(PhoneNumber.cpp里)不能运行?请指点。谢谢。
这是一个输入电话号码的程序,运行的时候不能跳出循环和输出结果,请指点。// Workshop 7 - Derived Classes
// w7x.h
const int MAX_NUMBERS = 20;
// Workshop 7 - Derived Classes
// w7x.cpp
#include <iostream>
using namespace std;
#include "w7x.h"
#include "PhoneNumber.h"
int main( ) {
int i, n;
bool keepreading = true;
PhoneNumber no, number[MAX_NUMBERS];
cout << "Telephone List" << endl;
cout << "==============" << endl;
n = 0;
do {
cin >> no;
if (no.valid())
number[n++] = no;
else
keepreading = false;
} while (keepreading && n < MAX_NUMBERS);
cout << endl;
for (i = 0; i < n; i++) {
number[i].display();
cout << endl;
}
}
//Header File--PhoneNumber.h
#include<iostream>
using namespace std;
class PhoneNumber
{
protected:
int area;
int local;
public:
PhoneNumber();
PhoneNumber(int a, int n);
void display() const ;
bool valid() const;
//~PhoneNumber();
friend istream& operator>>(istream& is, PhoneNumber& number);
};
#include <iostream>
#include "PhoneNumber.h"
using namespace std;
//non-argument constructor
PhoneNumber::PhoneNumber()
{
area=0;
local=0;
}
//two-argument constructor
PhoneNumber::PhoneNumber(int a, int n)
{
if ((a>=100 && a<=999) && (n>=1000000 && n<=9999999))
{
area=a;
local=n;
}
}
//a query that displays the telephone number in AAA-LLL-LLLL format
void PhoneNumber::display() const
{
cout<<area<<"-"<<local/10000<<"-"<<local%10000<<endl;
//5551212 / 10000=555,
//5551212 % 10000=1212
}
//a query that returns true if the number is valid, false otherwise
bool PhoneNumber::valid() const
{
return (area>=100 && area<=999) && (local>=1000000 && local<=9999999);
}
//Extraction overload that takes in a reference to an istream object as the left operand
//and a reference to a PhoneNumber as a right operand
istream& operator>>(istream& is, PhoneNumber& number)
{
int a, b;
bool keepgoing=true;
do
{
cout<<"Area Code :"<<endl;
is>>a;
if (a==0)
keepgoing=false;
else
{
cout<<"Local Number :"<<endl;
is>>b;
number.area=a;
number.local=b;
}
}while(keepgoing);
return is;
}