【发布时间】:2013-06-11 16:30:48
【问题描述】:
这是一段简单的代码。实际上它是一个“填充数组”功能代码。
#include <iostream>
using namespace std;
int main(){
int size = 10; a[10]; numberUsed;
cout << "Enter up to " << size << " nonnegative whole numbers.\n"
<< "Mark the end of the list with a negative number.\n";
int next, index = 0;
cin >> next;
while ((next >= 0) && (index < size)){
a[index] = next;
index++;
cin >> next;
}
numberUsed = index;
for(int i = 0 ; i < numberUsed -1 ; i++){
cout << a[i] << endl;
}
}
当用户输入整数时它工作正常。但是当我输入双精度值时,它应该转换该特定值。并为下一个输入的整数重复该值。 所以现在输入 1 2 3 4 5 6.5 7 8 9 -1 。我得到以下输出 1 2 3 4 5 6 6 6 6 6 任何帮助将不胜感激。
【问题讨论】:
-
您的程序无法编译。另外,为什么需要
numberUsed? -
我的猜测:它无法解析“。”因为整数中没有“.”,所以它卡在流管道中。所以 next 没有被修改(保持在 6)并且被一遍又一遍地存储
-
如果您将第 4 行中的分号替换为逗号,则程序将编译。发帖人可能正在使用接受该语法的非标准编译器。
-
当我用给定的输入输入给定的代码时,我得到 1 2 3 4 5 6 0 0 0 0。我可以确认 @AK4749 是正确的,因为 cin 有一个错误标志。奇怪的是我的输出不一样。
-
对分号表示歉意...用逗号代替这些分号..