Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

C++ "condition in condition"


ghost's Avatar
0 0

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 :(


ghost's Avatar
0 0

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 :)


ghost's Avatar
0 0

thx for your help ;)


ghost's Avatar
0 0

have an array of the files in the current directory, and remove whatever files from it when they type in "rm …"


ghost's Avatar
0 0

ok but how to tell c++ this "rm shutdown" ? i am using cin >> command; but it not accepting spaces so i must write something like this : rmshutdown, how can i write this commands with spaces?


spyware's Avatar
Banned
0 0

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.


ghost's Avatar
0 0

so how can i use spaces if i cant use a white space ?:o


ghost's Avatar
0 0

Can't you just do something like this:

string command;
cin &gt;&gt; command;
if(command == &quot;rm shutdown&quot;)
{
do_whatever();
}

Edit: Damn smileys


ghost's Avatar
0 0

i have this script in directory sbin, its not fully completed yet

{
     string command;
     system(&quot;cls&quot;);
     commands:
     cout &lt;&lt; &quot;/sbin&gt;&quot;;
     cin &gt;&gt; command;
     if ( command == &quot;help&quot; )
     {
     cout &lt;&lt; &quot;FC6 bash help&#92;n&quot;;
     cout &lt;&lt; &quot;======================&#92;n&quot;;
     cout &lt;&lt; &quot;&#92;n&quot;;
     cout &lt;&lt; &quot;cd ------- Change Directory - Navigate around the file system&#92;n&quot;;
     cout &lt;&lt; &quot;cat ------ Open a file to view its contents&#92;n&quot;;
     cout &lt;&lt; &quot;help ----- Show help&#92;n&quot;;
     cout &lt;&lt; &quot;ls ------- List files in a directory&#92;n&quot;;
     cout &lt;&lt; &quot;cls ------ Clear Screen&#92;n&quot;;
     cout &lt;&lt; &quot;rm ------- Remove File&#92;n&quot;;
     cout &lt;&lt; &quot;su -------- Login you as root
     cout &lt;&lt; &quot;shutdown - Shutdown the system
     cout &lt;&lt; &quot;&#92;n&quot;;
     goto commands;
     }
     if ( command == &quot;ls&quot; )
     {
     bool shutdown = true;
     cout &lt;&lt; &quot;netcat&#92;n&quot;;
     cout &lt;&lt; &quot;nmap&#92;n&quot;;
     cout &lt;&lt; &quot;ifconfig&#92;n&quot;;
     cout &lt;&lt; &quot;ping&#92;n&quot;;
     cout &lt;&lt; &quot;reboot&#92;n&quot;;
     if (shutdown)
     cout &lt;&lt; &quot;shutdown&#92;n&quot;;
     cout &lt;&lt; &quot;syscntrl0&#92;n&quot;;
     cout &lt;&lt; &quot;&#92;n&quot;;
     goto commands;
     }
     if ( command == &quot;cls&quot; )
     {
     system(&quot;cls&quot;);
     goto commands;
     }
     if ( command == &quot;rm shutdown&quot; )
     {
     bool shutdown = false;
     cout &lt;&lt; &quot;File shutdown was successfully removed!&#92;n&quot;;
     cout &lt;&lt; &quot;&#92;n&quot;;
     goto commands;
     else
     {
     cout &lt;&lt; &quot;ERROR: Command not recognized&#92;n&quot;;
     cout &lt;&lt; &quot;&#92;n&quot;;
     goto commands;
     }```
when i have if ( command == &quot;rm shutdown&quot; ) i get ERROR: Command not recognized i dont know why.. what should i have to do? and i was tryed this without spaces &quot;rmshutdown&quot; and i get &quot;File shutdown was successfully removws!&quot; but when i typed again command &quot;ls&quot; the shutdown appears again i dont know what i´m doing wrong :(

ghost's Avatar
0 0

When you type "ls", it sets shutdown to true again. Make it do this near the start of the prog:

bool shutdown = true;

then when you do "rm shutdown", make it do this:

shutdown = false;

As for the spacing issue, I'm stumped


ghost's Avatar
0 0

i am sending you a full source code can you pls compile it and check it?

#include &lt;string&gt;
#include &lt;windows.h&gt;
using namespace std;

void sbin();

int main()
{
    bool shutdown = true;
    cout &lt;&lt; &quot;Welcome in dancuc bash simulator&#92;n&quot;;
    cout &lt;&lt; &quot;&#92;n&quot;;
    cout &lt;&lt; &quot;This simulator is used as an example, note you&#92;n&quot;;
    cout &lt;&lt; &quot;starting without root privilegies!!&quot;;
    cout &lt;&lt; &quot;&#92;n&quot;;
    system(&quot;pause&quot;);
    system(&quot;cls&quot;);
    sbin();
    return 0;
}

void sbin()
{
     string command;
     system(&quot;cls&quot;);
     commands:
     cout &lt;&lt; &quot;/sbin&gt;&quot;;
     cin &gt;&gt; command;
     if ( command == &quot;help&quot; )
     {
     cout &lt;&lt; &quot;Function list and uses&#92;n&quot;;
     cout &lt;&lt; &quot;======================&#92;n&quot;;
     cout &lt;&lt; &quot;&#92;n&quot;;
     cout &lt;&lt; &quot;cd ------- Change Directory&#92;n&quot;;
     cout &lt;&lt; &quot;cat ------ Open a file in mode r&#92;n&quot;;
     cout &lt;&lt; &quot;help ----- Show help&#92;n&quot;;
     cout &lt;&lt; &quot;ls ------- List files&#92;n&quot;;
     cout &lt;&lt; &quot;cls ------ Clear Screen&#92;n&quot;;
     cout &lt;&lt; &quot;rm ------- Remove File&#92;n&quot;;
     cout &lt;&lt; &quot;su ------- Give to current user a root privilegies&#92;n&quot;;
     cout &lt;&lt; &quot;shutdown-- Shutdown the system&#92;n&quot;;
     cout &lt;&lt; &quot;reboot---- Reboots the system&#92;n&quot;;
     cout &lt;&lt; &quot;&#92;n&quot;;
     goto commands;
     }
     if ( command == &quot;ls&quot; )
     {
     cout &lt;&lt; &quot;netcat&#92;n&quot;;
     cout &lt;&lt; &quot;nmap&#92;n&quot;;
     cout &lt;&lt; &quot;ping&#92;n&quot;;
     cout &lt;&lt; &quot;reboot&#92;n&quot;;
     cout &lt;&lt; &quot;tracert&#92;n&quot;;
     if(shutdown)
     cout &lt;&lt; &quot;shutdown&#92;n&quot;;
     cout &lt;&lt; &quot;kernel&#92;n&quot;;
     cout &lt;&lt; &quot;root&#92;n&quot;;
     cout &lt;&lt; &quot;&#92;n&quot;;
     goto commands;
     }
     if ( command == &quot;cls&quot; )
     {
     system(&quot;cls&quot;);
     goto commands;
     }
     if ( command == &quot;rmshut&quot; ) //here is problem, and also have
     {                                      //problems with that bools
     bool shutdown = false;
     cout &lt;&lt; &quot;Shutdown was successfully deleted&#92;n&quot;;
     cout &lt;&lt; &quot;&#92;n&quot;;
     goto commands;
     }
     else
     {
     cout &lt;&lt; &quot;ERROR: Unrecognized command&#92;n&quot;;
     cout &lt;&lt; &quot;&#92;n&quot;;
     goto commands;
     }
}```
thanks in advance

ghost's Avatar
0 0

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 == &quot;roxorz my soxorz&quot;) cout &lt;&lt; &quot;shit I&#39;m leet!&quot; &lt;&lt; endl;

My suggestion would be to learn how to use basic loops so you can fix your incorrect use of goto. ;)

~T


ghost's Avatar
0 0

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 ;)


ghost's Avatar
0 0

Read up on using substr().

That will let you check the first few characters of the command, and see if it is "rm", then if the rest of the command is invalid, it can give the error.


ghost's Avatar
0 0

ok, so i googled for substr() and i get something informations, but how can i use substr() function in a condition? i tryed this: markupif ( command.find(&quot;rm&quot;) ) but i every get ERROR: Command not found what is wrong ?


ghost's Avatar
0 0

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


ghost's Avatar
0 0

thx thats all :D again thanks a lot to all helpers you helped me so much ! ;)