# Error : use of undeclared identifier

**URL:** <https://root-forum.cern.ch/t/error-use-of-undeclared-identifier/47791>\
**Category:** Newbie\
**Created:** [November 23, 2021, 1:20pm UTC](https://root-forum.cern.ch/t/error-use-of-undeclared-identifier/47791 "2021-11-23T13:20:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mzriu](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/e56c9b/32.png) [@mzriu](https://root-forum.cern.ch/u/mzriu)\
**Post date:** [November 23, 2021, 1:20pm UTC](https://root-forum.cern.ch/t/error-use-of-undeclared-identifier/47791/1 "2021-11-23T13:20:17Z")

</div>

ROOT Version : 6.24/06

Hello guys,

I am having a problem which is pretty weird. Here is my code :

```auto
Int_t ind_run = 0; 
    if(ind_run==0){
    	TString path = "/home/marius/Documents/Thèse/E819S/Copie/Histogrammes/single/8He-12micrA-sansRED_0001_single_out.root";
    	} else if(ind_run == 1){
    	TString path = "/home/marius/Documents/Thèse/E819S/Copie/Histogrammes/single/8He-10uA-sansRED-RUN1_0001_single_out.root";
    	} else if(ind_run == 2){
    	TString path = "/home/marius/Documents/Thèse/E819S/Copie/Histogrammes/single/8He-10uA-sansRED-RUN2_0001_single_out.root";
    	} else if(ind_run == 3){
    	TString path = "/home/marius/Documents/Thèse/E819S/Copie/Histogrammes/single/8He-Nobeamchopper-RUN139_0001_single_out.root";
    	} else if(ind_run == 4){
    	TString path = "/home/marius/Documents/Thèse/E819S/Copie/Histogrammes/single/8He-Nobeamchopper-RUN140_0001_single_out.root";
    	} else if(ind_run == 5){
    	TString path = "/home/marius/Documents/Thèse/E819S/Copie/Histogrammes/single/8He-beamchopper-RUN144_0001_single_out.root";
    	}
    	
    	
    	
    	TFile *f = new TFile(path);

```

My problem is whatever value I set for my ind\_run variable, I get the error message "use of undeclared identifier ‘path’ ". It seems like my code won’t go inside the if or else if statements and so my TString path is not declared. But why it won’t do that is a mystery to me…

Hope you guys have a solution !

Best,  
Marius

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [November 23, 2021, 1:27pm UTC](https://root-forum.cern.ch/t/error-use-of-undeclared-identifier/47791/2 "2021-11-23T13:27:54Z")

</div>

You declare your `TString path` inside the `if` or `else if` statements. It only survives until the next closing curly bracket. You should change it to:

```c++
TString path = "";
Int_t ind_run = 0; 
if(ind_run==0)
	path = "/home/marius/Documents/Thèse/E819S/Copie/Histogrammes/single/8He-12micrA-sansRED_0001_single_out.root";
else if(ind_run == 1)
  	path = "/home/marius/Documents/Thèse/E819S/Copie/Histogrammes/single/8He-10uA-sansRED-RUN1_0001_single_out.root";
else if(ind_run == 2)
   	path = "/home/marius/Documents/Thèse/E819S/Copie/Histogrammes/single/8He-10uA-sansRED-RUN2_0001_single_out.root";
else if(ind_run == 3)
   	path = "/home/marius/Documents/Thèse/E819S/Copie/Histogrammes/single/8He-Nobeamchopper-RUN139_0001_single_out.root";
else if(ind_run == 4)
   	path = "/home/marius/Documents/Thèse/E819S/Copie/Histogrammes/single/8He-Nobeamchopper-RUN140_0001_single_out.root";
else if(ind_run == 5)
   	path = "/home/marius/Documents/Thèse/E819S/Copie/Histogrammes/single/8He-beamchopper-RUN144_0001_single_out.root";
    	
    	
    	
    	
    TFile *f = new TFile(path);

```

---

<div class="post-metadata">

**Author:** ![mzriu](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/e56c9b/32.png) [@mzriu](https://root-forum.cern.ch/u/mzriu)\
**Post date:** [November 23, 2021, 1:31pm UTC](https://root-forum.cern.ch/t/error-use-of-undeclared-identifier/47791/3 "2021-11-23T13:31:30Z")

</div>

Hi thanks for your response !

It works nicely now, but I don’t really understand why the variable only survives inside the if statement. Do you have an explanation for that ?

Best,  
Marius

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [November 23, 2021, 1:43pm UTC](https://root-forum.cern.ch/t/error-use-of-undeclared-identifier/47791/4 "2021-11-23T13:43:36Z")

</div>

> [@mzriu](#):
>
> Do you have an explanation for that ?

Check this out: [Scope of Variables in C++ - GeeksforGeeks](https://www.geeksforgeeks.org/scope-of-variables-in-c/)

> - Variables defined within a function or block are said to be local to those functions.  
> …
> - Anything between ‘{‘ and ‘}’ is said to inside a block.
> - Local variables do not exist outside the block in which they are declared, i.e. they **can not** be accessed or used outside that block.
