Thread: Comparing strings - beginner question

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    4

    Comparing strings - beginner question

    So I've just started out programming more or less and have run into trouble. I'm going through the c++ tutorial on the site and so far doing pretty well I believe. I've tried searching about the web for a reason for the problem stated below, but have come up empty handed - mainly I suspect because of an inability to phrase my question right as my programming vocabulary I still sadly lacking.

    I'll work around the problem for now, but this is the first thing I just plain don't understand - and I'd like to. Any help is greatly appreciated.

    Why does this work;


    Code:
    #include <iostream>
    #include <cstring>
    
    
    using namespace std;
    
    
    int main()
    {
        char a[2], c[2], b[2], asdf = 'A';
    
    
        if (strcmp (a, c) + strcmp (a, c) == 2) {
            cout << "They're all the same.";
            cin.get();
    
    
        }
    
    
        else {
            cout << "They're not the same. Silly C++ - they actually are.";
            cin.get();
        }
    
    
    }

    While this returns error "array must be initialised with brace-enclosed initializor"?

    Code:
    #include <iostream>
    #include <cstring>
    
    
    using namespace std;
    
    
    int main()
    {
        char a[2], c[2], b[2] = 'A';
    
    
        if (strcmp (a, c) + strcmp (a, c) == 2) {
            cout << "They're all the same.";
            cin.get();
    
    
        }
    
    
        else {
            cout << "They're not the same. Silly C++ - they actually are.";
            cin.get();
        }
    
    
    }
    Note all I've done is remove the not-used asdf variable. I really don't get this, but I'll work around it in the cmd Tic Tac Toe program I'm trying to create.


    Thanks a lot for your time - I hope I'm not unknowingly breaking any forum rules, but if I am I'd like help in keeping with them as well. The site so far has been by far the best resource for learning for me a complete beginner. Thanks!


    EDIT; The above is just a test-program for me to try out theories and principles in. It's not part of the actual program I'm trying to create - merely me trying to understand a concept.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Yasha
    While this returns error "array must be initialised with brace-enclosed initializor"?
    Because you are trying to initialise b, which is an array of 2 char, as if it were a single char. You probably wanted to use a string literal like "A" instead.

    By the way, this is bad:
    Code:
    strcmp (a, c) + strcmp (a, c) == 2
    strcmp will return a positive integer if a is greater than c, but that integer does not have to be 1. (Also, you're just repeating strcmp(a, c) anyway.)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    4
    I see. I should probably just get some sleep if I'm making mistakes as simple this. I'd just really love to have this finished - I guess you know what that's like Thanks for the input. The goal for me is trying to find a way to identify when a player has got three in a row. The methods I've tried either result in nothing happening or whoever finishes a sequence involving either X or O's or a mix thereof get's credit. It's anonnoying the hell out of me - enough that I think it might be too advanced and that I should return to this later. I know finishing this would make my software-engineer-dad extremely proud though (as I work in a completely unrelated field).This was posted as a solution to a similar issue;


    This was posted as an answer to a similar issue;

    Code:
    #include <iostream.h>
    int main() { char aline[125]; char sline[125]; char lbracket[2] = {"["}; char rbracket[2] = {"]"}; cout<<"Type in aline"<<endl; cin>>aline; if (aline[0]==lbracket[0]) { cout<<"The first character was indeed a: [ "<<endl; } else { cout<<"The first character was not a: [ "<<endl; } int stop; cin>>stop;
    }

    Why is this not comparable to;

    Code:
    #include <iostream>#include <cstring>
    
    
    using namespace std;
    
    
    int main()
    {
        char a[2], c[2], b[2] = {"A"};
    
    
        if (a[0] == b[0]) {
            cout << "They're all the same.";
            cin.get();
    
    
        }
    
    
        else {
            cout << "They're not the same. Silly C++ - they actually are.";
            cin.get();
        }
    
    
    }

    Which for me does not work. It displays the else-message.

    I apparently and sadly do not understand strings at all :/


    Defining each char seperately made it work. Why - I do not understand.
    Last edited by Yasha; 12-12-2011 at 10:31 PM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Yasha
    Which for me does not work. It displays the else-message.
    Because null terminated strings should be compared by using strcmp (or strncmp), not operator==. If you want the more "natural" operator== syntax, use std::string instead of null terminated strings.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    4
    So I think I got around the problem with the code below which should allow me to compare the three. It seems to work. Basing my 'real' program on this. It's correct, no?


    Code:
    #include <iostream>
    #include <cstring>
    
    
    using namespace std;
    
    
    int main()
    {
        char a[2] = {"A"};
        char c[2] = {"A"};
        char b[2] = {"A"};
    
    
    //Display variables defined above just to be certain what they are.
        cout << a << b << c << endl;
    //Again for clarification to myself displaying result of equation used below. 
        cout << (a[0] == b[0]) << endl;
    
    
        if (a[0] == b[0] && a[0] == c[0]) {
            cout << "They're all the same.";
            cin.get();
    
    
        }
    
    
        else {
            cout << "They're not the same. Silly C++ - they actually are.";
            cin.get();
        }
    
    
    }

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This:
    Code:
    char a[2] = {"A"};
    char c[2] = {"A"};
    char b[2] = {"A"};
    should be:
    Code:
    char a[2] = "A";
    char c[2] = "A";
    char b[2] = "A";
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Dec 2011
    Posts
    4
    Quote Originally Posted by laserlight View Post
    This:
    Code:
    char a[2] = {"A"};
    char c[2] = {"A"};
    char b[2] = {"A"};
    should be:
    Code:
    char a[2] = "A";
    char c[2] = "A";
    char b[2] = "A";

    I tried it and it works too. But what's the difference? When should I use the curly brackets? EDIT; When writing to an array?


    Also - should I upload the code for the finalized Tic Tac Toe for anyone interested in what I'm using this for?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [VERY Beginner question] Strings + functions + ...pointers?!
    By arthurhess in forum C++ Programming
    Replies: 4
    Last Post: 12-08-2009, 02:44 PM
  2. Beginner Question: Problem with scanf and strings. Help!
    By lucidrave in forum C Programming
    Replies: 8
    Last Post: 08-11-2009, 10:22 PM
  3. Replies: 2
    Last Post: 04-29-2009, 10:13 AM
  4. beginner question about strings
    By gp364481 in forum C Programming
    Replies: 4
    Last Post: 09-05-2008, 06:31 PM
  5. Comparing Strings
    By zdude in forum C Programming
    Replies: 1
    Last Post: 10-04-2002, 07:47 PM