| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 675 人关注过本帖
标题:为啥这个循环(PhoneNumber.cpp里)不能运行?请指点。谢谢。
只看楼主 加入收藏
winglesswu
Rank: 1
等 级:新手上路
帖 子:92
专家分:0
注 册:2013-1-28
结帖率:71.88%
收藏
已结贴  问题点数:5 回复次数:2 
为啥这个循环(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;
}

搜索更多相关主题的帖子: include number 电话 
2013-07-27 20:50
love云彩
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:青藏高原
等 级:贵宾
威 望:53
帖 子:3663
专家分:11416
注 册:2012-11-17
收藏
得分:3 
用vs调试一下

思考赐予新生,时间在于定义
2013-07-28 02:12
额外覆盖
Rank: 14Rank: 14Rank: 14Rank: 14
等 级:城市猎人
威 望:6
帖 子:1726
专家分:5757
注 册:2012-9-22
收藏
得分:3 
输入运算符的重载那里?不应该啊  自己看看有没有逻辑错误

我现在所学的一切都是为了游戏!!!为了游戏,加油!加油!努力!
2013-07-28 17:10
快速回复:为啥这个循环(PhoneNumber.cpp里)不能运行?请指点。谢谢。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.028962 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved