-1

Im having some isues when I try to run my C++ program. What does it do is ask the user for the size of the array and what type of array is, double or int, then depending on the user choise, it declares the array, then it calls a function that fills the array with user values

#include <iostream>
using namespace std;

template<class T>
void fill(T *arr,int size){
        for(int i = 0; i < size; i ++){
                cout<<"Insert value " << i << " :" ;
                cin >> arr[i];
        }
}
int main(){
        int option,size;
        cout << "Size? ";
        cin >> size;
        cout << "1 = double" << endl
        << "2 = int" << endl;
        cin >> option;
        if(option == 1){
                double arr[size];
        }
        else if(option == 2){
                int arr[size];
        }
        fill(arr,size);
return 0;
}

But when I try to run it, I got this error

test.cpp: In function ‘int main()’: test.cpp:24:7: error: ‘arr’ was not declared in this scope fill(arr,size); ^

  • 1
    I think you need to take a step back, and start over with [a couple of good beginners books](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Or at the very least research *scoping*. A good book should also teach you that C++ doesn't have [variable-length arrays](https://en.wikipedia.org/wiki/Variable-length_array), so your code is invalid in more than one way. – Some programmer dude Nov 09 '17 at 22:51
  • 2
    @Victor Oyervides The arrays in this code snippet if(option == 1){ double arr[size]; } else if(option == 2){ int arr[size]; } are declared in the scopes of the if and else statements and are not visible outside these statements. – Vlad from Moscow Nov 09 '17 at 22:55

1 Answers1

2

In main(), your arr variables are only in scope of the if() blocks that declare them. They go out of scope when the if's closing } is reached. So they are indeed not in scope where fill() is being called.

Not that it would matter anyway, because you can't declare a variable with multiple types anyway. So, you would not be able to declare a single arr array variable in fill()'s scope and have the array type be int or double based on user input (well, unless you use std::variant or std::any, but that is a topic of its own).

You will need to call fill() inside of the if() blocks, where the arrays are in scope.

Also, you are relying on a non-standard vendor-specific extension known as "Variable Length Arrays", aka allocating a variable sized array in automatic (stack) memory instead of dynamic (heap) memory. Only some compilers implement that extension as an extra feature. Don't rely on it. The correct way to allocate an array of variable size is to use new[] instead, or better std::vector.

Try something more like this:

#include <iostream>
#include <vector>

template<class T>
void fill(T *arr, int size){
    for(int i = 0; i < size; i ++){
        std::cout << "Insert value " << i << " :";
        std::cin >> arr[i];
    }
}

int main(){
    int option, size;

    std::cout << "Size? ";
    std::cin >> size;
    std::cout << "1 = double" << std::endl
              << "2 = int" << std::endl;
    std::cin >> option;

    if (option == 1) {
        std::vector<double> arr(size);
        fill(&arr[0], size);
    }
    else if (option == 2) {
        std::vector<int> arr(size);
        fill(&arr[0], size);
    }

    return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770