Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: if in C++

  1. #1
    Senior Member
    Join Date
    Jun 2004
    Posts
    379

    if in C++

    Hi I have a small question abotu c++ if statments what i want to do is let a user choose one of 2 commands but the commands are not numbers their words like unix commands so here is what i am doing.

    Code:
    char com2;
    if (com2 == help)
     {
     cout << "commands" << endl;
     cout << "ls" << endl;
     }
     if (com2 == ls)
     {
     cout << "etc" << endl;
     cout << "root" << endl;
     cout << "home" << endl;
     }
    else
    {
    cout << "sorry command not found" << endl;
    }
    ok that is an example of something i want to do now when i try to compile it i get an error saying saying 'help' undeclared ( first use of function) I am using DevC++ to compile this. if any one has any suggestion on what i could do to fix this i would be very thankful.

  2. #2
    Junior Member
    Join Date
    Feb 2005
    Posts
    14
    "

    char com2;
    if (com2 == help)

    "

    i'd suggest u use an array like this:

    char com2[(no.of characters)];

    if(com2=="help") //notice the quatation marks in help.ls should also be enclosed in // quatation marks.

    alternatively u can also declare com2 to be a string variable.

  3. #3
    Leftie Linux Lover the_JinX's Avatar
    Join Date
    Nov 2001
    Location
    Beverwijk Netherlands
    Posts
    2,534
    you need to add quotes arround the "help".. Now the compiler thinks help is a function or a variable.. Qoutes around "ls" are needed too !
    ASCII stupid question, get a stupid ANSI.
    When in Russia, pet a PETSCII.

    Get your ass over to SLAYRadio the best station for C64 Remixes !

  4. #4
    Senior Member
    Join Date
    Jun 2004
    Posts
    379
    ok thank you i will fix my code and post back on how it went.

  5. #5
    Senior Member
    Join Date
    Mar 2004
    Posts
    557
    Hi Riot

    I recommend to do as xxxx123 suggested, however it still won't work
    (correct me if I am wrong).

    Code:
    #include<string.h>
    
    char com2[1024]; // 1024 is some choosen buffersize for simplicity
    (...)
    if(strcmp(com2,"help")==0){
    }else{
    }
    will work. See more detailed explanation[1].

    Cheers.


    [1] http://www.antionline.com/showthread...r=1#post822939
    If the only tool you have is a hammer, you tend to see every problem as a nail.
    (Abraham Maslow, Psychologist, 1908-70)

  6. #6
    Senior Member
    Join Date
    Jun 2004
    Posts
    379
    Thanks guys worked great, when i get the prog finished maby i will post it here k.
    Neavormind i got it to work.

  7. #7
    Junior Member
    Join Date
    Feb 2005
    Posts
    14
    sec_ware,

    i used microsoft visual c++ 6.the program compiled without error but it dosen't do what it's supposed to do.i forgot to test it.
    anyway,
    what does:

    com2=="help";

    do?



    thanks

  8. #8
    Senior Member
    Join Date
    Jun 2004
    Posts
    379

    anyway,
    what does:

    com2=="help";

    do?



    thanks
    well it is not the hole code their is about 400 lines of it what i did last night cuz i didnt have any thing else to do i maide a game and in the begging you pick who you want to hack anyway to hack who you pick it takes about 5 second to go through the steps to get in so to make it really cool what i did was make a replica of a unix/linux system not fully thow just some of the files and you can go through some of the file tree using some of the command help tells you what commands are available the only commands i put in wear help,cd( you can use "cd ../" to go back a dir as well as more forward with "cd etc") and ls and using these commands you can travel through the system and in some of the file their are more files you can go into. i think it will be cool once i get it working and add some more stuff to it.

  9. #9
    Senior Member
    Join Date
    Mar 2004
    Posts
    557
    Hi

    Its really astonishing that devc++ interprets that command correctly.
    As xxxx123 realised, Microsoft Visual C++ does not what expected.
    Neither does gcc 2.95.4, icc 7.0, nor KCC .

    In Microsoft Visual C++ 'com2=="help" ' is
    Code:
    00401068   lea         eax,[ebp-14h]
    0040106B   cmp        eax,offset string "help" 
    00401070   jne         main+75h

    Cheers.
    If the only tool you have is a hammer, you tend to see every problem as a nail.
    (Abraham Maslow, Psychologist, 1908-70)

  10. #10
    Senior Member
    Join Date
    Jun 2004
    Posts
    379
    sec_ware your right after i compiled it it dosnt work that correct it allways executiets the the first if even if it is not true here is my first section of code ware the problem is taken place. any one know what is wrong.

    Code:
    #include <iostream.h>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
    char com1[8];
    char com2[8];
    char comi2[8];
    char comi3[8];
    char comi4[8];
    char comi5[8];
    
     root:
     cout << "/root:";
     cin >> com1;
     if (com1 == "help");
     {
     cout << "commands" << endl;
     cout << "cd" << endl;
     cout << "ls" << endl;
     goto root;
     }
     if (com1 == "ls")
     {
     cout << "bugreport" << endl;
     cout << "landing" << endl;
     cout << "watergate" << endl;
     cout << "WOMDIRAQ" << endl;
     goto root;
     }
     if (com1 == "cd ../")
     {
     goto rootd;
     }
     else
     {
     cout << "BASH command not found:" << endl;
     goto root;
      }

Posting Permissions

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