Strings C++

Discussion in 'C++' started by luan@flypublicidade.com.br, Nov 6, 2006.

  1. Guest

    Hello. I have a problem with strings in C++.
    I'm using gcc/g++ gnu/linux compiler.
    How i can do tests with strings and constants in C++?

    Eg:
    char string[10];
    if (string == "test") {
    printf("OK");
    }

    I'm wating for help.
    Thanks.
     
    , Nov 6, 2006
    #1
    1. Advertising

  2. MHL Guest

    wrote:
    > Hello. I have a problem with strings in C++.
    > I'm using gcc/g++ gnu/linux compiler.
    > How i can do tests with strings and constants in C++?
    >
    > Eg:
    > char string[10];
    > if (string == "test") {
    > printf("OK");
    > }
    >
    > I'm wating for help.
    > Thanks.


    you either use strcmp or create a == operator. you can't compare string
    to string directly.

    hope this will help.
     
    MHL, Nov 6, 2006
    #2
    1. Advertising

  3. Guest

    > wrote:
    > > Hello. I have a problem with strings in C++.
    > > I'm using gcc/g++ gnu/linux compiler.
    > > How i can do tests with strings and constants in C++?
    > >
    > > Eg:
    > > char string[10];
    > > if (string == "test") {
    > > printf("OK");
    > > }
    > >
    > > I'm wating for help.
    > > Thanks.

    >
    > you either use strcmp or create a == operator. you can't compare string
    > to string directly.
    >
    > hope this will help.


    I've used strcmp, but for example if i compare "love" and "ovel" it
    will return true.
    And its is a problem, cause it isn't true.
    Thanks.
     
    , Nov 6, 2006
    #3
  4. Ian Collins Guest

    wrote:
    > Hello. I have a problem with strings in C++.
    > I'm using gcc/g++ gnu/linux compiler.
    > How i can do tests with strings and constants in C++?
    >
    > Eg:
    > char string[10];


    That isn't a C++ string.

    > if (string == "test") {
    > printf("OK");
    > }
    >

    Use C++ strings.

    std::string whatever;

    if( whatever == "test ) ...

    --
    Ian Collins.
     
    Ian Collins, Nov 6, 2006
    #4
  5. Simias Guest

    "" <> writes:
    > I've used strcmp, but for example if i compare "love" and "ovel" it
    > will return true.


    No, it won't: 'l' != 'o', so it will return `false', that is != 0
    (strcmp returns 0 only if both strings are equal, maybe that's your mistake?)

    > And its is a problem, cause it isn't true.
    > Thanks.
    >


    --
    Simias
     
    Simias, Nov 6, 2006
    #5
  6. Markus Moll Guest

    Hi

    wrote:

    > I've used strcmp, but for example if i compare "love" and "ovel" it
    > will return true.
    > And its is a problem, cause it isn't true.


    $ man strcmp
    [...]
    The strcmp() function compares the two strings s1 and s2. It returns
    an integer less than, equal to, or greater than zero if s1 is found,
    respectively, to be less than, to match, or be greater than s2.

    You should first know how things work before you try to use them.

    Markus
     
    Markus Moll, Nov 6, 2006
    #6
  7. David Harmon Guest

    On 5 Nov 2006 16:11:22 -0800 in comp.lang.c++,
    "" <> wrote,
    >Hello. I have a problem with strings in C++.


    Use the std::string class found in #include <string>
    See any decent C++ textbook for details.
    Avoid using naked char arrays as a string-substitute.
     
    David Harmon, Nov 6, 2006
    #7
  8. Jim Langston Guest

    <> wrote in message
    news:...
    > Hello. I have a problem with strings in C++.
    > I'm using gcc/g++ gnu/linux compiler.
    > How i can do tests with strings and constants in C++?
    >
    > Eg:
    > char string[10];
    > if (string == "test") {
    > printf("OK");
    > }
    >
    > I'm wating for help.
    > Thanks.


    If you use a c++ std::string it'll do what you want.

    std::string MyString = "test";
    if ( MyString == "test" ) {
    std::cout << "OK" << std::endl;
    }

    If you use C style strings (char array) you gotta use c-string functions.

    char MyString[10];
    strcpy( MyString, "test" );
    if ( strcmp( MyString, "test" ) == 0 ) {
    std::cout << "OK" << std:;endl;
    }

    std::string is much prefered.
     
    Jim Langston, Nov 6, 2006
    #8
  9. arnuld Guest

    wrote:
    > Hello. I have a problem with strings in C++.
    > I'm using gcc/g++ gnu/linux compiler.
    > How i can do tests with strings and constants in C++?
    >
    > Eg:
    > char string[10];
    > if (string == "test") {
    > printf("OK");
    > }


    as other folks have mentioned, you are using C in C++. it is not a
    standard C++ programme. try this one using C++ strings:

    // C++ strings

    #include <iostream>
    #include <string>

    int main() {
    std::string s;
    std::cout << "guess the word: ";
    std::cin >> s;

    if(s == "test")
    std::cout << "You guessed :)\n";
    else
    std::cout << "NOPE, you are wrong.\n";
    }

    you can customise it to your requirements. i just gave you an idea.

    > I'm wating for help.


    hey, a newbie helped you ;-)

    > Thanks.


    not a problem ;-)


    -- arnuld
    http://arnuld.blogspot.com
     
    arnuld, Nov 6, 2006
    #9
  10. Ron Natalie Guest

    MHL wrote:
    > wrote:
    >> Hello. I have a problem with strings in C++.
    >> I'm using gcc/g++ gnu/linux compiler.
    >> How i can do tests with strings and constants in C++?
    >>
    >> Eg:
    >> char string[10];
    >> if (string == "test") {
    >> printf("OK");
    >> }
    >>
    >> I'm wating for help.
    >> Thanks.

    >
    > you either use strcmp or create a == operator. you can't compare string
    > to string directly.
    >
    > hope this will help.
    >

    You can not create a == operator for arrays or pointers, only
    user defined types (classes and enums). The true answer is that
    the C++ string class is called string.
     
    Ron Natalie, Nov 6, 2006
    #10
  11. Ron Natalie Guest

    wrote:

    >
    > I've used strcmp, but for example if i compare "love" and "ovel" it
    > will return true.
    > And its is a problem, cause it isn't true.
    > Thanks.
    >


    strcmp doesn't return a boolean. It returns zero if
    the strings are equal, or a positive or negative value
    if they are not.
     
    Ron Natalie, Nov 6, 2006
    #11
  12. Ron Natalie Guest

    Simias wrote:
    > "" <> writes:
    >> I've used strcmp, but for example if i compare "love" and "ovel" it
    >> will return true.

    >
    > No, it won't: 'l' != 'o', so it will return `false', that is != 0
    > (strcmp returns 0 only if both strings are equal, maybe that's your mistake?)
    >
    >>

    strcmp returns zero if the strings are equal. The return value is int
    not bool.
     
    Ron Natalie, Nov 6, 2006
    #12
  13. Guest

    Here is my code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>

    #define false 0;
    #define true 1;

    typedef int BOOL;

    //interpret comand
    BOOL Comando(char *cmd) {
    int i = 0;
    char interp[8];

    while ((cmd != ' ') && (cmd != '\0')) {
    interp = toupper(cmd);
    i++;
    }

    interp = '\0';

    if ((strcmp(interp,"JOGO") == 0)) {
    return true;
    }
    else {
    return false;
    }

    }

    int main() {
    char comand[50];
    printf("OK.\n\r");
    fgets(comand,50,stdin);
    if (Comando(comand)) {
    printf("It's a valid comand.\n\r");
    }
    else {
    printf("It ins't a valid comand.\n\r");
    }
    return 0;
    }

    What's wrong?
    Thanks.
     
    , Nov 6, 2006
    #13
  14. pzehnder Guest

    wrote:
    > Hello. I have a problem with strings in C++.
    > I'm using gcc/g++ gnu/linux compiler.
    > How i can do tests with strings and constants in C++?
    >
    > Eg:
    > char string[10];
    > if (string == "test") {
    > printf("OK");
    > }
    >
    > I'm wating for help.
    > Thanks.


    You could try:

    char s[10];
    ....
    if (std::string(s) == "test") {
    std::cout << "OK" << std::endl;
    }
     
    pzehnder, Nov 6, 2006
    #14
  15. Guest

    > wrote:
    > > Hello. I have a problem with strings in C++.
    > > I'm using gcc/g++ gnu/linux compiler.
    > > How i can do tests with strings and constants in C++?
    > >
    > > Eg:
    > > char string[10];
    > > if (string == "test") {
    > > printf("OK");
    > > }
    > >
    > > I'm wating for help.
    > > Thanks.

    >
    > You could try:
    >
    > char s[10];
    > ...
    > if (std::string(s) == "test") {
    > std::cout << "OK" << std::endl;
    > }


    when i put:
    std::string -> this generate a error =(
     
    , Nov 6, 2006
    #15
  16. On 5 Nov 2006 20:36:56 -0800, "arnuld" <> wrote:
    > wrote:
    >> char string[10];
    >> if (string == "test") {
    >> printf("OK");
    >> }

    >
    >as other folks have mentioned, you are using C in C++. it is not a
    >standard C++ programme.


    What's not standard C++ in the above code? It doesn't print "OK" but
    that's another question ...

    Best wishes,
    Roland Pibinger
     
    Roland Pibinger, Nov 6, 2006
    #16
  17. Andre Kostur Guest

    "" <> wrote in
    news::

    > Here is my code:
    > #include <stdio.h>
    > #include <string.h>
    > #include <ctype.h>
    >
    > #define false 0;
    > #define true 1;
    >
    > typedef int BOOL;


    Why are you redefining the bool type? C++ has one. Use it.

    >
    > //interpret comand
    > BOOL Comando(char *cmd) {
    > int i = 0;
    > char interp[8];
    >
    > while ((cmd != ' ') && (cmd != '\0')) {
    > interp = toupper(cmd);
    > i++;
    > }
    >
    > interp = '\0';
    >
    > if ((strcmp(interp,"JOGO") == 0)) {
    > return true;
    > }
    > else {
    > return false;
    > }
    >
    > }
    >
    > int main() {
    > char comand[50];
    > printf("OK.\n\r");
    > fgets(comand,50,stdin);
    > if (Comando(comand)) {
    > printf("It's a valid comand.\n\r");
    > }
    > else {
    > printf("It ins't a valid comand.\n\r");
    > }
    > return 0;
    > }
    >
    > What's wrong?


    Doesn't fgets also capture the newline character? Try displaying the
    length of the input string in the Commando function. Also, if I entered
    a 50 character command in main, you'll get Undefined Behaviour in the
    Commando function. Your while loop won't stop at the end of the 8
    character array that you've defined in Commando.
     
    Andre Kostur, Nov 6, 2006
    #17
  18. Andre Kostur Guest

    "" <> wrote in
    news::

    >> wrote:
    >> > Hello. I have a problem with strings in C++.
    >> > I'm using gcc/g++ gnu/linux compiler.
    >> > How i can do tests with strings and constants in C++?
    >> >
    >> > Eg:
    >> > char string[10];
    >> > if (string == "test") {
    >> > printf("OK");
    >> > }
    >> >
    >> > I'm wating for help.
    >> > Thanks.

    >>
    >> You could try:
    >>
    >> char s[10];
    >> ...
    >> if (std::string(s) == "test") {
    >> std::cout << "OK" << std::endl;
    >> }

    >
    > when i put:
    > std::string -> this generate a error =(
    >


    We're not mind-readers. What's the error?
     
    Andre Kostur, Nov 6, 2006
    #18
  19. Earl Purple Guest

    On Nov 6, 2:11 pm, ""
    <> wrote:
    > Here is my code:
    > #include <stdio.h>
    > #include <string.h>
    > #include <ctype.h>
    >
    > #define false 0;
    > #define true 1;
    >
    > typedef int BOOL;
    >
    > //interpret comand
    > BOOL Comando(char *cmd) {
    > int i = 0;
    > char interp[8];
    >
    > while ((cmd != ' ') && (cmd != '\0')) {
    > interp = toupper(cmd);
    > i++;
    > }
    >
    > interp = '\0';
    >
    > if ((strcmp(interp,"JOGO") == 0)) {
    > return true;
    > }
    > else {
    > return false;
    > }
    >
    > }int main() {
    > char comand[50];
    > printf("OK.\n\r");
    > fgets(comand,50,stdin);
    > if (Comando(comand)) {
    > printf("It's a valid comand.\n\r");
    > }
    > else {
    > printf("It ins't a valid comand.\n\r");
    > }
    > return 0;
    >
    > }What's wrong?


    You're in the wrong newsgroup. Try comp.lang.c
     
    Earl Purple, Nov 6, 2006
    #19
  20. Earl Purple Guest

    On Nov 6, 2:30 pm, Andre Kostur <> wrote:
    > "" <> wrote innews::
    >
    > > Here is my code:
    > > #include <stdio.h>
    > > #include <string.h>
    > > #include <ctype.h>

    >
    > > #define false 0;
    > > #define true 1;

    >
    > > typedef int BOOL;Why are you redefining the bool type? C++ has one. Use it.


    What is worse is that he has redefined two C++ reserved words with
    pre-processors.
     
    Earl Purple, Nov 6, 2006
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kurt Krueckeberg
    Replies:
    2
    Views:
    718
    =?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?=
    Nov 17, 2004
  2. Rick

    Comparing strings from within strings

    Rick, Oct 21, 2003, in forum: C Programming
    Replies:
    3
    Views:
    388
    Irrwahn Grausewitz
    Oct 21, 2003
  3. Klaus Neuner
    Replies:
    7
    Views:
    502
    Klaus Neuner
    Jul 26, 2004
  4. Girish Sahani
    Replies:
    17
    Views:
    576
    Boris Borcic
    Jun 9, 2006
  5. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    772
    Malcolm
    Jun 24, 2006
Loading...

Share This Page