C++PrimerPlus(第6版)中文版:Chapter7.3_arrfun3

这是一个C++程序,它接受用户输入的数组元素,显示数组内容,然后根据用户输入的因子重新估值数组元素并再次显示。程序包含了错误处理机制,如遇到无效输入会提示用户重新输入。
// Chapter7.3_arrfun3.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
const int Max = 5;
//function prototypes
int fill_array(double ar[],int limit);
void show_array(const double ar[],int n);
void revalue(double r, double ar[],int n);


int main()
{
    using namespace std;
    double properties[Max];
    int size = fill_array(properties,Max);
    show_array(properties,size);
    if(size>0)
    {
        cout<<"Enter revaluation factor:";
            double factor;
            while (!(cin >> factor))
            {
                cin.clear();
                    while (cin.get()!='\n')
                        continue;
                    cout<<"Bad input:Please enter a number:";                                      
            }
            revalue(factor,properties,size);
            show_array(properties,size);

    }
    cout << "Done.\n";
    cin.get();
    cin.get();
    return 0;
    
}
int fill_array(double ar[], int limit)
{
    using namespace std;
    double temp;
    int i;
    for (i=0;i<limit;i++)
    {
        cout << "Enter value#" << (i + 1) << ":";
        cin >> temp;
        if (!cin)//bad input
        {
            cin.clear();
            while (cin.get() != '\n')
                continue;
            cout << "Bad input;input process terminated.\n";
            break;
        }
        else if (temp < 0)//signal to terminate
            break;
        ar[i] = temp;
    }
    return i;


}
//the following function can use ,but not alter,
//the array whose address is  ar 
void show_array(const double ar[],int n)
{
    using namespace std;
    for (int i=0;i<n;i++)
    {
        cout << "Property #" << (i + 1) << ":$";
        cout << ar[i] << endl;
        
    }

}
//multiplies each element of ar[] by r
void revalue(double r,double ar[],int n)
{
    for (int i = 0; i < n; i++)
        ar[i] *= r;
}


// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单

// 入门使用技巧: 
//   1. 使用解决方案资源管理器窗口添加/管理文件
//   2. 使用团队资源管理器窗口连接到源代码管理
//   3. 使用输出窗口查看生成输出和其他消息
//   4. 使用错误列表窗口查看错误
//   5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
//   6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值