if in C++ - Page 2
Page 2 of 2 FirstFirst 12
Results 11 to 16 of 16

Thread: if in C++

  1. #11
    Banned
    Join Date
    Jun 2003
    Posts
    1,302
    First thing I will say is, in C++ you don't actually need to ever use a goto, and using it to loop is a bit underdevoloped IMHO. Do a look up on try and catch statements. They are what you use instead of goto's.

    Second why don't use just use include <string>?


    A nice way you can do that without your goto is.


    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
    
    string com1;
    cout << "Enter a Command" << endl;
    cout << "Enter help for list of commands" << endl;
    cin.get(com1);
    while(com != "exit")
    {
    
                 if(com1 == "help")
    {              cout << "commands" << endl;
                    cout << "cd" << endl;
                    cout << "ls" << endl;
                    cout << "exit" << endl;
    
    }
    
                  else if (com1 == "ls")
    {
                  cout &lt;&lt; "bugreport" &lt;&lt; endl;
                  cout &lt;&lt; "landing" &lt;&lt; endl;
                  cout &lt;&lt; "watergate" &lt;&lt; endl;
                  cout &lt;&lt; "WOMDIRAQ" &lt;&lt; endl;
    }
    
                  else if (com1 == "cd ..")
    {
                   cout &lt;&lt; "/root:";
    }
                else if (com1 == "exit")
    {
    
    break;
    }
                 else
    {
    
                    cout &lt;&lt; "Command not found" &lt;&lt; endl;
    }
    
    cin &gt;&gt; com1;
    }
    }

  2. #12
    Junior Member
    Join Date
    Feb 2005
    Posts
    14
    riot,


    maybe u overlooked this:

    " if (com1 == "help");
    {
    cout &lt;&lt; "commands" &lt;&lt; endl;
    cout &lt;&lt; "cd" &lt;&lt; endl;
    cout &lt;&lt; "ls" &lt;&lt; endl;
    goto root;
    } "

    there shouldn't be any semicolon after " if( /*condition*/ ) "otherwise the instruction following it won't get executed,i think.

    also why not use a switch statement?

  3. #13
    Custom User
    Join Date
    Oct 2001
    Posts
    503
    also why not use a switch statement?
    switch only works for integers.

    The reason that if(string == "astring") {...} does not work is because it is not actually comparing two strings. The identifier, "string," is actually a pointer to the first memory address of the character array, therefore you are comparing a pointer for equality with a character constant (String). Therefore, what you are saying is:

    if the character pointer string is equal to "astring" then do something.

    This will not evaluate to true because "astring" is not a character pointer.

    Now I'm sure there are some false statements in the above, I'd be grateful if someone could try to explain what I've just said in more depth.

    So basically, what the above means is that you have to use strcmp(...) in the string.h header file like sec_ware (I think it was) already said.

    ac

  4. #14
    Senior Member
    Join Date
    Jun 2004
    Posts
    379
    Ok thanks for the explainations i did use what sec_ware said i should do and it compiles nice
    and the first 2 parts work but the second part dosent even thow the code is exactly the same just with diffrent output i am going to put in some comments into my code then post it back and if some one could review it for me that would be great.

    Here is my code i cant figure out why some of my code is off i have been working on it for a long thime and also thanks guys for helping me cuz i know i am not good at this codeing stuff an am probably becoming a pain by now. Also i put all most ever thing you need to know in the comments what works what dosen't description of what it is, and if any one wants the full code i will post it.
    Tryed to make it as easy as i could to read in black and white but didn't do shouch a good job sorry.
    Code:
    #include <iostream.h>
    #include <string.h>
    using namespace std;
    
    int main(int argc, char *argv[])
    {
    
    char com1[1024];
    char com2[1024];
    char comi2[1024];
    char comi3[1024];
    char comi4[1024];
    char comi5[1024];
    
      // i took out the first section because it works fine
    
      root:// start of the unix box in
     cout << "/root:";
     cin >> com1;
    if(strcmp(com1,"help")==0){      // tells you want commands their are
    
     cout << "commands" << endl;
     cout << "cd" << endl;
     cout << "ls" << endl;
     goto root;             // brings you back to root so you will have a prompt
     }
    if(strcmp(com1,"ls")==0){            // lets you give the ls command
    
     cout << "bugreport" << endl;
     cout << "landing" << endl;
     cout << "watergate" << endl;
     cout << "WOMDIRAQ" << endl;
     goto root; // same thing as befor
     }
     if(strcmp(com1,"cd../")==0){          // same thing as  ls
     goto rootd;
     }
    
     else{
    
     cout << "BASH command not found:" << endl;       // give you an error message if you use a nonlisted command
     goto root;
      }
    
     rootd:          // is the root dir that /root is keeped in
     cout << "/:";
     cin >> com2;
        if(strcmp(com2,"help")==0){           // same thing as befor
     cout << "commands" << endl;
     cout << "cd" << endl;
     cout << "ls" << endl;
     goto rootd;
     }
    
     if(strcmp(com2,"ls")==0){        // same thing give you the list of what in the file
    
     cout << "etc" << endl;
     cout << "root" << endl;
     cout << "home" << endl;
     goto rootd;
     }
    
     if(strcmp(com2,"cd etc")==0){         // this is wear i have a problem i think this is
     goto etc;                                         // i think this is wear at least once i move from
     }                                                    // the starting /root dir the cd command dosn't work
                                                          // no matter what you neter with cd it allways brings
     etc:                                              // you to the etc dir
     cout << "/etc:";
     cin >> comi2;            // the varable that is used  tell what command you put in
                                    // i all so used an embedded if statmentto move up and down the dirs
      if(strcmp(comi2,"ls")==0){              // this command works
    
     cout << "passwd" << endl;         // contents of etc
     cout << "shadow" << endl;
     }
      if(strcmp(comi2,"cd../")==0){       // same as befor and this one works
    
     goto rootd;
     }
     else{
    
     cout << "BASH command not found:" << endl;
     goto etc;
     }
    
     if(strcmp(com2,"cd root")==0){      // dose not work will bring you from the root
     goto root;                                     // dir to the etc and will give you the error message
     }                                                  // command not found befor going into the stc dir
    
     if(strcmp(com2,"cd home")==0){        // same as "cd root"
     goto home;                                        // I have stoped puting comments here because this is the farthest
     }                                                      // I can go in the program and cant tell what works and what dosen't
                                                           //by trying it becuase i can get into thoes directories but their is
     home:                                          // a little bit more down at the bottom
     cout << "/home:";
     cin >> comi3;
     if(strcmp(comi3,"help")==0){
     cout << "commands" << endl;
     cout << "cd" << endl;
     cout << "ls" << endl;
     }
     if(strcmp(comi3,"ls")==0){
    
     cout << "stu" << endl;
     cout << "john" << endl;
     }
     if(strcmp(comi3,"cd../")==0){
    
     goto rootd;
     }
      if(strcmp(comi3,"cd stu")==0){
      goto stu;
      }
    
     stu:
     cout << "/home/stu:";
     cin >> comi4;
     if(strcmp(comi4,"help")==0){
    
     cout << "commands" << endl;
     cout << "cd" << endl;
     cout << "ls" << endl;
     }
     if(strcmp(comi4,"ls")==0){
    
     cout << "JFK" << endl;
     cout << "buglanding" << endl;
     cout << "Area 54 landing" << endl;
     cout << "UFO" << endl;
     }
     if(strcmp(comi2,"cd../")==0){
    
     goto home;
     }
    
      else{
    
     cout << "BASH command not found:" << endl;
     goto stu;
     }
    
     if(strcmp(comi2,"cd john")==0){
     goto john;
     }
    
     john:
     cout <<"/home/john:";
     cin >> comi5;
     if(strcmp(comi5,"help")==0){
    
     cout << "commands" << endl;
     cout << "cd" << endl;
     cout << "ls" << endl;
     }
     if(strcmp(comi5,"ls")==0){
    
     cout << "bugreport" << endl;
     cout << "Mexican ufo siteing" << endl;
     cout << "Big Foot" << endl;
     cout << "top secret" << endl;
     }
     if(strcmp(comi5,"cd../")==0){
    
     goto home;
     }
     else{
    
     cout << "BASH command not found:" << endl;
     goto john;
     }
     if(strcmp(com2,"creator")==0){                   // this command dose not work like all other in
     cout << "This prorham was created by Norse" << endl;         //root dir it brings you
     cout << "You can distribute this program any way you want" << endl;        // the etc dir
     cout << "but you can not take credit for its creation thank you" << endl;
     cout << "and have a good time playing" << endl;
     }
     else{
    
     cout << "BASH command not found:" << endl;
     goto rootd;
     }
    
      end:
      cout << "something went wrong sorry" << endl;         // this is just for the beggining
      cout << "press any key then enter:" << endl;             //that i cut out if you decide not
      cin >> end;                                                                   //to go further with your hack
      }
      return 0;
    }

  5. #15
    Junior Member
    Join Date
    Feb 2005
    Posts
    14
    "/ the starting /root dir the cd command dosn't work
    // no matter what you neter with cd it allways brings
    // you to the etc dir "
    this is because:


    else{

    cout &lt;&lt; "BASH command not found:" &lt;&lt; endl;
    goto etc;
    }

    whatever mistake u enter will output the message and then goto etc.i'm assuming u meant to go to rootd?

    " cin &gt;&gt; com2;"

    cin will only read up to the first blank space,therefor if the user entered:

    cd etc

    or

    cd whatever

    it will be read as 'cd' only.to solve this use the cin.getline( ) function.cin.getline read all the characters typed until it's maximum is reached or until a newline is encounterd.i'd suggest replacing every cin in ur code with cin.getline.i think that should solve the problem.i've modified a part of ur code to illustrate what i meant.







    #include &lt;iostream.h&gt;
    #include &lt;string.h&gt;


    int main(int argc, char *argv[])
    {

    char com1[1024];
    char com2[1024];
    char comi2[1024];


    // i took out the first section because it works fine

    root:// start of the unix box in
    cout &lt;&lt; "/root:";

    cin.getline(com1,80);

    if(strcmp(com1,"help")==0){ // tells you want commands their are

    cout &lt;&lt; "commands" &lt;&lt; endl;
    cout &lt;&lt; "cd" &lt;&lt; endl;
    cout &lt;&lt; "ls" &lt;&lt; endl;
    goto root; // brings you back to root so you will have a prompt
    }
    if(strcmp(com1,"ls")==0){ // lets you give the ls command

    cout &lt;&lt; "bugreport" &lt;&lt; endl;
    cout &lt;&lt; "landing" &lt;&lt; endl;
    cout &lt;&lt; "watergate" &lt;&lt; endl;
    cout &lt;&lt; "WOMDIRAQ" &lt;&lt; endl;
    goto root; // same thing as befor
    }
    if(strcmp(com1,"cd../")==0){ // same thing as ls
    goto rootd;
    }

    else{

    cout &lt;&lt; "BASH command not found:" &lt;&lt; endl; // give you an error message if you use a nonlisted command
    goto root;
    }

    rootd: // is the root dir that /root is keeped in
    cout &lt;&lt; "/:";

    cin.getline(com2,80);

    if(strcmp(com2,"help")==0){ // same thing as befor
    cout &lt;&lt; "commands" &lt;&lt; endl;
    cout &lt;&lt; "cd" &lt;&lt; endl;
    cout &lt;&lt; "ls" &lt;&lt; endl;
    goto rootd;
    }

    if(strcmp(com2,"ls")==0){ // same thing give you the list of what in the file

    cout &lt;&lt; "etc" &lt;&lt; endl;
    cout &lt;&lt; "root" &lt;&lt; endl;
    cout &lt;&lt; "home" &lt;&lt; endl;
    goto rootd;
    }

    if(strcmp(com2,"cd etc")==0){ // this is wear i have a problem i think this is
    goto etc; // i think this is wear at least once i move from
    } // the starting /root dir the cd command dosn't work

    else{

    cout &lt;&lt; "BASH command not found:" &lt;&lt; endl;
    goto rootd;
    }
    // no matter what you neter with cd it allways brings
    etc: // you to the etc dir
    cout &lt;&lt; "/etc:";
    // the varable that is used tell what command you put in
    cin.getline(comi2,80); // i all so used an embedded if statmentto move up and down the dirs
    if(strcmp(comi2,"ls")==0){ // this command works

    cout &lt;&lt; "passwd" &lt;&lt; endl; // contents of etc
    cout &lt;&lt; "shadow" &lt;&lt; endl;
    goto etc;
    }
    if(strcmp(comi2,"cd../")==0){ // same as befor and this one works

    goto rootd;
    }
    else{

    cout &lt;&lt; "BASH command not found:" &lt;&lt; endl;
    goto etc;
    }

    return 0;
    }

  6. #16
    Senior Member
    Join Date
    Jun 2004
    Posts
    379
    xxxx123 thank you for your help I very much appreshate it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •