Why is an "error: redefinition of [variable]" occurring every time I run code more than once?


Please read tips for efficient and successful posting and posting code

ROOT Version: 6.18
Platform: Mac OS X
Text Editor: Visual Studio Code


{
    #include <iostream>
    using namespace std;
    int x2, y;
    int sum;
    cout << "Type a number: ";
    cin >> x2;
    cout << "Type another number: ";
    cin >> y;
    sum = x2 + y;
    cout << "Sum is: " << sum;

}

This is all I have in my visual studio file named f1.C (no main method). When I go to the Terminal to run .x f1.C, it runs fine, but when I run it again, it shows an error saying “error: redefinition of ‘x2’”. I’m clearly doing something wrong, but I’m not sure what. Can someone help?

It might help to use a function instead of a code block surrounded by { }. It might…