Hi I'm trying to write a program that reads an array of numbers until it gets an empty line or the array is full using a function, then displays the numbers entered.
However I keep getting error: declaration of int inarray[5] shadows a parameter
Here is the code:
#include <iostream>
#include <climits>
using namespace std;
#define MAX 5 // The maximum number of numbers we can input.
bool GetNum(int &inarray);
int main()
{
int i;
int inarray[MAX]; // The input array
int n;
while (GetNum(n))
{
cout << "Here are the numbers entered: " << endl;
for (i=0;i<MAX;i++)
{ // print out inarray
cout << n << " ""\n";
}
}
return 0;
}
bool GetNum(int &inarray)
{
int i;
int inarray[MAX];
bool gotNum = false;
cout<< "enter a number: ";
if (cin.peek() != '\n')
{
cin >> inarray[i];
gotNum = true;
}
cin.ignore(INT_MAX, '\n');
return gotNum;
}
My guess is that I'm not declaring the array correctly or that I'm not allow to pass an array to a function?
Hi Andre
Hmmm, there are actually quite a few things wrong here.
Firstly, to pass an array (i.e. a pointer) to a function, your function header should look like this:
bool GetNum( int* inarray )
Your code above takes a reference to an int as its parameter - very different.
The error message you're getting is because you're declaring a variable called inarray twice in the same function: once as a function parameter and then again in the function body. I suspect you don't want that second declaration at all.
Well, that'll get rid of some error messages and introduce some new ones but you might be able to figure them out yourself ;)
Regards, keineahnung
#include <iostream>
constint ARRAY_SIZE = 5;
// Passing a reference to an array...
void Fill_Array(int (&arr)[ARRAY_SIZE])
{
for(int i = 0; i < ARRAY_SIZE; ++i)
{
arr[i] = i;
}
}
// passing a pointer, both of the following are the same, the first one gives more info
// about what it is expecting
void Mod_Array(int arr[], int size)
{
for(int i =0; i < size; ++i)
{
arr[i] = size - i;
}
}
void Print_Array(int *arr, int size)
{
for(int i =0; i < size; ++i)
{
std::cout << arr[i] << " ";
}
std::cout << std::endl;
}
int main()
{
int anArray[ARRAY_SIZE];
Fill_Array(anArray);
Print_Array(anArray, ARRAY_SIZE);
Mod_Array(anArray, ARRAY_SIZE);
Print_Array(anArray, ARRAY_SIZE);
return 0;
}
The program should be able to take numbers until it finds an empty line. The GetNum function should have a reference to an array as parameter (GetNum (&inarray)) so changing the function definition to bool GetNum(int (&inarray)[MAX]) solved the error but now it terminates when it finds an empty line and never gets to call the ShowNum fuction.
You haven't initialized this variable so it holds a random value.
Then a few lines later you've got:
cin >> inarray[i];
So you're using i as an index into your array, inarray.
That array has a size of MAX, or 3. So to access the values in that array (for reading or writing) you can use an index of 0, 1 or 2. Anything else will be invalid, because it's outside of the memory you've set aside for your array.
And that's why your program's crashing (- on my system it gets a Segmentation Fault - a memory error).
Try adding this line after line 36: std::cout << "i=" << i << "\n";
I'll bet you it's not between 0 and 2!
You need to keep track of where you're storing these numbers in your array - a sort of counter that starts at 0 (the first element in the array) and pulls you out of the loop when you go past 2. A simple int's all you need, but you've got to set it to 0 before you start and then increment it each time you get a new number (++i).
Hope that helps ;)