Using local variable outside its scope

How do I use arrj[] outside the while loop as the array is a local
variable to while loop?

You have the answer in your question itself. You can not have the arrj out side the first for loop as it will be deleted from the stack as it go out of from this scope.

In order to use the arrj[], you need it to be declared before the while loop:

   int t = 2, size = 10;
   int arrj[size];   // declare before

   while(t!=0)  // this should be t--, otherwise your while loop will not end
      /* code */

However, as it look like to have array of integers as per user choise, I recommend you to use std::vector<>, by which you can achieve what you want to have.

   int t = 1, size;
   std::vector<int> arrj; // declare here

      for(int j=1;j <=3;j++)
         std::cin>> size;
         // resize the vector size as per user choise
         // now you can fill the vector using a range based for loop
         for(int& elements: arrj)  std::cin >> elements;

         // or simply
            int elements; std::cin >> elements;

Browse More Popular Posts

Leave a Comment