返回信息流输入十个数字后,并不显示,要继续输入十个数字,显示的是后面的十个数字,咋回事呀
#include <iostream>
using namespace std;
const int SIZE = 10;
int fillarray(double arr[], int SIZE);
void showarray(double arr[], int n);
int main()
{
double fox[10];
fillarray(fox, SIZE);
int size = fillarray(fox, SIZE);
showarray(fox, size);
cout << "Done!" << endl;
return 0;
}
int fillarray(double arr[],int SIZE)
{
int n;
double temp;
int i;
for ( i = 0; i < SIZE; i++)
{
cout << "Enter number #" << i + 1<<" ";
cin >> temp;
if (!cin)//输入错误的处理:非double值
{
cin.clear();
while (cin.get() != '\n')
continue;
cout << "Worong input" << endl;
break;//结束循环
}
else if (temp < 0)
break;
arr[i] = temp;
}
return i;
}
void showarray(double arr[], int n)
{
cout << "The array is:";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
}
这是一条镜像帖。来源:北邮人论坛 / cpp / #87367同步于 2015/6/3
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖
数组填充,不知道问题出在哪,求教
herbice
2015/6/3镜像同步4 回复
订阅后,新回复会通过你的通知中心匿名送达。
4 条回复
呀,让大家见笑了,这么低级的错误,多谢啦
【 在 nuanyangyang 的大作中提到: 】
: fillarray(fox, SIZE);
: int size = fillarray(fox, SIZE);
: 你往同一个数组里读了两次……
:
发自「贵邮」