Can't make this work

This is a discussion on Can't make this work within the C++ Programming forums, part of the General Programming Boards category; if(insane==insanityclownZ) { cout<<"WTF ! How did you know?\n"; cout<<"If you read the source code you die! insanityclownz"; } I set ...

  1. #1
    BlahBlahBlah
    Guest

    Angry Can't make this work

    if(insane==insanityclownZ)
    {
    cout<<"WTF ! How did you know?\n";
    cout<<"If you read the source code you die! insanityclownz";
    }



    I set it up to say the text inside the couts when the variable "insane" is insanityclownZ


    but there is a compile error.

    How do i make this work?

  2. #2
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    Could you post more code, what you have provided probably is not the cause of the problem.

    What was the error? What datatypes are insane and insanityclownZ?
    "Queen and huntress, chaste and fair,
    Now the sun is laid to sleep,
    Seated in thy silver chair,
    State in wonted manner keep."

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    43
    #include <iostream.h>

    int main()
    {

    cout<<"### m4 installer v0.1 ###";
    cout<<"\n";
    cout<<"### (c) Microsoft 2002 ###";
    cout<<"\n";
    cout<<"### Testers: DO NOT GIVE THIS TO YOUR FRIENDS OR ANYBODY ELSE ###";
    cout<<"\n";
    cout<<"### Press enter to begin. ###";
    cout<<"\n";
    char insane[100];
    cin>>insane;
    if(insane==insanityclownZ)
    {
    cout<<"WTF ! How did you know?\n";
    cout<<"If you read the source code you die! insanityclownz";
    }
    cin.get();

    cin.get();
    cin.get();
    return 0;
    }



    this is the whole code

  4. #4
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    A few things.

    For one, if you wanted to refer to the string insanityclownZ you'd have to say:

    "insanityclownZ"

    (notice the quotes

    Second, you can't do i direct comparison between arrays using operator==

    use strcmp from the string header

    it takes 2 parameters which are of the type const char* and it will return 0 if they are the same.

  5. #5
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    Okay, couple of things..

    1. You should use <iostream> without the .h extension.. the old <iostream.h> method has been depricated. (That is of course if your compiler is recent enough, which I believe it is.

    2. You need to make the functions in the std namespace available to your program. You can do this a couple of ways by using the "using" directive.

    either
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main(void)
    {
       // code
       return 0;
    }
    or since you are only using a couple of the functions you can explicitly include them as below.

    Code:
    #include <iostream>
    
    using std::cout;
    using std::cin;
    
    int main(void)
    {
       // code
       return 0;
    }
    You may also in your code use std::cout directly in the body of your code.

    3. insanityclownZ is not declared anywhere. You must declare a variable before you use it, or in the case of C++ you can declare it when you use it for the first time.

    4. You cannot compare strings or arrays of characters using the == operator. You can do this a couple of ways too, using the strcmp() function is one.. You would have to include the <cstring> header file.

    I have modified your code, so that upon entering the string "exit", it will execute the code that you wanted to execute.
    Code:
    #include <iostream>
    #include <cstring>
    
    // Or you can use "using namespace std;"
    using std::cout;
    using std::cin;
    
    int main(void)
    {
        char insane[100];
        char insanityclownZ[] = "exit";
    
        cout<<"### m4 installer v0.1 ###";
        cout<<"\n";
        cout<<"### (c) Microsoft 2002 ###";
        cout<<"\n";
       cout<<"### Testers: DO NOT GIVE THIS TO YOUR FRIENDS OR    ANYBODY ELSE ###";
        cout<<"\n";
        cout<<"### Press enter to begin. ###"; 
        cout<<"\n";
    
        cin>>insane;
    
        if(strcmp(insane, insanityclownZ) == 0)
        {
            cout<<"WTF ! How did you know?\n";
            cout<<"If you read the source code you die! insanityclownz";
        }
      
        cin.get();
    
        return 0;
    }
    Last edited by foniks munkee; 11-28-2002 at 03:32 AM.
    "Queen and huntress, chaste and fair,
    Now the sun is laid to sleep,
    Seated in thy silver chair,
    State in wonted manner keep."

  6. #6
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    1. You should use <iostream> without the .h extension.. the old <iostream.h> method has been depricated. (That is of course if your compiler is recent enough, which I believe it is.
    I use iostream.h, whats wrong with it? Does it lack anything??

  7. #7
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    >> if(!strcmp(insane, insanityclownZ))

    I dont know how good practice this is. The function returns 0 if a match is found. Here you are testing if it's false, which is defined as 0 on most compilers, but what you are testing for isn't exactly correct. IMO, I think you should change it to
    Code:
    if(strcmp(insane, insanityclownZ) == 0)
    becuase it does not return FALSE, it returns 0...

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    43
    I used this code, no compiling error, but when i type in insanityclownZ, it does not say anything...

    #include <iostream>

    #include <cstring>


    int main(void)
    {
    char insane[100];
    char insanityclownZ[] = "exit";

    cout<<"### m4 installer v0.1 ###";
    cout<<"\n";
    cout<<"### (c) Microsoft 2002 ###";
    cout<<"\n";
    cout<<"### Testers: DO NOT GIVE THIS TO YOUR FRIENDS OR ANYBODY ELSE ###";
    cout<<"\n";
    cout<<"### Press enter to begin. ###";
    cout<<"\n";

    cin>>insane;

    if(strcmp(insane, insanityclownZ) == 0)
    {
    cout<<"WTF ! How did you know?\n";
    cout<<"If you read the source code you die! insanityclownz";
    }

    cin.get();
    cin.get();
    return 0;
    }


    this is the code

  9. #9
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    I use iostream.h, whats wrong with it? Does it lack anything??
    It doesn't lack anything it is just that this method has been depricated, it is no longer the accepted standard and support for it will eventually be phased out.

    However it is worth noting that the header files that have dropped the .h extension also incorporate namespaces and therefore you have to include the std namespace to utilise those functions/classes etc.

    You are quite right regarding the strcmp function, it really isn't good form to do it the way I did, and I have since rectified the code. Lazyness and bad habits you see.
    "Queen and huntress, chaste and fair,
    Now the sun is laid to sleep,
    Seated in thy silver chair,
    State in wonted manner keep."

  10. #10
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    screw that - as long as my compiler supports it i'll use it

  11. #11
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    Code:
        if(strcmp(insane, "insanityclownZ") == 0)
        {
            cout<<"WTF ! How did you know?\n";
            cout<<"If you read the source code you die! insanityclownz";
        }
    That is because insanityclownZ is a variable. If you wish to test the users input against a string which contains the characters "insanityclownZ", then you can replace that block of code with what I have posted above..
    "Queen and huntress, chaste and fair,
    Now the sun is laid to sleep,
    Seated in thy silver chair,
    State in wonted manner keep."

  12. #12
    Registered User
    Join Date
    Nov 2002
    Posts
    43
    thanks.

    to use strcmp, i must have #include <cstring>?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Makefile Problem: None rule to make target
    By chris24300 in forum Linux Programming
    Replies: 25
    Last Post: 06-17-2009, 10:45 AM
  2. Game Programming FAQ
    By TechWins in forum Game Programming
    Replies: 5
    Last Post: 09-29-2004, 03:00 AM
  3. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  4. cant make it work
    By papous27 in forum Linux Programming
    Replies: 0
    Last Post: 03-05-2002, 09:49 PM
  5. how to make files
    By quiksilver9531 in forum C++ Programming
    Replies: 6
    Last Post: 02-22-2002, 06:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21