# If Else problems for running code

**URL:** <https://root-forum.cern.ch/t/if-else-problems-for-running-code/34719>\
**Category:** Newbie\
**Created:** [June 20, 2019, 5:17am UTC](https://root-forum.cern.ch/t/if-else-problems-for-running-code/34719 "2019-06-20T05:17:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![JorgeM](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/58956e/32.png) [@JorgeM](https://root-forum.cern.ch/u/JorgeM)\
**Post date:** [June 20, 2019, 5:17am UTC](https://root-forum.cern.ch/t/if-else-problems-for-running-code/34719/1 "2019-06-20T05:17:23Z")

</div>

Hello fellow ROOTers,  
I am trying to run some if else statements for my code, but It doesn’t seem to work.

```auto
  //----Filling the Histograms----
  std::string answer;
  cout << "Open default file? (y or n) ";
  cin >> answer;
  if (answer == y) {
    inp.open("Am241pos8.TKA");
  }
  if (answer == n) { 
    std::string filename;
    cout << "Enter file name: ";
    cin >> filename;
    ifstream inp; double x;
    inp.open(filename);
  }
  else {
    cout << "Please type yes(y) or no(n)"<< endl;
  }

```

What I’m trying to do here is to give out some prompts before running the rest of my code. For example, if the person types y I want root to run the default file and if they type n then the other code should be run instead. However, I am struggling with it and the shell returns this

In file included from input\_line\_13:1:  
/home/jorge/alpha/alphacounter.C:67:17: error: use of undeclared identifier ‘y’  
if (answer == y) {  
^  
/home/jorge/alpha/alphacounter.C:68:5: error: use of undeclared identifier ‘inp’  
inp.open(“Am241pos8.TKA”);  
^  
Is there anything else I can do about this so that it runs like I want?

J

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [June 20, 2019, 6:24am UTC](https://root-forum.cern.ch/t/if-else-problems-for-running-code/34719/2 "2019-06-20T06:24:13Z")

</div>

```auto
  std::ifstream inp; double x;
  std::string answer;
  std::cout << "Open default file? (y or n) ";
  std::cin >> answer;
  if (answer == "y") {
    inp.open("Am241pos8.TKA");
  } else if (answer == "n") { 
    std::string filename;
    std::cout << "Enter file name: ";
    std::cin >> filename;
    inp.open(filename);
  } else {
    std::cout << "Please type yes(y) or no(n)" << std::endl;
  }

```
