string comparisons

Discussion in 'C++' started by Horn, Oct 3, 2003.

  1. Horn

    Horn Guest

    I am having trouble comparing strings. When I run the program and I
    type in what I think is the right string, it goes to the else part of
    the program. This is for my computer science class in high school so
    excuse me if you think it's really easy. If you know what I should do
    then please send me a message at

    //Britton Horn

    #include "stdafx.h"
    #include <iostream.h>
    #include <stdlib.h>
    #ifndef _OOSTRING_H
    #define _OOSTRING_H
    #include <string.h>

    int main()
    {
    char yourname[25];
    char myname[25] = "britton";
    int answer;

    cout << "What is your name?\n";
    cin.get(yourname, 25);

    if (yourname == myname)
    {
    cout << "What is 2 + 2?\n";
    cin >> answer;
    cout << "Correct!\n";
    }
    else
    {
    cout << "What is 2 + 2?\n";
    cin >> answer;
    if (answer == 4)
    {
    cout << "I'm sorry. The answer is 22.\n";
    }
    if (answer != 4)
    {
    cout << "I'm sorry. The answer is 4.\n";
    }
    }
    return 0;
    #endif
    }
     
    Horn, Oct 3, 2003
    #1
    1. Advertising

  2. Horn

    WW Guest

    Horn wrote:
    > I am having trouble comparing strings. When I run the program and I
    > type in what I think is the right string, it goes to the else part of
    > the program. This is for my computer science class in high school so
    > excuse me if you think it's really easy. If you know what I should do
    > then please send me a message at



    Posted AND mailed. LAST TIME! Post here and read here. Answers to my
    mailbox will be deleted unread:

    http://www.parashift.com/c -faq-lite/how-to-post.html#faq-5.4

    > //Britton Horn
    >
    > #include "stdafx.h"


    Non-standard header

    > #include <iostream.h>


    Non-standard header

    > #include <stdlib.h>
    > #ifndef _OOSTRING_H
    > #define _OOSTRING_H


    WHY on Earth is this in a header???

    > #include <string.h>
    >
    > int main()
    > {
    > char yourname[25];
    > char myname[25] = "britton";
    > int answer;
    >
    > cout << "What is your name?\n";
    > cin.get(yourname, 25);
    >
    > if (yourname == myname)


    Learn about strcmp. Or better: learn about std::string.

    http://www.dinkumware.com/manuals/reader.aspx?b=p/&h=string.html#strcmp

    http://www.dinkumware.com/manuals/reader.aspx?b=p/&h=string2.html

    http://www.google.com/search?hl=en&lr=&ie=UTF-8&oe=UTF-8&safe=off&q=std::string tutorial c++

    In short: http://tinyurl.com/pm6r

    > {
    > cout << "What is 2 + 2?\n";
    > cin >> answer;
    > cout << "Correct!\n";
    > }
    > else
    > {
    > cout << "What is 2 + 2?\n";
    > cin >> answer;
    > if (answer == 4)
    > {
    > cout << "I'm sorry. The answer is 22.\n";
    > }
    > if (answer != 4)
    > {
    > cout << "I'm sorry. The answer is 4.\n";
    > }
    > }
    > return 0;
    > #endif


    WHY on Earth is this (the main function) in a header???

    > }


    --
    WW aka Attila
     
    WW, Oct 3, 2003
    #2
    1. Advertising

  3. Horn

    Aggro Guest

    Horn wrote:


    > #include <string.h>
    >
    > int main()
    > {
    > char yourname[25];
    > char myname[25] = "britton";
    > int answer;
    >
    >
    > if (yourname == myname)


    Any particular reason why not using the std::string? If you must use
    char arrays, you can't compare them like this. You need to use strcmp()
    function, or write one by yourself. If you use std::string you can
    compare strings like that. Have a look at these two examples:

    -------------------------------
    #include <string>
    #include <iostream>

    int main()
    {
    std::string a = "hello";
    std::string b = "hello";

    if( a == b )
    {
    std::cout << "Strings are equal." << std::endl;
    }
    else
    {
    std::cout << "Strings are not equal." << std::endl;
    }

    return 0;
    }
    --------------------------

    ------------------------------
    #include <cstring>
    #include <iostream>

    int main()
    {
    char c[] = "hello";
    char d[] = "hello";

    if( strcmp( c,d ) == 0 )
    {
    std::cout << "Strings are equal." << std::endl;
    }
    else
    {
    std::cout << "Strings are not equal." << std::endl;
    }

    return 0;
    }
    --------------------------------
     
    Aggro, Oct 3, 2003
    #3
  4. Horn

    WW Guest

    Aggro wrote:
    > Any particular reason why not using the std::string? If you must use
    > char arrays, you can't compare them like this. You need to use
    > strcmp() function, or write one by yourself. If you use std::string
    > you can compare strings like that. Have a look at these two examples:


    Fish, or fishing?

    --
    WW aka Attila
     
    WW, Oct 3, 2003
    #4
  5. Horn

    Kris Wempa Guest

    "Horn" <> wrote in message
    news:...
    > int main()
    > {
    > char yourname[25];
    > char myname[25] = "britton";
    > int answer;
    >
    > cout << "What is your name?\n";
    > cin.get(yourname, 25);
    >
    > if (yourname == myname)


    You can't compare character strings like this. You may be able to do this
    for C++ string data types, but not for traditional C character strings. You
    need to do this:

    if (strcmp(yourname,myname) == 0)

    > cout << "What is 2 + 2?\n";
    > cin >> answer;
    > if (answer == 4)
    > {
    > cout << "I'm sorry. The answer is 22.\n";
    > }
    > if (answer != 4)
    > {
    > cout << "I'm sorry. The answer is 4.\n";
    > }
    > }


    Is this part working correctly ?
     
    Kris Wempa, Oct 3, 2003
    #5
  6. Horn wrote:

    > I am having trouble comparing strings. When I run the program and I
    > type in what I think is the right string, it goes to the else part of
    > the program. This is for my computer science class in high school so
    > excuse me if you think it's really easy. If you know what I should do
    > then please send me a message at


    I won't, and it's rude to ask. Post here, read here.

    >
    > //Britton Horn
    >
    > #include "stdafx.h"


    Incomplete code makes it difficult to diagnose problems. You haven't
    given us the contents of this header file.

    > #include <iostream.h>


    This is not a standard C++ header. Standard C++ uses <iostream>.

    > #include <stdlib.h>


    This header is deprecated in favor of <cstdlib>.

    > #ifndef _OOSTRING_H
    > #define _OOSTRING_H


    This is illegal. You may not use identifiers beginning with an
    underscore followed by either another underscore or an uppercase letter.
    They are reserved for the implementation's use. Even if your compiler
    fails to diagnose this (most won't), it is an error. A compiler may
    reject the code, or may even accept it with a dramatically altered meaning.

    The simplest rule of thumb is *never use an identifier that begins with
    an underscore, or contains a sequence of two underscores.*

    Besides that, "include guards" make no sense in a non-header file.

    > #include <string.h>


    This header is deprecated in favor of <cstring>, but you should just use
    <string> and the std::string class instead (as others have already said).
    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
     
    Kevin Goodsell, Oct 3, 2003
    #6
  7. Jonathan Mcdougall, Oct 3, 2003
    #7
  8. Horn

    Ashish Guest

    "Horn" <> wrote in message
    news:...
    > I am having trouble comparing strings. When I run the program and I
    > type in what I think is the right string, it goes to the else part of
    > the program. This is for my computer science class in high school so
    > excuse me if you think it's really easy. If you know what I should do
    > then please send me a message at
    >
    > //Britton Horn
    >
    > #include "stdafx.h"
    > #include <iostream.h>
    > #include <stdlib.h>
    > #ifndef _OOSTRING_H
    > #define _OOSTRING_H
    > #include <string.h>
    >
    > int main()
    > {
    > char yourname[25];
    > char myname[25] = "britton";
    > int answer;
    >
    > cout << "What is your name?\n";
    > cin.get(yourname, 25);
    >
    > if (yourname == myname)


    You cant compare string like this. This will compare the addresses pointed
    to by 'yourname' and 'myname'. To compare string, use...

    if(strcmp(yourname, myname) == 0)


    > {
    > cout << "What is 2 + 2?\n";
    > cin >> answer;
    > cout << "Correct!\n";
    > }
    > else
    > {
    > cout << "What is 2 + 2?\n";
    > cin >> answer;
    > if (answer == 4)
    > {
    > cout << "I'm sorry. The answer is 22.\n";
    > }
    > if (answer != 4)
    > {
    > cout << "I'm sorry. The answer is 4.\n";
    > }
    > }
    > return 0;
    > #endif
    > }
     
    Ashish, Oct 6, 2003
    #8
    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. Replies:
    1
    Views:
    431
    Mike Schilling
    Jun 27, 2005
  2. Sam R
    Replies:
    1
    Views:
    511
  3. Nishad
    Replies:
    2
    Views:
    4,856
    Dimitre Novatchev
    Sep 30, 2006
  4. Merk
    Replies:
    12
    Views:
    1,403
    Jon Skeet [C# MVP]
    Dec 13, 2006
  5. laura
    Replies:
    2
    Views:
    246
    Walter Roberson
    Sep 22, 2006
Loading...

Share This Page