hi everyone,

I am trying to initialize array in main method, and insert value in array in another method named "popArr()", in last print array in "printArr()" method. But its not working and i am getting errors. The environment i am using borland c++. The code is as follows.

#include <iostream.h>
#include <conio.h>

void popArr();
void printArr();

void main()
{

    int arr[10];

   //methods
   popArr();
   printArr();

getch();
}

void popArr()
{
    //get values and store in array
   cout << "Enter numbers in array :" << endl;
    for(int i=0; i<10; i++)
   {
    cin >> arr[i];
   }
}

void printArr()
{
    //print array values
   cout << "Array : " << endl;
    for(int i=0; i<10; i++)
   {
    cout << arr[i] << " ";
   }
}

Anyone please help. I will highly appreciate. Thnx.

Before we get to the specific issue, I'd like to mention a few things. First, it is incorrect to use void as the return type of the main() function; according to the C++ language standard, you should always use int main(). Some compilers do allow void main(), but strictly speaking it is incorrect.

Second, you are using the older form of the header files. In modern C++ (since the 1998 standard), the correct standard library headers no longer use the '.h' extension; thus, it should be <iostream> rather than <iostream.h>. On a related note, the C++98 standard also requires that all standard library classes, tyoes, and objects be placed in the std namespace. Thus, you would need to scope the references to std::cout and std::cin.

(BTW, what version of the compiler are you using? If it is the Turbo C++ compiler, you should be aware that that is over twenty years old, and you ought to replace it with something newer. There are several good free compilers available that support the latest version of the standard, so there's no reason to use the older compiler if you have any choice in the matter.)

Finally, you should be aware that the <conio.h> library is proprietary to the Borland compilers, and is specific to DOS programs (which is not the same as Windows console, though the console is backwards-compatible with DOS to a degree). It is both non-portable and outdated, and you are best off avoiding it.

Now we get to the actual problem at hand (about time!). The issue here is that arr[] is declared as a local variable of main(), which means that it is not visibile form other functions unless you pass it as a parameter to them. to this effect, youwould want to change the function signature of popArray() and printArray() as follows:

void popArr(int arr[]);
void printArr(int arr[]);

And change the calls to the functions as so:

    //methods
    popArr(arr);
    printArr(arr);

Is this clear now?

Actually I could not find any object oriented code in your post, so no methods also.

Here's your code a little bit revise, and with some new implementations.
1st of all, change Borland, because it's old.
Ok, so here's the code:

#include <iostream>
using namespace std;
#define MAX 100

struct arr{
    int size;
    int ar[MAX];
};

void popArr(arr &ar);
void printArr(arr ar);

int main(){
    arr arr;
    popArr(arr);
    printArr(arr);
    return (0);
}

void popArr(arr &ar){
    cout<<"Array size: ";
    cin>>ar.size;
    cout << "Enter numbers in array.\n";
    for(int i=0; i<ar.size; i++){
        cout<<"> ";
        cin >> ar.ar[i];
    }
}

void printArr(arr ar){
    cout << "Array : " << endl;
    for(int i=0; i<ar.size; i++)
        cout << ar.ar[i] << " ";
}

Let's analyse it a bit.
We have a struct:

struct arr{
    int size;
    int ar[MAX];
};

which will be our array. Inside it we have two things, an int size, and an array of ints of size MAX, which I defined it having a value of 100: #define MAX 100.
Ok, but you will ask me, why do I need that struct? Well it's not mandatory, but I want to give an example of how you can do things.
Ok, so in my main function, which is set to int main() and return(0)', not to void main(), I initialize the array, and than call the functions.
At my first call I put the & operator, meaning the reference operator, that means that it will pass the variable by reference, and will not make a copy of that. Passing by reference means that every change I make on that variable will remain outside the funciton as well.
So, we have the popArr function which fist asks for a size, which will be used to set a limit of how many numbers can be inserted into our array. Notice that if you put a value greater than 100 it will crash when adding things into the array, at the index>100, because it will try do add things in a memory segment which wasn't allocated for that.
So, by passing the struct by reference all changes that we'll do on it will remain after exiting the function.
But, when calling the print function, well, we send only a copy of the variable, because the print function will only print stuff, and will not be required to make changes on the struct.
So there you go, your program revised.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.