我遇到的问题是,我试图从函数EnterNumber()返回一个数组,并将其显示在主目录中,但结果非常疯狂。我使用了调试器,调试器中的数字是正确的,只是一旦它打印到屏幕上就不正确了。


我意识到我的项目中有一个全球性的项目,但它得到了我的教授的认可,他希望我们这次只为这个项目做这件事。
只是想弄清楚为什么它的印刷不正确。谢谢。
#include <iostream>
using namespace std;
void EnterNumber(int Number[]);
const int SIZE=20;
int main()
{
int LargeNumber1[SIZE];
int LargeNumber2[SIZE];
for (int Counter1=0; Counter1<=19; ++Counter1)//zeros arrays out
{
LargeNumber1[Counter1]=0;
LargeNumber2[Counter1]=0;
}
EnterNumber(LargeNumber1);
for (int Counter2=0; Counter2<=19; ++Counter2)//display array 1 contents
{
cout << LargeNumber1[SIZE];
}
cout << "\n\n";
EnterNumber(LargeNumber2);
for (int Counter2=0; Counter2<=19; ++Counter2)//display array 2 contents
{
cout << LargeNumber2[SIZE];
}
}
void EnterNumber(int Number[])
{
int TemporaryArray[SIZE];
int PlaceCounter;
char Storage;
PlaceCounter=0;
for (int Counter1=0; Counter1<=19; ++Counter1)//zeros arrays out
{
TemporaryArray[Counter1]=0;
Number[Counter1]=0;
}
cout << "Please enter a large number --> ";
cin.get(Storage);
while (Storage!='\n' && PlaceCounter<SIZE)//puts number in temp array - left aligned
{
TemporaryArray[PlaceCounter]=(Storage-'0');
++PlaceCounter;
cin.get(Storage);
}
--PlaceCounter;//decrement one to get it to work properly with element style counting, else, extra zero at end
for (int A=SIZE-1; PlaceCounter>=0; A--, PlaceCounter--)//transfers old array into new array, right aligned
{
Number[A]=TemporaryArray[PlaceCounter];
}
cout << "\n";
}发布于 2015-11-13 06:47:55
这是:
for (int Counter2=0; Counter2<=19; ++Counter2)
{
cout << LargeNumber1[SIZE];
}应该是这样:
for (int Counter2=0; Counter2<SIZE; ++Counter2)
{
cout << LargeNumber1[Counter2];
}您正在反复打印一个刚好超出数组末尾的数字。
https://stackoverflow.com/questions/33687219
复制相似问题