[英]Function returns Array but output is not as expected
我遇到的問題是我正在嘗試從函數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";
}
這個:
for (int Counter2=0; Counter2<=19; ++Counter2)
{
cout << LargeNumber1[SIZE];
}
應該是這樣的:
for (int Counter2=0; Counter2<SIZE; ++Counter2)
{
cout << LargeNumber1[Counter2];
}
您反復打印的數字剛好超出數組末尾。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.