0
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <sstream>
#include <bits/stdc++.h>

using namespace std;

int main(){
    while (true)
    {
        cout << "$ ";
        char ui[999];
        cin.getline(ui, sizeof(ui));
        if(ui == "clear")
        {
            system("cls");
        }
        else
        {
            system(ui);
        }
    }
}

It will run the else even if ui equals clear. Is it because its a char array and not a string?. Or is it because of system()?

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
Geremachek2
  • 121
  • 1
  • 6
  • 3
    I think you want `strcmp` not `==` – cleblanc Jun 15 '18 at 18:25
  • 7
    Classical mistate. You're comparing addresses, not actual strings. Since it's C++, use *proper* strings: `std::string ui;` and your problems will go away. If you insist on C-strings, use `std::strcmp()`. – HolyBlackCat Jun 15 '18 at 18:26
  • strcmp only seems to work if I type "clear" not clear – Geremachek2 Jun 15 '18 at 18:54
  • post the new code. – cleblanc Jun 15 '18 at 18:57
  • never mind, I just had to change cin.getline to getline(cin, ui) for string ui; – Geremachek2 Jun 15 '18 at 19:01
  • Unrelated. Your use of `#includes` suggests that you don't know what `#include ` is for. I recommend reading [How does #include work in C++?](https://stackoverflow.com/questions/25311011/how-does-include-bits-stdc-h-work-in-c) and then reading [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – user4581301 Jun 15 '18 at 19:01

0 Answers0