1

so i have made this:

#include <iostream>
#include <string>
using namespace std;

int main()
{
  while(true)
  {
     string x;
     string y;
     string an1[3] = {"quit","exit","leave"};
     double ver = 0.1;
     cout << "basic0.1% ";
     cin >> x;
    if (x == "upgrade")
     {
      system("yay -Syyu");
     }
     else if (x == "install")
     {
      cout << "What do you want to install?" << "\n";
      cin >> y;
      system(("sudo yay -S " + y).c_str());
     }
     else if (x == "version")
     {
       cout << "version: " << ver << "\n"; 
     }
     else if (x == "clear")
     {
       system("clear");
     }

     else if(x == "quit" || "leave" || "exit")
     {
       break;
     }
     else if(x == "ls")
     {

     }
  }
  return 0;
}

and in the else if(x == "ls"){} i wanted to show the output of the command,i tried putting system("ls"); but it just exits the program. btw this should be a little terminal emulator for linux but i am not sure yet,i am just having some fun.

  • Using `system()` is a very inelegant way of doing this. Look at `popen()` as an alternative, this will allow you to create a child that executes the command and provide a two communication stream to communicate with the child processes. – Martin York Oct 29 '19 at 16:03

1 Answers1

5

system("ls"). works fine. Your problem is this:

    else if(x == "quit" || "leave" || "exit")
    {
      break;
    }

Let's say you type ls. Then x contains the string "ls". x == "quit" is false because you didn't type quit. "leave" is true because it's a non-null pointer. "exit" is true because it's a non-null pointer. false || true || true is true, so the program quits.

user253751
  • 57,427
  • 7
  • 48
  • 90
  • FWIW, I recommend [`std::filesystem::directory_iterator`](https://en.cppreference.com/w/cpp/filesystem/directory_iterator) instead of `system()` - faster, more focused, probably safer too. – Nick Reed Oct 29 '19 at 15:18