// 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 文件
C++PrimerPlus(第6版)中文版:Chapter7.3_arrfun3
最新推荐文章于 2025-12-19 16:34:09 发布
这是一个C++程序,它接受用户输入的数组元素,显示数组内容,然后根据用户输入的因子重新估值数组元素并再次显示。程序包含了错误处理机制,如遇到无效输入会提示用户重新输入。
1965

被折叠的 条评论
为什么被折叠?



