If Else problems for running code

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

  //----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

  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;
  }
1 Like