C++ "condition in condition"
hi, again need some help with c++, i´m programming virtual linux console for my school, and i have something like this:
if ( command == "ls" )
{
cout << "reboot\n";
cout << "shutdown\n";
cout << "\n";
goto commands;
}
else
{
cout << "Unrecognized command\n";
goto commands;
}
so its simple when user type into our virtual console command "ls" he get this output, but i need to be more realistic, and i want.. when user types this: rm shutdown, i want when he types ls again, shutdown wont appear in the list. i tryed something like "condition in condition" but this doesn´t work… i think i must use some logical operators but how? and there is another problem, is in c++ any way to use wildcards? i wanna this: when user types cd he gets : no file to open and when he writes cd <existingfile> he goes to this file and when he type cd <badfile> he get file not found? how can i do this? thanks in advance for help and please sorry for my bad english :(
I'm afraid the best way to do this is continue reading your books and learn a few things about objects before you try again. Or else you could always do it manually like this (ugly):
bool shutdown = true; bool reboot = true;
if( command = ls ){ if(shutdown) cout << "shutdown\n"; if(restart) cout << "restart\n"; cout << "\n"; }
But again.. I recommend you fint another solution :)
white space: White space in C++ includes the blank (space character), horizontal tab, end-of-line, vertical tab, form feed, and comments. White space is ignored by the compiler (except when required to separate tokens or when used in a character or string constant), and therefore can be used freely by the programmer to make the program easy for a human to read. Some implementations of C++ treat nonstandard source characters as either white space or line breaks.
i have this script in directory sbin, its not fully completed yet
{
string command;
system("cls");
commands:
cout << "/sbin>";
cin >> command;
if ( command == "help" )
{
cout << "FC6 bash help\n";
cout << "======================\n";
cout << "\n";
cout << "cd ------- Change Directory - Navigate around the file system\n";
cout << "cat ------ Open a file to view its contents\n";
cout << "help ----- Show help\n";
cout << "ls ------- List files in a directory\n";
cout << "cls ------ Clear Screen\n";
cout << "rm ------- Remove File\n";
cout << "su -------- Login you as root
cout << "shutdown - Shutdown the system
cout << "\n";
goto commands;
}
if ( command == "ls" )
{
bool shutdown = true;
cout << "netcat\n";
cout << "nmap\n";
cout << "ifconfig\n";
cout << "ping\n";
cout << "reboot\n";
if (shutdown)
cout << "shutdown\n";
cout << "syscntrl0\n";
cout << "\n";
goto commands;
}
if ( command == "cls" )
{
system("cls");
goto commands;
}
if ( command == "rm shutdown" )
{
bool shutdown = false;
cout << "File shutdown was successfully removed!\n";
cout << "\n";
goto commands;
else
{
cout << "ERROR: Command not recognized\n";
cout << "\n";
goto commands;
}```
when i have if ( command == "rm shutdown" ) i get ERROR: Command not recognized i dont know why.. what should i have to do? and i was tryed this without spaces "rmshutdown" and i get "File shutdown was successfully removws!" but when i typed again command "ls" the shutdown appears again i dont know what i´m doing wrong :(
i am sending you a full source code can you pls compile it and check it?
#include <string>
#include <windows.h>
using namespace std;
void sbin();
int main()
{
bool shutdown = true;
cout << "Welcome in dancuc bash simulator\n";
cout << "\n";
cout << "This simulator is used as an example, note you\n";
cout << "starting without root privilegies!!";
cout << "\n";
system("pause");
system("cls");
sbin();
return 0;
}
void sbin()
{
string command;
system("cls");
commands:
cout << "/sbin>";
cin >> command;
if ( command == "help" )
{
cout << "Function list and uses\n";
cout << "======================\n";
cout << "\n";
cout << "cd ------- Change Directory\n";
cout << "cat ------ Open a file in mode r\n";
cout << "help ----- Show help\n";
cout << "ls ------- List files\n";
cout << "cls ------ Clear Screen\n";
cout << "rm ------- Remove File\n";
cout << "su ------- Give to current user a root privilegies\n";
cout << "shutdown-- Shutdown the system\n";
cout << "reboot---- Reboots the system\n";
cout << "\n";
goto commands;
}
if ( command == "ls" )
{
cout << "netcat\n";
cout << "nmap\n";
cout << "ping\n";
cout << "reboot\n";
cout << "tracert\n";
if(shutdown)
cout << "shutdown\n";
cout << "kernel\n";
cout << "root\n";
cout << "\n";
goto commands;
}
if ( command == "cls" )
{
system("cls");
goto commands;
}
if ( command == "rmshut" ) //here is problem, and also have
{ //problems with that bools
bool shutdown = false;
cout << "Shutdown was successfully deleted\n";
cout << "\n";
goto commands;
}
else
{
cout << "ERROR: Unrecognized command\n";
cout << "\n";
goto commands;
}
}```
thanks in advance
You seriously need to re-read whatever C++ tutorial you found or find a new one. You are trying to use the value of shutdown
as a condition, but you fail to declare within the same scope as your sbin()
function, and since you include the windows.h header file it thinks you are trying to use the socket function of shutdown()
which will always return true. As for your whitespace problem, that's why getline()
exists.
e.g.
string myCommand;
getline(cin, myCommand);
if(myCommand == "roxorz my soxorz") cout << "shit I'm leet!" << endl;
My suggestion would be to learn how to use basic loops so you can fix your incorrect use of goto
. ;)
~T
ok thnx so i am going to learn something new about getline() and loops, but can i ask on last question? is in c++ any way to use wildcards? because i have that condition in my souce, you can see it, if you type correct command, is everything ok, but when you type something incorrect, you get "ERROR: Command not found!" i want when user type something like this: "rm <incorrectfile>" that he get "File not found" and not that "ERROR: Command not recognized" is there any way how to do this ? thnx again to all for their help ;)
I'm not sure if it's exactly the same in c++, but at least in Java it would be: String str = "Orangejuice"; if (str.substring(0,5).equals("orange")) { do_stuff() } //if the first six characters is "orange".. //I'd guess you'd have to replace .equals() with == or something.. //, I dont know strings and C++ very well