if block always false.

This is a discussion on if block always false. within the C++ Programming forums, part of the General Programming Boards category; part of my code Code: #include <iostream> #include <algorithm> #include <string> #include <fstream> using namespace std; int main () { ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    7

    if block always false.

    part of my code
    Code:
    #include <iostream>
    #include <algorithm>
    #include <string>
    #include <fstream>
    using namespace std;
    
    int main () {
      int x;
      int y;
    char a[]={};
    char b[]={};
    
      ifstream word("test1.txt");
      word>>a;
      sort(a,a+strlen(a));
      ifstream wordlist("text2.txt");
      wordlist>>b;
      sort(b,b+strlen(b));
       cout<< a<<"="<<b;
       if (a==b)
       {cout<<"hello";} //print if equal
       cin.get();
    }
    problem:
    if block is always false.
    i need char value for sort command.

  2. #2
    Cryptanalyst
    Join Date
    Sep 2007
    Posts
    52
    I guess it should be
    Code:
    if(a[0] == b[0])
    {
        .....
        .....
    }
    cin.get();
    return 0;
    Last edited by SVXX; 10-04-2007 at 06:53 AM.

  3. #3
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    You cant compare strings like that in C++. Maybe use strcmp(). IE:
    Code:
    if(! strcmp(a, b))
    {
        cout<<"hello";
    }
    [edit] The reason your code is allways false is because you are comparing two memory addresses, which will always be different.[/edit]
    Last edited by mike_g; 10-04-2007 at 06:56 AM.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    In the context of that if test, the code is comparing the address' of the two arrays in memory which will always be false. Also, your code to read the values from the user is wrong, your arrays are size 0, my compiler doesn't even allow me to do that (error C2466: cannot allocate an array of constant size 0). You've included the string header, are you hesitant to use it for some reason?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Better yet, just use C++ strings, which both allow that type of comparison and also grow dynamically. The OP is using C-style strings without allocating enough space (none, actually) for them.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes. The pointer to a and pointer to b are not likely to ever be equal, as there is no such thing as zero-length arrays. You haven't specified an array length, so I guess that b is one more than a, as the "default" size of a unsized array is 1.

    This also means that you are most likely overflowing your array when you enter anything on your input, as one char is occupied by the end of string marker.

    So, declare a[100], b[100] or some such to make them have a size and use strcmp() to compare, or use C++ string types.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    7
    thanks for the help!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 06:57 AM
  2. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  3. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  4. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM

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