我遇到了这个程序的问题,它是向前和向后打印出一个列表,但是当我向后打印出列表时,列表中的第一个数字是一个随机的大量数字而不是正确的数字。例如
0 1 2 3 4 5 6 7 8 0
4286398 8 7 6 5 4 3 2 1 0
谁能解释一下我的代码有什么问题。
还有谁能告诉我如何将计数器从 printList 函数传递给一个名为 checkList() 的新函数,以便计数器在 checkList() 中的值与 printList() 末尾的值相同。
代码:
void printList(int array1[]){
int counter = 0;
int x;
ifstream theFile("list.txt");
while(theFile >> x){
array1[x] = x;
cout << array1[x] << " ";
counter = counter + 1;
}
cout << endl << counter << endl;;
int n = counter;
for(int i = n -1; i >= 0; i--){
cout << array1[i] << " ";
}